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 # my $orig_key = $key eq 'length' ? undef : $key;
53 if ( $key =~ /^-?\d+$/ ) {
55 $key += $self->FETCHSIZE;
56 unless ( $key >= 0 ) {
62 $key = pack($self->{engine}{long_pack}, $key);
65 my $rv = $self->SUPER::FETCH( $key, $orig_key );
73 my $self = shift->_get_self;
74 my ($key, $value) = @_;
76 $self->lock( $self->LOCK_EX );
78 # my $orig = $key eq 'length' ? undef : $key;
83 if ( $key =~ /^\-?\d+$/ ) {
86 $size = $self->FETCHSIZE;
89 die( "Modification of non-creatable array value attempted, subscript $orig_key" );
93 $key = pack($self->{engine}{long_pack}, $key);
96 my $rv = $self->SUPER::STORE( $key, $value, $orig_key );
98 if ( $numeric_idx && $rv == 2 ) {
99 $size = $self->FETCHSIZE unless defined $size;
100 if ( $orig_key >= $size ) {
101 $self->STORESIZE( $orig_key + 1 );
111 my $self = shift->_get_self;
114 $self->lock( $self->LOCK_SH );
116 if ( $key =~ /^\-?\d+$/ ) {
118 $key += $self->FETCHSIZE;
119 unless ( $key >= 0 ) {
125 $key = pack($self->{engine}{long_pack}, $key);
128 my $rv = $self->SUPER::EXISTS( $key );
136 my $self = shift->_get_self;
139 my $unpacked_key = $key;
140 my $orig = $key eq 'length' ? undef : $key;
142 $self->lock( $self->LOCK_EX );
144 my $size = $self->FETCHSIZE;
145 if ( $key =~ /^-?\d+$/ ) {
148 unless ( $key >= 0 ) {
154 $key = pack($self->{engine}{long_pack}, $key);
157 my $rv = $self->SUPER::DELETE( $key, $orig );
159 if ($rv && $unpacked_key == $size - 1) {
160 $self->STORESIZE( $unpacked_key );
169 my $self = shift->_get_self;
171 $self->lock( $self->LOCK_SH );
173 my $SAVE_FILTER = $self->_fileobj->{filter_fetch_value};
174 $self->_fileobj->{filter_fetch_value} = undef;
176 my $packed_size = $self->FETCH('length');
178 $self->_fileobj->{filter_fetch_value} = $SAVE_FILTER;
183 return int(unpack($self->{engine}{long_pack}, $packed_size));
190 my $self = shift->_get_self;
191 my ($new_length) = @_;
193 $self->lock( $self->LOCK_EX );
195 my $SAVE_FILTER = $self->_fileobj->{filter_store_value};
196 $self->_fileobj->{filter_store_value} = undef;
198 my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length), 'length');
200 $self->_fileobj->{filter_store_value} = $SAVE_FILTER;
208 my $self = shift->_get_self;
210 $self->lock( $self->LOCK_EX );
212 my $length = $self->FETCHSIZE();
215 my $content = $self->FETCH( $length - 1 );
216 $self->DELETE( $length - 1 );
229 my $self = shift->_get_self;
231 $self->lock( $self->LOCK_EX );
233 my $length = $self->FETCHSIZE();
235 while (my $content = shift @_) {
236 $self->STORE( $length, $content );
246 my $self = shift->_get_self;
248 $self->lock( $self->LOCK_EX );
250 my $length = $self->FETCHSIZE();
253 my $content = $self->FETCH( 0 );
255 for (my $i = 0; $i < $length - 1; $i++) {
256 $self->STORE( $i, $self->FETCH($i + 1) );
258 $self->DELETE( $length - 1 );
271 my $self = shift->_get_self;
272 my @new_elements = @_;
274 $self->lock( $self->LOCK_EX );
276 my $length = $self->FETCHSIZE();
277 my $new_size = scalar @new_elements;
280 for (my $i = $length - 1; $i >= 0; $i--) {
281 $self->STORE( $i + $new_size, $self->FETCH($i) );
285 for (my $i = 0; $i < $new_size; $i++) {
286 $self->STORE( $i, $new_elements[$i] );
291 return $length + $new_size;
295 my $self = shift->_get_self;
297 $self->lock( $self->LOCK_EX );
299 my $length = $self->FETCHSIZE();
302 # Calculate offset and length of splice
305 $offset = 0 unless defined $offset;
306 if ($offset < 0) { $offset += $length; }
309 if (scalar @_) { $splice_length = shift; }
310 else { $splice_length = $length - $offset; }
311 if ($splice_length < 0) { $splice_length += ($length - $offset); }
314 # Setup array with new elements, and copy out old elements for return
316 my @new_elements = @_;
317 my $new_size = scalar @new_elements;
319 my @old_elements = map {
321 } $offset .. ($offset + $splice_length - 1);
324 # Adjust array length, and shift elements to accomodate new section.
326 if ( $new_size != $splice_length ) {
327 if ($new_size > $splice_length) {
328 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
329 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
333 for (my $i = $offset + $splice_length; $i < $length; $i++) {
334 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
336 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
337 $self->DELETE( $length - 1 );
344 # Insert new elements into array
346 for (my $i = $offset; $i < $offset + $new_size; $i++) {
347 $self->STORE( $i, shift @new_elements );
353 # Return deleted section, or last element in scalar context.
355 return wantarray ? @old_elements : $old_elements[-1];
358 # We don't need to define it, yet.
359 # It will be useful, though, when we split out HASH and ARRAY
362 # Perl will call EXTEND() when the array is likely to grow.
363 # We don't care, but include it because it gets called at times.
371 my $length = $self->length();
372 for (my $index = 0; $index < $length; $index++) {
373 my $value = $self->get($index);
374 $self->_copy_value( \$db_temp->[$index], $value );
381 # Public method aliases
383 sub length { (shift)->FETCHSIZE(@_) }
384 sub pop { (shift)->POP(@_) }
385 sub push { (shift)->PUSH(@_) }
386 sub unshift { (shift)->UNSHIFT(@_) }
387 sub splice { (shift)->SPLICE(@_) }
389 # This must be last otherwise we have to qualify all other calls to shift
390 # as calls to CORE::shift
391 sub shift { (CORE::shift)->SHIFT(@_) }