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 return $class->_init($args);
33 my $self = shift->_get_self;
38 if ( !defined $key ) {
40 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
42 elsif ( $key =~ /^-?\d+$/ ) {
44 $key += $self->FETCHSIZE;
45 unless ( $key >= 0 ) {
51 elsif ( $key ne 'length' ) {
53 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
56 my $rv = $self->SUPER::FETCH( $key );
64 my $self = shift->_get_self;
65 my ($key, $value) = @_;
67 $self->lock_exclusive;
71 if ( !defined $key ) {
73 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
75 elsif ( $key =~ /^-?\d+$/ ) {
78 $size = $self->FETCHSIZE;
79 if ( $key + $size < 0 ) {
80 die( "Modification of non-creatable array value attempted, subscript $key" );
85 elsif ( $key ne 'length' ) {
87 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
90 my $rv = $self->SUPER::STORE( $key, $value );
92 if ( $idx_is_numeric ) {
93 $size = $self->FETCHSIZE unless defined $size;
94 if ( $key >= $size ) {
95 $self->STORESIZE( $key + 1 );
105 my $self = shift->_get_self;
110 if ( !defined $key ) {
112 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
114 elsif ( $key =~ /^-?\d+$/ ) {
116 $key += $self->FETCHSIZE;
117 unless ( $key >= 0 ) {
123 elsif ( $key ne 'length' ) {
125 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
128 my $rv = $self->SUPER::EXISTS( $key );
136 my $self = shift->_get_self;
138 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
140 $self->lock_exclusive;
142 my $size = $self->FETCHSIZE;
143 if ( !defined $key ) {
145 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
147 elsif ( $key =~ /^-?\d+$/ ) {
150 unless ( $key >= 0 ) {
156 elsif ( $key ne 'length' ) {
158 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
161 my $rv = $self->SUPER::DELETE( $key );
163 if ($rv && $key == $size - 1) {
164 $self->STORESIZE( $key );
172 # Now that we have a real Reference sector, we should store arrayzize there. However,
173 # arraysize needs to be transactionally-aware, so a simple location to store it isn't
176 my $self = shift->_get_self;
180 my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
181 $self->_storage->{filter_fetch_value} = undef;
183 my $size = $self->FETCH('length') || 0;
185 $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
193 my $self = shift->_get_self;
194 my ($new_length) = @_;
196 $self->lock_exclusive;
198 my $SAVE_FILTER = $self->_storage->{filter_store_value};
199 $self->_storage->{filter_store_value} = undef;
201 my $result = $self->STORE('length', $new_length, 'length');
203 $self->_storage->{filter_store_value} = $SAVE_FILTER;
211 my $self = shift->_get_self;
213 $self->lock_exclusive;
215 my $length = $self->FETCHSIZE();
218 my $content = $self->FETCH( $length - 1 );
219 $self->DELETE( $length - 1 );
232 my $self = shift->_get_self;
234 $self->lock_exclusive;
236 my $length = $self->FETCHSIZE();
238 while (my $content = shift @_) {
239 $self->STORE( $length, $content );
248 # XXX This really needs to be something more direct within the file, not a
249 # fetch and re-store. -RobK, 2007-09-20
252 my ($old_key, $new_key) = @_;
254 return $self->_engine->make_reference( $self, $old_key, $new_key );
258 my $self = shift->_get_self;
259 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
261 $self->lock_exclusive;
263 my $length = $self->FETCHSIZE();
270 my $content = $self->DELETE( 0 );
272 # Unless the deletion above has cleared the array ...
274 for (my $i = 0; $i < $length - 1; $i++) {
275 $self->_move_value( $i+1, $i );
278 $self->DELETE( $length - 1 );
287 my $self = shift->_get_self;
288 my @new_elements = @_;
290 $self->lock_exclusive;
292 my $length = $self->FETCHSIZE();
293 my $new_size = scalar @new_elements;
296 for (my $i = $length - 1; $i >= 0; $i--) {
297 $self->_move_value( $i, $i+$new_size );
300 $self->STORESIZE( $length + $new_size );
303 for (my $i = 0; $i < $new_size; $i++) {
304 $self->STORE( $i, $new_elements[$i] );
309 return $length + $new_size;
313 my $self = shift->_get_self;
315 $self->lock_exclusive;
317 my $length = $self->FETCHSIZE();
320 # Calculate offset and length of splice
323 $offset = 0 unless defined $offset;
324 if ($offset < 0) { $offset += $length; }
327 if (scalar @_) { $splice_length = shift; }
328 else { $splice_length = $length - $offset; }
329 if ($splice_length < 0) { $splice_length += ($length - $offset); }
332 # Setup array with new elements, and copy out old elements for return
334 my @new_elements = @_;
335 my $new_size = scalar @new_elements;
337 my @old_elements = map {
339 } $offset .. ($offset + $splice_length - 1);
342 # Adjust array length, and shift elements to accomodate new section.
344 if ( $new_size != $splice_length ) {
345 if ($new_size > $splice_length) {
346 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
347 $self->_move_value( $i, $i + ($new_size - $splice_length) );
349 $self->STORESIZE( $length + $new_size - $splice_length );
352 for (my $i = $offset + $splice_length; $i < $length; $i++) {
353 $self->_move_value( $i, $i + ($new_size - $splice_length) );
355 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
356 $self->DELETE( $length - 1 );
363 # Insert new elements into array
365 for (my $i = $offset; $i < $offset + $new_size; $i++) {
366 $self->STORE( $i, shift @new_elements );
372 # Return deleted section, or last element in scalar context.
374 return wantarray ? @old_elements : $old_elements[-1];
377 # We don't need to populate it, yet.
378 # It will be useful, though, when we split out HASH and ARRAY
381 # Perl will call EXTEND() when the array is likely to grow.
382 # We don't care, but include it because it gets called at times.
390 my $length = $self->length();
391 for (my $index = 0; $index < $length; $index++) {
392 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
399 # Public method aliases
401 sub length { (shift)->FETCHSIZE(@_) }
402 sub pop { (shift)->POP(@_) }
403 sub push { (shift)->PUSH(@_) }
404 sub unshift { (shift)->UNSHIFT(@_) }
405 sub splice { (shift)->SPLICE(@_) }
407 # This must be last otherwise we have to qualify all other calls to shift
408 # as calls to CORE::shift
409 sub shift { (CORE::shift)->SHIFT(@_) }