1 package DBM::Deep::Array;
6 use warnings FATAL => 'all';
7 no warnings 'recursion';
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;
19 eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
26 my $args = $class->_get_args( @_ );
28 $args->{type} = $class->TYPE_ARRAY;
30 return $class->_init($args);
34 my $self = shift->_get_self;
39 if ( !defined $key ) {
41 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
43 elsif ( $key =~ /^-?\d+$/ ) {
45 $key += $self->FETCHSIZE;
46 unless ( $key >= 0 ) {
52 elsif ( $key ne 'length' ) {
54 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
57 my $rv = $self->SUPER::FETCH( $key );
65 my $self = shift->_get_self;
66 my ($key, $value) = @_;
68 $self->lock_exclusive;
72 if ( !defined $key ) {
74 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
76 elsif ( $key =~ /^-?\d+$/ ) {
79 $size = $self->FETCHSIZE;
80 if ( $key + $size < 0 ) {
81 die( "Modification of non-creatable array value attempted, subscript $key" );
86 elsif ( $key ne 'length' ) {
88 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
91 my $rv = $self->SUPER::STORE( $key, $value );
93 if ( $idx_is_numeric ) {
94 $size = $self->FETCHSIZE unless defined $size;
95 if ( $key >= $size ) {
96 $self->STORESIZE( $key + 1 );
106 my $self = shift->_get_self;
111 if ( !defined $key ) {
113 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
115 elsif ( $key =~ /^-?\d+$/ ) {
117 $key += $self->FETCHSIZE;
118 unless ( $key >= 0 ) {
124 elsif ( $key ne 'length' ) {
126 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
129 my $rv = $self->SUPER::EXISTS( $key );
137 my $self = shift->_get_self;
139 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
141 $self->lock_exclusive;
143 my $size = $self->FETCHSIZE;
144 if ( !defined $key ) {
146 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
148 elsif ( $key =~ /^-?\d+$/ ) {
151 unless ( $key >= 0 ) {
157 elsif ( $key ne 'length' ) {
159 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
162 my $rv = $self->SUPER::DELETE( $key );
164 if ($rv && $key == $size - 1) {
165 $self->STORESIZE( $key );
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.
177 my $self = shift->_get_self;
181 my $SAVE_FILTER = $self->_engine->storage->{filter_fetch_value};
182 $self->_engine->storage->{filter_fetch_value} = undef;
184 my $size = $self->FETCH('length') || 0;
186 $self->_engine->storage->{filter_fetch_value} = $SAVE_FILTER;
194 my $self = shift->_get_self;
195 my ($new_length) = @_;
197 $self->lock_exclusive;
199 my $SAVE_FILTER = $self->_engine->storage->{filter_store_value};
200 $self->_engine->storage->{filter_store_value} = undef;
202 my $result = $self->STORE('length', $new_length, 'length');
204 $self->_engine->storage->{filter_store_value} = $SAVE_FILTER;
212 my $self = shift->_get_self;
214 $self->lock_exclusive;
216 my $length = $self->FETCHSIZE();
219 my $content = $self->FETCH( $length - 1 );
220 $self->DELETE( $length - 1 );
233 my $self = shift->_get_self;
235 $self->lock_exclusive;
237 my $length = $self->FETCHSIZE();
239 while (my $content = shift @_) {
240 $self->STORE( $length, $content );
249 # XXX This really needs to be something more direct within the file, not a
250 # fetch and re-store. -RobK, 2007-09-20
253 my ($old_key, $new_key) = @_;
255 return $self->_engine->make_reference( $self, $old_key, $new_key );
259 my $self = shift->_get_self;
260 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
262 $self->lock_exclusive;
264 my $length = $self->FETCHSIZE();
271 my $content = $self->DELETE( 0 );
273 # Unless the deletion above has cleared the array ...
275 for (my $i = 0; $i < $length - 1; $i++) {
276 $self->_move_value( $i+1, $i );
279 $self->DELETE( $length - 1 );
288 my $self = shift->_get_self;
289 my @new_elements = @_;
291 $self->lock_exclusive;
293 my $length = $self->FETCHSIZE();
294 my $new_size = scalar @new_elements;
297 for (my $i = $length - 1; $i >= 0; $i--) {
298 $self->_move_value( $i, $i+$new_size );
301 $self->STORESIZE( $length + $new_size );
304 for (my $i = 0; $i < $new_size; $i++) {
305 $self->STORE( $i, $new_elements[$i] );
310 return $length + $new_size;
314 my $self = shift->_get_self;
316 $self->lock_exclusive;
318 my $length = $self->FETCHSIZE();
321 # Calculate offset and length of splice
324 $offset = 0 unless defined $offset;
325 if ($offset < 0) { $offset += $length; }
328 if (scalar @_) { $splice_length = shift; }
329 else { $splice_length = $length - $offset; }
330 if ($splice_length < 0) { $splice_length += ($length - $offset); }
333 # Setup array with new elements, and copy out old elements for return
335 my @new_elements = @_;
336 my $new_size = scalar @new_elements;
338 my @old_elements = map {
340 } $offset .. ($offset + $splice_length - 1);
343 # Adjust array length, and shift elements to accomodate new section.
345 if ( $new_size != $splice_length ) {
346 if ($new_size > $splice_length) {
347 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
348 $self->_move_value( $i, $i + ($new_size - $splice_length) );
350 $self->STORESIZE( $length + $new_size - $splice_length );
353 for (my $i = $offset + $splice_length; $i < $length; $i++) {
354 $self->_move_value( $i, $i + ($new_size - $splice_length) );
356 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
357 $self->DELETE( $length - 1 );
364 # Insert new elements into array
366 for (my $i = $offset; $i < $offset + $new_size; $i++) {
367 $self->STORE( $i, shift @new_elements );
373 # Return deleted section, or last element in scalar context.
375 return wantarray ? @old_elements : $old_elements[-1];
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.
388 my $length = $self->length();
389 for (my $index = 0; $index < $length; $index++) {
390 $self->_copy_value( \$db_temp->[$index], $self->get($index) );
399 my $size = $self->FETCHSIZE;
400 for my $key ( 0 .. $size - 1 ) {
401 $self->_engine->delete_key( $self, $key, $key );
403 $self->STORESIZE( 0 );
408 sub length { (shift)->FETCHSIZE(@_) }
409 sub pop { (shift)->POP(@_) }
410 sub push { (shift)->PUSH(@_) }
411 sub unshift { (shift)->UNSHIFT(@_) }
412 sub splice { (shift)->SPLICE(@_) }
414 # This must be last otherwise we have to qualify all other calls to shift
415 # as calls to CORE::shift
416 sub shift { (CORE::shift)->SHIFT(@_) }