File Coverage

blib/lib/DBM/Deep/Array.pm
Criterion Covered Total %
statement 212 212 100.0
branch 68 72 94.4
condition 5 5 100.0
subroutine 30 30 100.0
pod 6 6 100.0
total 321 325 98.7


line stmt bran cond sub pod time code
1             package DBM::Deep::Array;
2              
3 20     20   430 use 5.008_004;
  20         72  
4              
5 20     20   111 use strict;
  20         39  
  20         624  
6 20     20   129 use warnings FATAL => 'all';
  20         40  
  20         891  
7 20     20   126 no warnings 'recursion';
  20         47  
  20         1265  
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             our $NEGATIVE_INDICES = 1;
13              
14 20     20   142 use base 'DBM::Deep';
  20         62  
  20         1792  
15              
16 20     20   148 use Scalar::Util ();
  20         52  
  20         47825  
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 37823 100   37823   93333 Scalar::Util::reftype $_[0] eq 'ARRAY' ? tied @{$_[0]} : $_[0];
  3218         8131  
28             }
29              
30 23     23   57 sub _repr { [] }
31              
32             sub TIEARRAY {
33 2160     2160   3856 my $class = shift;
34 2160         5400 my $args = $class->_get_args( @_ );
35              
36 2158         4982 $args->{type} = $class->TYPE_ARRAY;
37              
38 2158         5594 return $class->_init($args);
39             }
40              
41             sub FETCH {
42 520     520   17984 my $self = shift->_get_self;
43 520         954 my ($key) = @_;
44              
45 520         1373 $self->lock_shared;
46              
47 520 100       3478 if ( !defined $key ) {
    100          
    100          
48 1         12 $self->unlock;
49 1         16 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
50             }
51             elsif ( $key =~ /^-?\d+$/ ) {
52 231 100       635 if ( $key < 0 ) {
53 11         42 $key += $self->FETCHSIZE;
54 11 100       54 unless ( $key >= 0 ) {
55 1         11 $self->unlock;
56 1         6 return;
57             }
58             }
59             }
60             elsif ( $key ne 'length' ) {
61 1         4 $self->unlock;
62 1         8 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
63             }
64              
65 517         1564 my $rv = $self->SUPER::FETCH( $key );
66              
67 517         1436 $self->unlock;
68              
69 517         2256 return $rv;
70             }
71              
72             sub STORE {
73 303     303   2088 my $self = shift->_get_self;
74 303         632 my ($key, $value) = @_;
75              
76 303         753 $self->lock_exclusive;
77              
78 303         521 my $size;
79             my $idx_is_numeric;
80 303 100       2052 if ( !defined $key ) {
    100          
    100          
81 1         6 $self->unlock;
82 1         6 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
83             }
84             elsif ( $key =~ /^-?\d+$/ ) {
85 151         265 $idx_is_numeric = 1;
86 151 100       351 if ( $key < 0 ) {
87 2         7 $size = $self->FETCHSIZE;
88 2 100       116 if ( $key + $size < 0 ) {
89 1         15 die( "Modification of non-creatable array value attempted, subscript $key" );
90             }
91 1         5 $key += $size
92             }
93             }
94             elsif ( $key ne 'length' ) {
95 1         5 $self->unlock;
96 1         12 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97             }
98              
99 300         893 my $rv = $self->SUPER::STORE( $key, $value );
100              
101 300 100       649 if ( $idx_is_numeric ) {
102 150 100       499 $size = $self->FETCHSIZE unless defined $size;
103 150 100       392 if ( $key >= $size ) {
104 129         404 $self->STORESIZE( $key + 1 );
105             }
106             }
107              
108 300         730 $self->unlock;
109              
110 300         884 return $rv;
111             }
112              
113             sub EXISTS {
114 9     9   1447 my $self = shift->_get_self;
115 9         23 my ($key) = @_;
116              
117 9         28 $self->lock_shared;
118              
119 9 100       86 if ( !defined $key ) {
    100          
    50          
120 1         4 $self->unlock;
121 1         4 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
122             }
123             elsif ( $key =~ /^-?\d+$/ ) {
124 7 100       28 if ( $key < 0 ) {
125 2         8 $key += $self->FETCHSIZE;
126 2 100       12 unless ( $key >= 0 ) {
127 1         4 $self->unlock;
128 1         6 return;
129             }
130             }
131             }
132             elsif ( $key ne 'length' ) {
133 1         5 $self->unlock;
134 1         7 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
135             }
136              
137 6         38 my $rv = $self->SUPER::EXISTS( $key );
138              
139 6         20 $self->unlock;
140              
141 6         84 return $rv;
142             }
143              
144             sub DELETE {
145 20     20   48 my $self = shift->_get_self;
146 20         39 my ($key) = @_;
147 20         29 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
148              
149 20         59 $self->lock_exclusive;
150              
151 20         47 my $size = $self->FETCHSIZE;
152 20 100       151 if ( !defined $key ) {
    100          
    50          
153 1         5 $self->unlock;
154 1         4 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
155             }
156             elsif ( $key =~ /^-?\d+$/ ) {
157 18 100       43 if ( $key < 0 ) {
158 3         7 $key += $size;
159 3 100       10 unless ( $key >= 0 ) {
160 2         7 $self->unlock;
161 2         13 return;
162             }
163             }
164             }
165             elsif ( $key ne 'length' ) {
166 1         4 $self->unlock;
167 1         8 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
168             }
169              
170 16         50 my $rv = $self->SUPER::DELETE( $key );
171              
172 16 100 100     73 if ($rv && $key == $size - 1) {
173 10         34 $self->STORESIZE( $key );
174             }
175              
176 16         41 $self->unlock;
177              
178 16         44 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 287     287   13184 my $self = shift->_get_self;
186              
187 287         898 $self->lock_shared;
188              
189 287         662 my $SAVE_FILTER = $self->_engine->storage->{filter_fetch_value};
190 287         603 $self->_engine->storage->{filter_fetch_value} = undef;
191              
192 287   100     628 my $size = $self->FETCH('length') || 0;
193              
194 287         676 $self->_engine->storage->{filter_fetch_value} = $SAVE_FILTER;
195              
196 287         735 $self->unlock;
197              
198 287         979 return $size;
199             }
200              
201             sub STORESIZE {
202 150     150   404 my $self = shift->_get_self;
203 150         273 my ($new_length) = @_;
204              
205 150         425 $self->lock_exclusive;
206              
207 150         362 my $SAVE_FILTER = $self->_engine->storage->{filter_store_value};
208 150         320 $self->_engine->storage->{filter_store_value} = undef;
209              
210 150         459 my $result = $self->STORE('length', $new_length, 'length');
211              
212 150         374 $self->_engine->storage->{filter_store_value} = $SAVE_FILTER;
213              
214 150         375 $self->unlock;
215              
216 150         272 return $result;
217             }
218              
219             sub POP {
220 2     2   7 my $self = shift->_get_self;
221              
222 2         8 $self->lock_exclusive;
223              
224 2         9 my $length = $self->FETCHSIZE();
225              
226 2 100       17 if ($length) {
227 1         4 my $content = $self->FETCH( $length - 1 );
228 1         5 $self->DELETE( $length - 1 );
229              
230 1         3 $self->unlock;
231              
232 1         5 return $content;
233             }
234             else {
235 1         4 $self->unlock;
236 1         16 return;
237             }
238             }
239              
240             sub PUSH {
241 8     8   28 my $self = shift->_get_self;
242              
243 8         27 $self->lock_exclusive;
244              
245 8         29 my $length = $self->FETCHSIZE();
246              
247 8         41 for my $content (@_) {
248 20         57 $self->STORE( $length, $content );
249 20         47 $length++;
250             }
251              
252 8         29 $self->unlock;
253              
254 8         26 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     29   46 my $self = shift;
261 29         66 my ($old_key, $new_key) = @_;
262              
263 29         74 return $self->_engine->make_reference( $self, $old_key, $new_key );
264             }
265              
266             sub SHIFT {
267 3     3   45 my $self = shift->_get_self;
268 3         8 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
269              
270 3         13 $self->lock_exclusive;
271              
272 3         12 my $length = $self->FETCHSIZE();
273              
274 3 100       23 if ( !$length ) {
275 1         4 $self->unlock;
276 1         5 return;
277             }
278              
279 2         6 my $content = $self->DELETE( 0 );
280              
281             # Unless the deletion above has cleared the array ...
282 2 50       9 if ( $length > 1 ) {
283 2         11 for (my $i = 0; $i < $length - 1; $i++) {
284 4         12 $self->_move_value( $i+1, $i );
285             }
286              
287 2         12 $self->DELETE( $length - 1 );
288             }
289              
290 2         10 $self->unlock;
291              
292 2         12 return $content;
293             }
294              
295             sub UNSHIFT {
296 7     7   76 my $self = shift->_get_self;
297 7         21 my @new_elements = @_;
298              
299 7         33 $self->lock_exclusive;
300              
301 7         20 my $length = $self->FETCHSIZE();
302 7         25 my $new_size = scalar @new_elements;
303              
304 7 100       35 if ($length) {
305 6         27 for (my $i = $length - 1; $i >= 0; $i--) {
306 18         61 $self->_move_value( $i, $i+$new_size );
307             }
308              
309 6         26 $self->STORESIZE( $length + $new_size );
310             }
311              
312 7         45 for (my $i = 0; $i < $new_size; $i++) {
313 11         30 $self->STORE( $i, $new_elements[$i] );
314             }
315              
316 7         41 $self->unlock;
317              
318 7         52 return $length + $new_size;
319             }
320              
321             sub SPLICE {
322 7     7   59 my $self = shift->_get_self;
323              
324 7         34 $self->lock_exclusive;
325              
326 7         17 my $length = $self->FETCHSIZE();
327              
328             ##
329             # Calculate offset and length of splice
330             ##
331 7         14 my $offset = shift;
332 7 100       20 $offset = 0 unless defined $offset;
333 7 100       18 if ($offset < 0) { $offset += $length; }
  1         6  
334              
335 7         10 my $splice_length;
336 7 100       23 if (scalar @_) { $splice_length = shift; }
  5         9  
337 2         6 else { $splice_length = $length - $offset; }
338 7 100       15 if ($splice_length < 0) { $splice_length += ($length - $offset); }
  1         3  
339              
340             ##
341             # Setup array with new elements, and copy out old elements for return
342             ##
343 7         27 my @new_elements = @_;
344 7         13 my $new_size = scalar @new_elements;
345              
346             my @old_elements = map {
347 7         21 $self->FETCH( $_ )
  9         22  
348             } $offset .. ($offset + $splice_length - 1);
349              
350             ##
351             # Adjust array length, and shift elements to accommodate new section.
352             ##
353 7 50       22 if ( $new_size != $splice_length ) {
354 7 100       18 if ($new_size > $splice_length) {
355 3         12 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
356 5         14 $self->_move_value( $i, $i + ($new_size - $splice_length) );
357             }
358 3         34 $self->STORESIZE( $length + $new_size - $splice_length );
359             }
360             else {
361 4         13 for (my $i = $offset + $splice_length; $i < $length; $i++) {
362 2         7 $self->_move_value( $i, $i + ($new_size - $splice_length) );
363             }
364 4         19 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
365 7         25 $self->DELETE( $length - 1 );
366 7         20 $length--;
367             }
368             }
369             }
370              
371             ##
372             # Insert new elements into array
373             ##
374 7         55 for (my $i = $offset; $i < $offset + $new_size; $i++) {
375 7         21 $self->STORE( $i, shift @new_elements );
376             }
377              
378 7         30 $self->unlock;
379              
380             ##
381             # Return deleted section, or last element in scalar context.
382             ##
383 7 100       49 return wantarray ? @old_elements : $old_elements[-1];
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       21     sub EXTEND {}
391              
392             sub _copy_node {
393 23     23   40 my $self = shift;
394 23         41 my ($db_temp) = @_;
395              
396 23         68 my $length = $self->length();
397 23         87 for (my $index = 0; $index < $length; $index++) {
398 30         115 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
399             }
400              
401 23         64 return 1;
402             }
403              
404             sub _clear {
405 1     1   3 my $self = shift;
406              
407 1         5 my $size = $self->FETCHSIZE;
408 1         17 for my $key ( 0 .. $size - 1 ) {
409 5         15 $self->_engine->delete_key( $self, $key, $key );
410             }
411 1         36 $self->STORESIZE( 0 );
412              
413 1         3 return;
414             }
415              
416 46     46 1 3487 sub length { (shift)->FETCHSIZE(@_) }
417 2     2 1 13 sub pop { (shift)->POP(@_) }
418 5     5 1 23 sub push { (shift)->PUSH(@_) }
419 3     3 1 16 sub unshift { (shift)->UNSHIFT(@_) }
420 6     6 1 20 sub splice { (shift)->SPLICE(@_) }
421              
422             # This must be last otherwise we have to qualify all other calls to shift
423             # as calls to CORE::shift
424 2     2 1 13 sub shift { (CORE::shift)->SHIFT(@_) }
425              
426             1;
427             __END__