blib/lib/DBM/Deep/Array.pm

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