1 package DBM::Deep::Array;
6 use warnings FATAL => 'all';
8 # This is to allow DBM::Deep::Array to handle negative indices on
9 # its own. Otherwise, Perl would intercept the call to negative
10 # indices for us. This was causing bugs for negative index handling.
11 our $NEGATIVE_INDICES = 1;
18 eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
25 my $args = $class->_get_args( @_ );
27 $args->{type} = $class->TYPE_ARRAY;
29 my $self = $class->_init($args);
37 my $self = shift->_get_self;
39 warn "ARRAY:FETCH( $key )\n" if DBM::Deep::DEBUG;
43 if ( !defined $key ) {
45 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
47 elsif ( $key =~ /^-?\d+$/ ) {
49 $key += $self->FETCHSIZE;
50 unless ( $key >= 0 ) {
56 elsif ( $key ne 'length' ) {
58 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
61 my $rv = $self->SUPER::FETCH( $key );
69 my $self = shift->_get_self;
70 my ($key, $value) = @_;
71 warn "ARRAY::STORE($self, $key)\n" if DBM::Deep::DEBUG;
73 $self->lock_exclusive;
77 if ( !defined $key ) {
79 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
81 elsif ( $key =~ /^-?\d+$/ ) {
84 $size = $self->FETCHSIZE;
85 if ( $key + $size < 0 ) {
86 die( "Modification of non-creatable array value attempted, subscript $key" );
91 elsif ( $key ne 'length' ) {
93 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
96 my $rv = $self->SUPER::STORE( $key, $value );
98 if ( $idx_is_numeric ) {
99 $size = $self->FETCHSIZE unless defined $size;
100 if ( $key >= $size ) {
101 $self->STORESIZE( $key + 1 );
111 my $self = shift->_get_self;
113 warn "ARRAY::EXISTS($self, $key)\n" if DBM::Deep::DEBUG;
117 if ( !defined $key ) {
119 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
121 elsif ( $key =~ /^-?\d+$/ ) {
123 $key += $self->FETCHSIZE;
124 unless ( $key >= 0 ) {
130 elsif ( $key ne 'length' ) {
132 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
135 my $rv = $self->SUPER::EXISTS( $key );
143 my $self = shift->_get_self;
145 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
147 $self->lock_exclusive;
149 my $size = $self->FETCHSIZE;
150 if ( !defined $key ) {
152 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
154 elsif ( $key =~ /^-?\d+$/ ) {
157 unless ( $key >= 0 ) {
163 elsif ( $key ne 'length' ) {
165 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
168 my $rv = $self->SUPER::DELETE( $key );
170 if ($rv && $key == $size - 1) {
171 $self->STORESIZE( $key );
179 # Now that we have a real Reference sector, we should store arrayzize there. However,
180 # arraysize needs to be transactionally-aware, so a simple location to store it isn't
183 my $self = shift->_get_self;
184 warn "ARRAY::FETCHSIZE($self)\n" if DBM::Deep::DEBUG;
188 my $SAVE_FILTER = $self->_engine->storage->{filter_fetch_value};
189 $self->_engine->storage->{filter_fetch_value} = undef;
191 # If there is no flushing, then things get out of sync.
192 my $size = $self->FETCH('length') || 0;
194 $self->_engine->storage->{filter_fetch_value} = $SAVE_FILTER;
202 my $self = shift->_get_self;
203 my ($new_length) = @_;
204 warn "ARRAY::STORESIZE($self, $new_length)\n" if DBM::Deep::DEBUG;
206 $self->lock_exclusive;
208 my $SAVE_FILTER = $self->_engine->storage->{filter_store_value};
209 $self->_engine->storage->{filter_store_value} = undef;
211 my $result = $self->STORE('length', $new_length, 'length');
213 $self->_engine->storage->{filter_store_value} = $SAVE_FILTER;
221 my $self = shift->_get_self;
222 warn "ARRAY::POP($self)\n" if DBM::Deep::DEBUG;
224 $self->lock_exclusive;
226 my $length = $self->FETCHSIZE();
229 my $content = $self->FETCH( $length - 1 );
230 $self->DELETE( $length - 1 );
243 my $self = shift->_get_self;
244 warn "ARRAY::PUSH($self)\n" if DBM::Deep::DEBUG;
246 $self->lock_exclusive;
248 my $length = $self->FETCHSIZE();
250 while (my $content = shift @_) {
251 $self->STORE( $length, $content );
260 # XXX This really needs to be something more direct within the file, not a
261 # fetch and re-store. -RobK, 2007-09-20
264 my ($old_key, $new_key) = @_;
266 return $self->_engine->make_reference( $self, $old_key, $new_key );
270 my $self = shift->_get_self;
271 warn "ARRAY::SHIFT($self)\n" if DBM::Deep::DEBUG;
273 $self->lock_exclusive;
275 my $length = $self->FETCHSIZE();
282 my $content = $self->DELETE( 0 );
284 # Unless the deletion above has cleared the array ...
286 for (my $i = 0; $i < $length - 1; $i++) {
287 $self->_move_value( $i+1, $i );
290 $self->DELETE( $length - 1 );
299 my $self = shift->_get_self;
300 warn "ARRAY::UNSHIFT($self)\n" if DBM::Deep::DEBUG;
301 my @new_elements = @_;
303 $self->lock_exclusive;
305 my $length = $self->FETCHSIZE();
306 my $new_size = scalar @new_elements;
309 for (my $i = $length - 1; $i >= 0; $i--) {
310 $self->_move_value( $i, $i+$new_size );
313 $self->STORESIZE( $length + $new_size );
316 for (my $i = 0; $i < $new_size; $i++) {
317 $self->STORE( $i, $new_elements[$i] );
322 return $length + $new_size;
326 my $self = shift->_get_self;
327 warn "ARRAY::SPLICE($self)\n" if DBM::Deep::DEBUG;
329 $self->lock_exclusive;
331 my $length = $self->FETCHSIZE();
334 # Calculate offset and length of splice
337 $offset = 0 unless defined $offset;
338 if ($offset < 0) { $offset += $length; }
341 if (scalar @_) { $splice_length = shift; }
342 else { $splice_length = $length - $offset; }
343 if ($splice_length < 0) { $splice_length += ($length - $offset); }
346 # Setup array with new elements, and copy out old elements for return
348 my @new_elements = @_;
349 my $new_size = scalar @new_elements;
351 my @old_elements = map {
353 } $offset .. ($offset + $splice_length - 1);
356 # Adjust array length, and shift elements to accomodate new section.
358 if ( $new_size != $splice_length ) {
359 if ($new_size > $splice_length) {
360 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
361 $self->_move_value( $i, $i + ($new_size - $splice_length) );
363 $self->STORESIZE( $length + $new_size - $splice_length );
366 for (my $i = $offset + $splice_length; $i < $length; $i++) {
367 $self->_move_value( $i, $i + ($new_size - $splice_length) );
369 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
370 $self->DELETE( $length - 1 );
377 # Insert new elements into array
379 for (my $i = $offset; $i < $offset + $new_size; $i++) {
380 $self->STORE( $i, shift @new_elements );
386 # Return deleted section, or last element in scalar context.
388 return wantarray ? @old_elements : $old_elements[-1];
391 # We don't need to populate it, yet.
392 # It will be useful, though, when we split out HASH and ARRAY
394 warn "ARRAY::EXTEND()\n" if DBM::Deep::DEBUG;
396 # Perl will call EXTEND() when the array is likely to grow.
397 # We don't care, but include it because it gets called at times.
405 my $length = $self->length();
406 for (my $index = 0; $index < $length; $index++) {
407 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
414 # Public method aliases
416 sub length { (shift)->FETCHSIZE(@_) }
417 sub pop { (shift)->POP(@_) }
418 sub push { (shift)->PUSH(@_) }
419 sub unshift { (shift)->UNSHIFT(@_) }
420 sub splice { (shift)->SPLICE(@_) }
422 # This must be last otherwise we have to qualify all other calls to shift
423 # as calls to CORE::shift
424 sub shift { (CORE::shift)->SHIFT(@_) }