1 package DBM::Deep::Array;
8 our $VERSION = q(1.0010);
10 # This is to allow DBM::Deep::Array to handle negative indices on
11 # its own. Otherwise, Perl would intercept the call to negative
12 # indices for us. This was causing bugs for negative index handling.
13 our $NEGATIVE_INDICES = 1;
20 eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
27 my $args = $class->_get_args( @_ );
29 $args->{type} = $class->TYPE_ARRAY;
31 return $class->_init($args);
35 my $self = shift->_get_self;
38 $self->lock( $self->LOCK_SH );
40 if ( !defined $key ) {
42 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
44 elsif ( $key =~ /^-?\d+$/ ) {
46 $key += $self->FETCHSIZE;
47 unless ( $key >= 0 ) {
53 elsif ( $key ne 'length' ) {
55 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
58 my $rv = $self->SUPER::FETCH( $key );
66 my $self = shift->_get_self;
67 my ($key, $value) = @_;
69 $self->lock( $self->LOCK_EX );
73 if ( !defined $key ) {
75 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
77 elsif ( $key =~ /^-?\d+$/ ) {
80 $size = $self->FETCHSIZE;
81 if ( $key + $size < 0 ) {
82 die( "Modification of non-creatable array value attempted, subscript $key" );
87 elsif ( $key ne 'length' ) {
89 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
92 my $rv = $self->SUPER::STORE( $key, $value );
94 if ( $idx_is_numeric ) {
95 $size = $self->FETCHSIZE unless defined $size;
96 if ( $key >= $size ) {
97 $self->STORESIZE( $key + 1 );
107 my $self = shift->_get_self;
110 $self->lock( $self->LOCK_SH );
112 if ( !defined $key ) {
114 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
116 elsif ( $key =~ /^-?\d+$/ ) {
118 $key += $self->FETCHSIZE;
119 unless ( $key >= 0 ) {
125 elsif ( $key ne 'length' ) {
127 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
130 my $rv = $self->SUPER::EXISTS( $key );
138 my $self = shift->_get_self;
140 warn "ARRAY::DELETE($self,$key)\n" if DBM::Deep::DEBUG;
142 $self->lock( $self->LOCK_EX );
144 my $size = $self->FETCHSIZE;
145 if ( !defined $key ) {
147 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
149 elsif ( $key =~ /^-?\d+$/ ) {
152 unless ( $key >= 0 ) {
158 elsif ( $key ne 'length' ) {
160 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
163 my $rv = $self->SUPER::DELETE( $key );
165 if ($rv && $key == $size - 1) {
166 $self->STORESIZE( $key );
174 # Now that we have a real Reference sector, we should store arrayzize there. However,
175 # arraysize needs to be transactionally-aware, so a simple location to store it isn't
178 my $self = shift->_get_self;
180 $self->lock( $self->LOCK_SH );
182 my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
183 $self->_storage->{filter_fetch_value} = undef;
185 my $size = $self->FETCH('length') || 0;
187 $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
195 my $self = shift->_get_self;
196 my ($new_length) = @_;
198 $self->lock( $self->LOCK_EX );
200 my $SAVE_FILTER = $self->_storage->{filter_store_value};
201 $self->_storage->{filter_store_value} = undef;
203 my $result = $self->STORE('length', $new_length, 'length');
205 $self->_storage->{filter_store_value} = $SAVE_FILTER;
213 my $self = shift->_get_self;
215 $self->lock( $self->LOCK_EX );
217 my $length = $self->FETCHSIZE();
220 my $content = $self->FETCH( $length - 1 );
221 $self->DELETE( $length - 1 );
234 my $self = shift->_get_self;
236 $self->lock( $self->LOCK_EX );
238 my $length = $self->FETCHSIZE();
240 while (my $content = shift @_) {
241 $self->STORE( $length, $content );
250 # XXX This really needs to be something more direct within the file, not a
251 # fetch and re-store. -RobK, 2007-09-20
254 my ($old_key, $new_key) = @_;
256 return $self->_engine->make_reference( $self, $old_key, $new_key );
260 my $self = shift->_get_self;
261 warn "SHIFT($self)\n" if DBM::Deep::DEBUG;
263 $self->lock( $self->LOCK_EX );
265 my $length = $self->FETCHSIZE();
272 my $content = $self->FETCH( 0 );
274 for (my $i = 0; $i < $length - 1; $i++) {
275 $self->_move_value( $i+1, $i );
278 $self->DELETE( $length - 1 );
286 my $self = shift->_get_self;
287 my @new_elements = @_;
289 $self->lock( $self->LOCK_EX );
291 my $length = $self->FETCHSIZE();
292 my $new_size = scalar @new_elements;
295 for (my $i = $length - 1; $i >= 0; $i--) {
296 $self->_move_value( $i, $i+$new_size );
299 $self->STORESIZE( $length + $new_size );
302 for (my $i = 0; $i < $new_size; $i++) {
303 $self->STORE( $i, $new_elements[$i] );
308 return $length + $new_size;
312 my $self = shift->_get_self;
314 $self->lock( $self->LOCK_EX );
316 my $length = $self->FETCHSIZE();
319 # Calculate offset and length of splice
322 $offset = 0 unless defined $offset;
323 if ($offset < 0) { $offset += $length; }
326 if (scalar @_) { $splice_length = shift; }
327 else { $splice_length = $length - $offset; }
328 if ($splice_length < 0) { $splice_length += ($length - $offset); }
331 # Setup array with new elements, and copy out old elements for return
333 my @new_elements = @_;
334 my $new_size = scalar @new_elements;
336 my @old_elements = map {
338 } $offset .. ($offset + $splice_length - 1);
341 # Adjust array length, and shift elements to accomodate new section.
343 if ( $new_size != $splice_length ) {
344 if ($new_size > $splice_length) {
345 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
346 $self->_move_value( $i, $i + ($new_size - $splice_length) );
348 $self->STORESIZE( $length + $new_size - $splice_length );
351 for (my $i = $offset + $splice_length; $i < $length; $i++) {
352 $self->_move_value( $i, $i + ($new_size - $splice_length) );
354 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
355 $self->DELETE( $length - 1 );
362 # Insert new elements into array
364 for (my $i = $offset; $i < $offset + $new_size; $i++) {
365 $self->STORE( $i, shift @new_elements );
371 # Return deleted section, or last element in scalar context.
373 return wantarray ? @old_elements : $old_elements[-1];
376 # We don't need to populate it, yet.
377 # 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.
389 my $length = $self->length();
390 for (my $index = 0; $index < $length; $index++) {
391 my $value = $self->get($index);
392 $self->_copy_value( \$db_temp->[$index], $value );
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(@_) }