1 package DBM::Deep::Array;
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]
21 sub _repr { shift;[ @_ ] }
28 local $SIG{'__DIE__'};
29 $self->push( @$struct );
31 $self->_throw_error("Cannot import: type mismatch");
38 my $args = $class->_get_args( @_ );
40 $args->{type} = $class->TYPE_ARRAY;
42 return $class->_init($args);
46 my $self = shift->_get_self;
49 $self->lock( $self->LOCK_SH );
51 if ( $key =~ /^-?\d+$/ ) {
53 $key += $self->FETCHSIZE;
54 unless ( $key >= 0 ) {
60 $key = pack($self->{engine}{long_pack}, $key);
63 my $rv = $self->SUPER::FETCH( $key );
71 my $self = shift->_get_self;
72 my ($key, $value) = @_;
74 $self->lock( $self->LOCK_EX );
76 my $orig = $key eq 'length' ? undef : $key;
80 if ( $key =~ /^\-?\d+$/ ) {
83 $size = $self->FETCHSIZE;
86 die( "Modification of non-creatable array value attempted, subscript $orig" );
90 $key = pack($self->{engine}{long_pack}, $key);
93 my $rv = $self->SUPER::STORE( $key, $value, $orig );
95 if ( $numeric_idx && $rv == 2 ) {
96 $size = $self->FETCHSIZE unless defined $size;
97 if ( $orig >= $size ) {
98 $self->STORESIZE( $orig + 1 );
108 my $self = shift->_get_self;
111 $self->lock( $self->LOCK_SH );
113 if ( $key =~ /^\-?\d+$/ ) {
115 $key += $self->FETCHSIZE;
116 unless ( $key >= 0 ) {
122 $key = pack($self->{engine}{long_pack}, $key);
125 my $rv = $self->SUPER::EXISTS( $key );
133 my $self = shift->_get_self;
136 my $unpacked_key = $key;
137 my $orig = $key eq 'length' ? undef : $key;
139 $self->lock( $self->LOCK_EX );
141 my $size = $self->FETCHSIZE;
142 if ( $key =~ /^-?\d+$/ ) {
145 unless ( $key >= 0 ) {
151 $key = pack($self->{engine}{long_pack}, $key);
154 my $rv = $self->SUPER::DELETE( $key, $orig );
156 if ($rv && $unpacked_key == $size - 1) {
157 $self->STORESIZE( $unpacked_key );
166 my $self = shift->_get_self;
168 $self->lock( $self->LOCK_SH );
170 my $SAVE_FILTER = $self->_fileobj->{filter_fetch_value};
171 $self->_fileobj->{filter_fetch_value} = undef;
173 my $packed_size = $self->FETCH('length');
175 $self->_fileobj->{filter_fetch_value} = $SAVE_FILTER;
180 return int(unpack($self->{engine}{long_pack}, $packed_size));
187 my $self = shift->_get_self;
188 my ($new_length) = @_;
190 $self->lock( $self->LOCK_EX );
192 my $SAVE_FILTER = $self->_fileobj->{filter_store_value};
193 $self->_fileobj->{filter_store_value} = undef;
195 my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length));
197 $self->_fileobj->{filter_store_value} = $SAVE_FILTER;
205 my $self = shift->_get_self;
207 $self->lock( $self->LOCK_EX );
209 my $length = $self->FETCHSIZE();
212 my $content = $self->FETCH( $length - 1 );
213 $self->DELETE( $length - 1 );
226 my $self = shift->_get_self;
228 $self->lock( $self->LOCK_EX );
230 my $length = $self->FETCHSIZE();
232 while (my $content = shift @_) {
233 $self->STORE( $length, $content );
243 my $self = shift->_get_self;
245 $self->lock( $self->LOCK_EX );
247 my $length = $self->FETCHSIZE();
250 my $content = $self->FETCH( 0 );
252 for (my $i = 0; $i < $length - 1; $i++) {
253 $self->STORE( $i, $self->FETCH($i + 1) );
255 $self->DELETE( $length - 1 );
268 my $self = shift->_get_self;
269 my @new_elements = @_;
271 $self->lock( $self->LOCK_EX );
273 my $length = $self->FETCHSIZE();
274 my $new_size = scalar @new_elements;
277 for (my $i = $length - 1; $i >= 0; $i--) {
278 $self->STORE( $i + $new_size, $self->FETCH($i) );
282 for (my $i = 0; $i < $new_size; $i++) {
283 $self->STORE( $i, $new_elements[$i] );
288 return $length + $new_size;
292 my $self = shift->_get_self;
294 $self->lock( $self->LOCK_EX );
296 my $length = $self->FETCHSIZE();
299 # Calculate offset and length of splice
302 $offset = 0 unless defined $offset;
303 if ($offset < 0) { $offset += $length; }
306 if (scalar @_) { $splice_length = shift; }
307 else { $splice_length = $length - $offset; }
308 if ($splice_length < 0) { $splice_length += ($length - $offset); }
311 # Setup array with new elements, and copy out old elements for return
313 my @new_elements = @_;
314 my $new_size = scalar @new_elements;
316 my @old_elements = map {
318 } $offset .. ($offset + $splice_length - 1);
321 # Adjust array length, and shift elements to accomodate new section.
323 if ( $new_size != $splice_length ) {
324 if ($new_size > $splice_length) {
325 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
326 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
330 for (my $i = $offset + $splice_length; $i < $length; $i++) {
331 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
333 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
334 $self->DELETE( $length - 1 );
341 # Insert new elements into array
343 for (my $i = $offset; $i < $offset + $new_size; $i++) {
344 $self->STORE( $i, shift @new_elements );
350 # Return deleted section, or last element in scalar context.
352 return wantarray ? @old_elements : $old_elements[-1];
355 # We don't need to define it, yet.
356 # It will be useful, though, when we split out HASH and ARRAY
359 # Perl will call EXTEND() when the array is likely to grow.
360 # We don't care, but include it because it gets called at times.
368 my $length = $self->length();
369 for (my $index = 0; $index < $length; $index++) {
370 my $value = $self->get($index);
371 $self->_copy_value( \$db_temp->[$index], $value );
378 # Public method aliases
380 sub length { (shift)->FETCHSIZE(@_) }
381 sub pop { (shift)->POP(@_) }
382 sub push { (shift)->PUSH(@_) }
383 sub unshift { (shift)->UNSHIFT(@_) }
384 sub splice { (shift)->SPLICE(@_) }
386 # This must be last otherwise we have to qualify all other calls to shift
387 # as calls to CORE::shift
388 sub shift { (CORE::shift)->SHIFT(@_) }