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 23     23   547 use 5.008_004;
  23         85  
4              
5 23     23   125 use strict;
  23         38  
  23         828  
6 23     23   102 use warnings FATAL => 'all';
  23         39  
  23         1866  
7 23     23   165 no warnings 'recursion';
  23         44  
  23         1577  
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 23     23   143 use base 'DBM::Deep';
  23         66  
  23         2393  
15              
16 23     23   141 use Scalar::Util ();
  23         55  
  23         60244  
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 71603 100   71603   173756 Scalar::Util::reftype $_[0] eq 'ARRAY' ? tied @{$_[0]} : $_[0];
  3224         10090  
28             }
29              
30 23     23   95 sub _repr { [] }
31              
32             sub TIEARRAY {
33 2170     2170   4509 my $class = shift;
34 2170         6768 my $args = $class->_get_args( @_ );
35              
36 2168         6122 $args->{type} = $class->TYPE_ARRAY;
37              
38 2168         7221 return $class->_init($args);
39             }
40              
41             sub FETCH {
42 1250     1250   32763 my $self = shift->_get_self;
43 1250         2855 my ($key) = @_;
44              
45 1250         4006 $self->lock_shared;
46              
47 1250 100       11038 if ( !defined $key ) {
    100          
    100          
48 1         7 $self->unlock;
49 1         6 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
50             }
51             elsif ( $key =~ /^-?\d+$/ ) {
52 243 100       858 if ( $key < 0 ) {
53 11         61 $key += $self->FETCHSIZE;
54 11 100       41 unless ( $key >= 0 ) {
55 1         5 $self->unlock;
56 1         6 return;
57             }
58             }
59             }
60             elsif ( $key ne 'length' ) {
61 1         7 $self->unlock;
62 1         9 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
63             }
64              
65 1247         4805 my $rv = $self->SUPER::FETCH( $key );
66              
67 1247         4185 $self->unlock;
68              
69 1247         5709 return $rv;
70             }
71              
72             sub STORE {
73 1029     1029   6316 my $self = shift->_get_self;
74 1029         2571 my ($key, $value) = @_;
75              
76 1029         5220 $self->lock_exclusive;
77              
78 1029         2176 my $size;
79             my $idx_is_numeric;
80 1029 100       12528 if ( !defined $key ) {
    100          
    100          
81 1         6 $self->unlock;
82 1         5 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
83             }
84             elsif ( $key =~ /^-?\d+$/ ) {
85 516         1130 $idx_is_numeric = 1;
86 516 100       1936 if ( $key < 0 ) {
87 2         11 $size = $self->FETCHSIZE;
88 2 100       12 if ( $key + $size < 0 ) {
89 1         19 die( "Modification of non-creatable array value attempted, subscript $key" );
90             }
91 1         3 $key += $size
92             }
93             }
94             elsif ( $key ne 'length' ) {
95 1         8 $self->unlock;
96 1         7 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97             }
98              
99 1026         4113 my $rv = $self->SUPER::STORE( $key, $value );
100              
101 1022 100       2826 if ( $idx_is_numeric ) {
102 511 100       2272 $size = $self->FETCHSIZE unless defined $size;
103 511 100       4541 if ( $key >= $size ) {
104 490         1871 $self->STORESIZE( $key + 1 );
105             }
106             }
107              
108 1022         3207 $self->unlock;
109              
110 1022         3252 return $rv;
111             }
112              
113             sub EXISTS {
114 9     9   2450 my $self = shift->_get_self;
115 9         25 my ($key) = @_;
116              
117 9         41 $self->lock_shared;
118              
119 9 100       87 if ( !defined $key ) {
    100          
    50          
120 1         6 $self->unlock;
121 1         257 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
122             }
123             elsif ( $key =~ /^-?\d+$/ ) {
124 7 100       25 if ( $key < 0 ) {
125 2         9 $key += $self->FETCHSIZE;
126 2 100       9 unless ( $key >= 0 ) {
127 1         5 $self->unlock;
128 1         7 return;
129             }
130             }
131             }
132             elsif ( $key ne 'length' ) {
133 1         5 $self->unlock;
134 1         6 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
135             }
136              
137 6         37 my $rv = $self->SUPER::EXISTS( $key );
138              
139 6         40 $self->unlock;
140              
141 6         50 return $rv;
142             }
143              
144             sub DELETE {
145 20     20   78 my $self = shift->_get_self;
146 20         79 my ($key) = @_;
147 20         30 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
148              
149 20         95 $self->lock_exclusive;
150              
151 20         1083 my $size = $self->FETCHSIZE;
152 20 100       208 if ( !defined $key ) {
    100          
    50          
153 1         5 $self->unlock;
154 1         6 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
155             }
156             elsif ( $key =~ /^-?\d+$/ ) {
157 18 100       91 if ( $key < 0 ) {
158 3         7 $key += $size;
159 3 100       12 unless ( $key >= 0 ) {
160 2         7 $self->unlock;
161 2         15 return;
162             }
163             }
164             }
165             elsif ( $key ne 'length' ) {
166 1         6 $self->unlock;
167 1         6 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
168             }
169              
170 16         84 my $rv = $self->SUPER::DELETE( $key );
171              
172 16 100 100     97 if ($rv && $key == $size - 1) {
173 10         50 $self->STORESIZE( $key );
174             }
175              
176 16         61 $self->unlock;
177              
178 16         74 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 1005     1005   14933 my $self = shift->_get_self;
186              
187 1005         6838 $self->lock_shared;
188              
189 1005         2773 my $SAVE_FILTER = $self->_engine->storage->{filter_fetch_value};
190 1005         2825 $self->_engine->storage->{filter_fetch_value} = undef;
191              
192 1005   100     3240 my $size = $self->FETCH('length') || 0;
193              
194 1005         2787 $self->_engine->storage->{filter_fetch_value} = $SAVE_FILTER;
195              
196 1005         4164 $self->unlock;
197              
198 1005         3119 return $size;
199             }
200              
201             sub STORESIZE {
202 511     511   1257 my $self = shift->_get_self;
203 511         1254 my ($new_length) = @_;
204              
205 511         1935 $self->lock_exclusive;
206              
207 511         1714 my $SAVE_FILTER = $self->_engine->storage->{filter_store_value};
208 511         1547 $self->_engine->storage->{filter_store_value} = undef;
209              
210 511         1972 my $result = $self->STORE('length', $new_length, 'length');
211              
212 511         1729 $self->_engine->storage->{filter_store_value} = $SAVE_FILTER;
213              
214 511         1510 $self->unlock;
215              
216 511         1198 return $result;
217             }
218              
219             sub POP {
220 2     2   8 my $self = shift->_get_self;
221              
222 2         10 $self->lock_exclusive;
223              
224 2         8 my $length = $self->FETCHSIZE();
225              
226 2 100       11 if ($length) {
227 1         4 my $content = $self->FETCH( $length - 1 );
228 1         8 $self->DELETE( $length - 1 );
229              
230 1         5 $self->unlock;
231              
232 1         9 return $content;
233             }
234             else {
235 1         6 $self->unlock;
236 1         23 return;
237             }
238             }
239              
240             sub PUSH {
241 365     365   4462 my $self = shift->_get_self;
242              
243 365         1435 $self->lock_exclusive;
244              
245 365         1193 my $length = $self->FETCHSIZE();
246              
247 365         1245 for my $content (@_) {
248 377         1341 $self->STORE( $length, $content );
249 374         1242 $length++;
250             }
251              
252 362         1061 $self->unlock;
253              
254 362         2507 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   61 my $self = shift;
261 29         67 my ($old_key, $new_key) = @_;
262              
263 29         128 return $self->_engine->make_reference( $self, $old_key, $new_key );
264             }
265              
266             sub SHIFT {
267 3     3   82 my $self = shift->_get_self;
268 3         7 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
269              
270 3         18 $self->lock_exclusive;
271              
272 3         14 my $length = $self->FETCHSIZE();
273              
274 3 100       15 if ( !$length ) {
275 1         7 $self->unlock;
276 1         7 return;
277             }
278              
279 2         10 my $content = $self->DELETE( 0 );
280              
281             # Unless the deletion above has cleared the array ...
282 2 50       11 if ( $length > 1 ) {
283 2         12 for (my $i = 0; $i < $length - 1; $i++) {
284 4         22 $self->_move_value( $i+1, $i );
285             }
286              
287 2         12 $self->DELETE( $length - 1 );
288             }
289              
290 2         13 $self->unlock;
291              
292 2         30 return $content;
293             }
294              
295             sub UNSHIFT {
296 7     7   106 my $self = shift->_get_self;
297 7         22 my @new_elements = @_;
298              
299 7         30 $self->lock_exclusive;
300              
301 7         28 my $length = $self->FETCHSIZE();
302 7         18 my $new_size = scalar @new_elements;
303              
304 7 100       24 if ($length) {
305 6         50 for (my $i = $length - 1; $i >= 0; $i--) {
306 18         63 $self->_move_value( $i, $i+$new_size );
307             }
308              
309 6         32 $self->STORESIZE( $length + $new_size );
310             }
311              
312 7         33 for (my $i = 0; $i < $new_size; $i++) {
313 11         37 $self->STORE( $i, $new_elements[$i] );
314             }
315              
316 7         26 $self->unlock;
317              
318 7         88 return $length + $new_size;
319             }
320              
321             sub SPLICE {
322 7     7   96 my $self = shift->_get_self;
323              
324 7         47 $self->lock_exclusive;
325              
326 7         89 my $length = $self->FETCHSIZE();
327              
328             ##
329             # Calculate offset and length of splice
330             ##
331 7         22 my $offset = shift;
332 7 100       31 $offset = 0 unless defined $offset;
333 7 100       31 if ($offset < 0) { $offset += $length; }
  1         5  
334              
335 7         16 my $splice_length;
336 7 100       30 if (scalar @_) { $splice_length = shift; }
  5         13  
337 2         9 else { $splice_length = $length - $offset; }
338 7 100       29 if ($splice_length < 0) { $splice_length += ($length - $offset); }
  1         4  
339              
340             ##
341             # Setup array with new elements, and copy out old elements for return
342             ##
343 7         25 my @new_elements = @_;
344 7         18 my $new_size = scalar @new_elements;
345              
346             my @old_elements = map {
347 7         42 $self->FETCH( $_ )
  9         33  
348             } $offset .. ($offset + $splice_length - 1);
349              
350             ##
351             # Adjust array length, and shift elements to accommodate new section.
352             ##
353 7 50       37 if ( $new_size != $splice_length ) {
354 7 100       27 if ($new_size > $splice_length) {
355 3         20 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
356 5         30 $self->_move_value( $i, $i + ($new_size - $splice_length) );
357             }
358 3         24 $self->STORESIZE( $length + $new_size - $splice_length );
359             }
360             else {
361 4         26 for (my $i = $offset + $splice_length; $i < $length; $i++) {
362 2         15 $self->_move_value( $i, $i + ($new_size - $splice_length) );
363             }
364 4         25 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
365 7         47 $self->DELETE( $length - 1 );
366 7         54 $length--;
367             }
368             }
369             }
370              
371             ##
372             # Insert new elements into array
373             ##
374 7         50 for (my $i = $offset; $i < $offset + $new_size; $i++) {
375 7         34 $self->STORE( $i, shift @new_elements );
376             }
377              
378 7         37 $self->unlock;
379              
380             ##
381             # Return deleted section, or last element in scalar context.
382             ##
383 7 100       131 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       23     sub EXTEND {}
391              
392             sub _copy_node {
393 23     23   60 my $self = shift;
394 23         55 my ($db_temp) = @_;
395              
396 23         83 my $length = $self->length();
397 23         131 for (my $index = 0; $index < $length; $index++) {
398 30         144 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
399             }
400              
401 23         63 return 1;
402             }
403              
404             sub _clear {
405 1     1   1 my $self = shift;
406              
407 1         3 my $size = $self->FETCHSIZE;
408 1         5 for my $key ( 0 .. $size - 1 ) {
409 5         11 $self->_engine->delete_key( $self, $key, $key );
410             }
411 1         6 $self->STORESIZE( 0 );
412              
413 1         18 return;
414             }
415              
416 46     46 1 6393 sub length { (shift)->FETCHSIZE(@_) }
417 2     2 1 13 sub pop { (shift)->POP(@_) }
418 5     5 1 37 sub push { (shift)->PUSH(@_) }
419 3     3 1 23 sub unshift { (shift)->UNSHIFT(@_) }
420 6     6 1 48 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 12 sub shift { (CORE::shift)->SHIFT(@_) }
425              
426             1;
427             __END__