blib/lib/DBM/Deep/Array.pm

filter: Statementstmt 100% 214 / 214 Branchbran 95.4% 84 / 88 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 30 / 30 Podpod 100% 6 / 6 totaltotal 98.8% 342 / 346 CC 49 SCAR 17.2
CC
49
Cov
99%
SPLICE
12.0
STORE
9.0
DELETE
8.0
CRAP 5.6
« blib/lib/DBM/Deep/Sector/File.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/BucketList.pm »
1 package DBM::Deep::Array;
2  
3 22 ▶use 5.008_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # This is to allow DBM::Deep::Array to handle negative indices on
10 # its own. Otherwise, Perl would intercept the call to negative
11 # indices for us. This was causing bugs for negative index handling.
12 22 our $NEGATIVE_INDICES = 1;
13  
14 22 ▶use base 'DBM::Deep';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub _get_self {
19 # We used to have
20 # eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
21 # but this does not always work during global destruction (DBM::Deep’s
22 # destructor calls this method), but will return $_[0] even when $_[0]
23 # is tied, if it’s tied to undef. In those cases it’s better to return
24 # undef, so the destructor can tell not to do anything, and, if any-
25 # thing else calls us, it will fail with a more helpful error message.
26  
27 38406 ▶ Scalar::Util::reftype $_[0] eq 'ARRAY' ? tied @{$_[0]} : $_[0];
Subroutine _get_self: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'ARRAY' ? : 3220 35186 38406
28 }
29  
30 23 ▶sub _repr { [] }
Subroutine _repr: called CC=1 SCAR=0.0
31  
32 sub TIEARRAY {
33 2166 ▶ my $class = shift;
Subroutine TIEARRAY: called CC=1 SCAR=0.0
34 2166 my $args = $class->_get_args( @_ );
35  
36 2164 $args->{type} = $class->TYPE_ARRAY;
37  
38 2164 return $class->_init($args);
39 }
40  
41 sub FETCH {
42 528 ▶ my $self = shift->_get_self;
Subroutine FETCH: called CC=6 SCAR=17.9
43 528 my ($key) = @_;
44  
45 528 $self->lock_shared;
46  
47 528 ▶ if ( !defined $key ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 1 527 528
elsif ($key =~ /^-?\d+$/) { } 231 296 527
elsif ($key ne 'length') { } 1 295 296
48 1 $self->unlock;
49 1 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
50 }
51 elsif ( $key =~ /^-?\d+$/ ) {
52 231 ▶ if ( $key < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key < 0) 11 220 231
53 11 $key += $self->FETCHSIZE;
54 11 ▶ unless ( $key >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key >= 0) 1 10 11
55 1 $self->unlock;
56 1 return;
57 }
58 }
59 }
60 elsif ( $key ne 'length' ) {
61 1 $self->unlock;
62 1 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
63 }
64  
65 525 my $rv = $self->SUPER::FETCH( $key );
66  
67 525 $self->unlock;
68  
69 525 return $rv;
70 }
71  
72 sub STORE {
73 319 ▶ my $self = shift->_get_self;
Subroutine STORE: called CC=9 SCAR=22.0
74 319 my ($key, $value) = @_;
75  
76 319 $self->lock_exclusive;
77  
78 319 my $size;
79 my $idx_is_numeric;
80 319 ▶ if ( !defined $key ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 1 318 319
elsif ($key =~ /^-?\d+$/) { } 159 159 318
elsif ($key ne 'length') { } 1 158 159
81 1 $self->unlock;
82 1 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
83 }
84 elsif ( $key =~ /^-?\d+$/ ) {
85 159 $idx_is_numeric = 1;
86 159 ▶ if ( $key < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key < 0) 2 157 159
87 2 $size = $self->FETCHSIZE;
88 2 ▶ if ( $key + $size < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key + $size < 0) 1 1 2
89 1 die( "Modification of non-creatable array value attempted, subscript $key" );
90 }
91 1 $key += $size
92 }
93 }
94 elsif ( $key ne 'length' ) {
95 1 $self->unlock;
96 1 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97 }
98  
99 316 my $rv = $self->SUPER::STORE( $key, $value );
100  
101 316 ▶ if ( $idx_is_numeric ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx_is_numeric) 158 158 316
102 158 ▶ $size = $self->FETCHSIZE unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 157 1 158
103 158 ▶ if ( $key >= $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key >= $size) 137 21 158
104 137 $self->STORESIZE( $key + 1 );
105 }
106 }
107  
108 316 $self->unlock;
109  
110 316 return $rv;
111 }
112  
113 sub EXISTS {
114 9 ▶ my $self = shift->_get_self;
Subroutine EXISTS: called CC=6 SCAR=17.9
115 9 my ($key) = @_;
116  
117 9 $self->lock_shared;
118  
119 9 ▶ if ( !defined $key ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 1 8 9
elsif ($key =~ /^-?\d+$/) { } 7 1 8
elsif ($key ne 'length') { } 1 0 1
120 1 $self->unlock;
121 1 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
122 }
123 elsif ( $key =~ /^-?\d+$/ ) {
124 7 ▶ if ( $key < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key < 0) 2 5 7
125 2 $key += $self->FETCHSIZE;
126 2 ▶ unless ( $key >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key >= 0) 1 1 2
127 1 $self->unlock;
128 1 return;
129 }
130 }
131 }
132 elsif ( $key ne 'length' ) {
133 1 $self->unlock;
134 1 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
135 }
136  
137 6 my $rv = $self->SUPER::EXISTS( $key );
138  
139 6 $self->unlock;
140  
141 6 return $rv;
142 }
143  
144 sub DELETE {
145 20 ▶ my $self = shift->_get_self;
Subroutine DELETE: called CC=8 SCAR=20.8
146 20 my ($key) = @_;
147 20 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
148  
149 20 $self->lock_exclusive;
150  
151 20 my $size = $self->FETCHSIZE;
152 20 ▶ if ( !defined $key ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 1 19 20
elsif ($key =~ /^-?\d+$/) { } 18 1 19
elsif ($key ne 'length') { } 1 0 1
153 1 $self->unlock;
154 1 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
155 }
156 elsif ( $key =~ /^-?\d+$/ ) {
157 18 ▶ if ( $key < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key < 0) 3 15 18
158 3 $key += $size;
159 3 ▶ unless ( $key >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key >= 0) 2 1 3
160 2 $self->unlock;
161 2 return;
162 }
163 }
164 }
165 elsif ( $key ne 'length' ) {
166 1 $self->unlock;
167 1 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
168 }
169  
170 16 my $rv = $self->SUPER::DELETE( $key );
171  
172 16 ▶ if ($rv && $key == $size - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $key == $size - 1) 10 6 16
Condition100%3/3
$rv and $key == $size - 1
!ll&&!rl&&r
2410
MC/DC100%2/2
$rv and $key == $size - 1
$rv $key == $size - 1
Truth table100%3/3
A and B
A= $rvB= $key == $size - 1
ABresult
0X0
100
111
173 10 $self->STORESIZE( $key );
174 }
175  
176 16 $self->unlock;
177  
178 16 return $rv;
179 }
180  
181 # Now that we have a real Reference sector, we should store arrayzize there.
182 # However, arraysize needs to be transactionally-aware, so a simple location to
183 # store it isn't going to work.
184 sub FETCHSIZE {
185 295 ▶ my $self = shift->_get_self;
Subroutine FETCHSIZE: called CC=2 SCAR=6.9
186  
187 295 $self->lock_shared;
188  
189 295 my $SAVE_FILTER = $self->_engine->storage->{filter_fetch_value};
190 295 $self->_engine->storage->{filter_fetch_value} = undef;
191  
192 295 ▶ my $size = $self->FETCH('length') || 0;
Condition100%2/2
$self->FETCH('length') || 0
l!l
23065
MC/DC100%1/1
$self->FETCH('length') || 0
$self->FETCH('length')
Truth table100%2/2
A
A= $self->FETCH('length')
Aresult
11
00
193  
194 295 $self->_engine->storage->{filter_fetch_value} = $SAVE_FILTER;
195  
196 295 $self->unlock;
197  
198 295 return $size;
199 }
200  
201 sub STORESIZE {
202 158 ▶ my $self = shift->_get_self;
Subroutine STORESIZE: called CC=1 SCAR=0.0
203 158 my ($new_length) = @_;
204  
205 158 $self->lock_exclusive;
206  
207 158 my $SAVE_FILTER = $self->_engine->storage->{filter_store_value};
208 158 $self->_engine->storage->{filter_store_value} = undef;
209  
210 158 my $result = $self->STORE('length', $new_length, 'length');
211  
212 158 $self->_engine->storage->{filter_store_value} = $SAVE_FILTER;
213  
214 158 $self->unlock;
215  
216 158 return $result;
217 }
218  
219 sub POP {
220 2 ▶ my $self = shift->_get_self;
Subroutine POP: called CC=2 SCAR=6.9
221  
222 2 $self->lock_exclusive;
223  
224 2 my $length = $self->FETCHSIZE();
225  
226 2 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) { } 1 1 2
227 1 my $content = $self->FETCH( $length - 1 );
228 1 $self->DELETE( $length - 1 );
229  
230 1 $self->unlock;
231  
232 1 return $content;
233 }
234 else {
235 1 $self->unlock;
236 1 return;
237 }
238 }
239  
240 sub PUSH {
241 8 ▶ my $self = shift->_get_self;
Subroutine PUSH: called CC=2 SCAR=6.9
242  
243 8 $self->lock_exclusive;
244  
245 8 my $length = $self->FETCHSIZE();
246  
247 8 for my $content (@_) {
248 20 $self->STORE( $length, $content );
249 20 $length++;
250 }
251  
252 8 $self->unlock;
253  
254 8 return $length;
255 }
256  
257 # XXX This really needs to be something more direct within the file, not a
258 # fetch and re-store. -RobK, 2007-09-20
259 sub _move_value {
260 29 ▶ my $self = shift;
Subroutine _move_value: called CC=1 SCAR=0.0
261 29 my ($old_key, $new_key) = @_;
262  
263 29 return $self->_engine->make_reference( $self, $old_key, $new_key );
264 }
265  
266 sub SHIFT {
267 3 ▶ my $self = shift->_get_self;
Subroutine SHIFT: called CC=4 SCAR=13.9
268 3 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
269  
270 3 $self->lock_exclusive;
271  
272 3 my $length = $self->FETCHSIZE();
273  
274 3 ▶ if ( !$length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($length) 1 2 3
275 1 $self->unlock;
276 1 return;
277 }
278  
279 2 my $content = $self->DELETE( 0 );
280  
281 # Unless the deletion above has cleared the array ...
282 2 ▶ if ( $length > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 1) 2 0 2
283 2 ▶ for (my $i = 0; $i < $length - 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length - 1) 4 2 6
284 4 $self->_move_value( $i+1, $i );
285 }
286  
287 2 $self->DELETE( $length - 1 );
288 }
289  
290 2 $self->unlock;
291  
292 2 return $content;
293 }
294  
295 sub UNSHIFT {
296 7 ▶ my $self = shift->_get_self;
Subroutine UNSHIFT: called CC=4 SCAR=13.9
297 7 my @new_elements = @_;
298  
299 7 $self->lock_exclusive;
300  
301 7 my $length = $self->FETCHSIZE();
302 7 my $new_size = scalar @new_elements;
303  
304 7 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 6 1 7
305 6 ▶ for (my $i = $length - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 18 6 24
306 18 $self->_move_value( $i, $i+$new_size );
307 }
308  
309 6 $self->STORESIZE( $length + $new_size );
310 }
311  
312 7 ▶ for (my $i = 0; $i < $new_size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $new_size) 11 7 18
313 11 $self->STORE( $i, $new_elements[$i] );
314 }
315  
316 7 $self->unlock;
317  
318 7 return $length + $new_size;
319 }
320  
321 sub SPLICE {
322 7 ▶ my $self = shift->_get_self;
Subroutine SPLICE: called CC=12 SCAR=24.8
323  
324 7 $self->lock_exclusive;
325  
326 7 my $length = $self->FETCHSIZE();
327  
328 ##
329 # Calculate offset and length of splice
330 ##
331 7 my $offset = shift;
332 7 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 6 7
333 7 ▶ if ($offset < 0) { $offset += $length; }
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 1 6 7
334  
335 7 my $splice_length;
336 7 ▶ if (scalar @_) { $splice_length = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 5 2 7
337 2 else { $splice_length = $length - $offset; }
338 7 ▶ if ($splice_length < 0) { $splice_length += ($length - $offset); }
Branch: 1 branch
BranchTrueFalseTotal
if ($splice_length < 0) 1 6 7
339  
340 ##
341 # Setup array with new elements, and copy out old elements for return
342 ##
343 7 my @new_elements = @_;
344 7 my $new_size = scalar @new_elements;
345  
346 my @old_elements = map {
347 7 $self->FETCH( $_ )
348 } $offset .. ($offset + $splice_length - 1);
349  
350 ##
351 # Adjust array length, and shift elements to accommodate new section.
352 ##
353 7 ▶ if ( $new_size != $splice_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_size != $splice_length) 7 0 7
354 7 ▶ if ($new_size > $splice_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_size > $splice_length) { } 3 4 7
355 3 ▶ for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $offset + $splice_length) 5 3 8
356 5 $self->_move_value( $i, $i + ($new_size - $splice_length) );
357 }
358 3 $self->STORESIZE( $length + $new_size - $splice_length );
359 }
360 else {
361 4 ▶ for (my $i = $offset + $splice_length; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 2 4 6
362 2 $self->_move_value( $i, $i + ($new_size - $splice_length) );
363 }
364 4 ▶ for (my $i = 0; $i < $splice_length - $new_size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $splice_length - $new_size) 7 4 11
365 7 $self->DELETE( $length - 1 );
366 7 $length--;
367 }
368 }
369 }
370  
371 ##
372 # Insert new elements into array
373 ##
374 7 ▶ for (my $i = $offset; $i < $offset + $new_size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $offset + $new_size) 7 7 14
375 7 $self->STORE( $i, shift @new_elements );
376 }
377  
378 7 $self->unlock;
379  
380 ##
381 # Return deleted section, or last element in scalar context.
382 ##
383 7 ▶ return wantarray ? @old_elements : $old_elements[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 2 7
384 }
385  
386 # We don't need to populate it, yet.
387 # It will be useful, though, when we split out HASH and ARRAY
388 # Perl will call EXTEND() when the array is likely to grow.
389 # We don't care, but include it because it gets called at times.
390 ▶sub EXTEND {}
Subroutine EXTEND: called CC=1 SCAR=0.0
391  
392 sub _copy_node {
393 23 ▶ my $self = shift;
Subroutine _copy_node: called CC=2 SCAR=6.9
394 23 my ($db_temp) = @_;
395  
396 23 my $length = $self->length();
397 23 ▶ for (my $index = 0; $index < $length; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $length) 30 23 53
398 30 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
399 }
400  
401 23 return 1;
402 }
403  
404 sub _clear {
405 1 ▶ my $self = shift;
Subroutine _clear: called CC=2 SCAR=6.9
406  
407 1 my $size = $self->FETCHSIZE;
408 1 for my $key ( 0 .. $size - 1 ) {
409 5 $self->_engine->delete_key( $self, $key, $key );
410 }
411 1 $self->STORESIZE( 0 );
412  
413 1 return;
414 }
415  
416 46 ▶sub length { (shift)->FETCHSIZE(@_) }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
417 2 ▶sub pop { (shift)->POP(@_) }
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
418 5 ▶sub push { (shift)->PUSH(@_) }
Subroutine push: called CC=1 SCAR=0.0
Pod documented
419 3 ▶sub unshift { (shift)->UNSHIFT(@_) }
Subroutine unshift: called CC=1 SCAR=0.0
Pod documented
420 6 ▶sub splice { (shift)->SPLICE(@_) }
Subroutine splice: called CC=1 SCAR=0.0
Pod documented
421  
422 # This must be last otherwise we have to qualify all other calls to shift
423 # as calls to CORE::shift
424 2 ▶sub shift { (CORE::shift)->SHIFT(@_) }
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
425  
426 22 1;
427 __END__
« blib/lib/DBM/Deep/Sector/File.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/BucketList.pm »