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;
52 if ( $key =~ /^-?\d+$/ ) {
54 $key += $self->FETCHSIZE;
55 unless ( $key >= 0 ) {
61 $key = pack($self->{engine}{long_pack}, $key);
64 my $rv = $self->SUPER::FETCH( $key, $orig_key );
72 my $self = shift->_get_self;
73 my ($key, $value) = @_;
75 $self->lock( $self->LOCK_EX );
77 my $orig = $key eq 'length' ? undef : $key;
81 if ( $key =~ /^\-?\d+$/ ) {
84 $size = $self->FETCHSIZE;
87 die( "Modification of non-creatable array value attempted, subscript $orig" );
91 $key = pack($self->{engine}{long_pack}, $key);
94 my $rv = $self->SUPER::STORE( $key, $value, $orig );
96 if ( $numeric_idx && $rv == 2 ) {
97 $size = $self->FETCHSIZE unless defined $size;
98 if ( $orig >= $size ) {
99 $self->STORESIZE( $orig + 1 );
109 my $self = shift->_get_self;
112 $self->lock( $self->LOCK_SH );
114 if ( $key =~ /^\-?\d+$/ ) {
116 $key += $self->FETCHSIZE;
117 unless ( $key >= 0 ) {
123 $key = pack($self->{engine}{long_pack}, $key);
126 my $rv = $self->SUPER::EXISTS( $key );
134 my $self = shift->_get_self;
137 my $unpacked_key = $key;
138 my $orig = $key eq 'length' ? undef : $key;
140 $self->lock( $self->LOCK_EX );
142 my $size = $self->FETCHSIZE;
143 if ( $key =~ /^-?\d+$/ ) {
146 unless ( $key >= 0 ) {
152 $key = pack($self->{engine}{long_pack}, $key);
155 my $rv = $self->SUPER::DELETE( $key, $orig );
157 if ($rv && $unpacked_key == $size - 1) {
158 $self->STORESIZE( $unpacked_key );
167 my $self = shift->_get_self;
169 $self->lock( $self->LOCK_SH );
171 my $SAVE_FILTER = $self->_fileobj->{filter_fetch_value};
172 $self->_fileobj->{filter_fetch_value} = undef;
174 my $packed_size = $self->FETCH('length');
176 $self->_fileobj->{filter_fetch_value} = $SAVE_FILTER;
181 return int(unpack($self->{engine}{long_pack}, $packed_size));
188 my $self = shift->_get_self;
189 my ($new_length) = @_;
191 $self->lock( $self->LOCK_EX );
193 my $SAVE_FILTER = $self->_fileobj->{filter_store_value};
194 $self->_fileobj->{filter_store_value} = undef;
196 my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length));
198 $self->_fileobj->{filter_store_value} = $SAVE_FILTER;
206 my $self = shift->_get_self;
208 $self->lock( $self->LOCK_EX );
210 my $length = $self->FETCHSIZE();
213 my $content = $self->FETCH( $length - 1 );
214 $self->DELETE( $length - 1 );
227 my $self = shift->_get_self;
229 $self->lock( $self->LOCK_EX );
231 my $length = $self->FETCHSIZE();
233 while (my $content = shift @_) {
234 $self->STORE( $length, $content );
244 my $self = shift->_get_self;
246 $self->lock( $self->LOCK_EX );
248 my $length = $self->FETCHSIZE();
251 my $content = $self->FETCH( 0 );
253 for (my $i = 0; $i < $length - 1; $i++) {
254 $self->STORE( $i, $self->FETCH($i + 1) );
256 $self->DELETE( $length - 1 );
269 my $self = shift->_get_self;
270 my @new_elements = @_;
272 $self->lock( $self->LOCK_EX );
274 my $length = $self->FETCHSIZE();
275 my $new_size = scalar @new_elements;
278 for (my $i = $length - 1; $i >= 0; $i--) {
279 $self->STORE( $i + $new_size, $self->FETCH($i) );
283 for (my $i = 0; $i < $new_size; $i++) {
284 $self->STORE( $i, $new_elements[$i] );
289 return $length + $new_size;
293 my $self = shift->_get_self;
295 $self->lock( $self->LOCK_EX );
297 my $length = $self->FETCHSIZE();
300 # Calculate offset and length of splice
303 $offset = 0 unless defined $offset;
304 if ($offset < 0) { $offset += $length; }
307 if (scalar @_) { $splice_length = shift; }
308 else { $splice_length = $length - $offset; }
309 if ($splice_length < 0) { $splice_length += ($length - $offset); }
312 # Setup array with new elements, and copy out old elements for return
314 my @new_elements = @_;
315 my $new_size = scalar @new_elements;
317 my @old_elements = map {
319 } $offset .. ($offset + $splice_length - 1);
322 # Adjust array length, and shift elements to accomodate new section.
324 if ( $new_size != $splice_length ) {
325 if ($new_size > $splice_length) {
326 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
327 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
331 for (my $i = $offset + $splice_length; $i < $length; $i++) {
332 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
334 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
335 $self->DELETE( $length - 1 );
342 # Insert new elements into array
344 for (my $i = $offset; $i < $offset + $new_size; $i++) {
345 $self->STORE( $i, shift @new_elements );
351 # Return deleted section, or last element in scalar context.
353 return wantarray ? @old_elements : $old_elements[-1];
356 # We don't need to define it, yet.
357 # It will be useful, though, when we split out HASH and ARRAY
360 # Perl will call EXTEND() when the array is likely to grow.
361 # We don't care, but include it because it gets called at times.
369 my $length = $self->length();
370 for (my $index = 0; $index < $length; $index++) {
371 my $value = $self->get($index);
372 $self->_copy_value( \$db_temp->[$index], $value );
379 # Public method aliases
381 sub length { (shift)->FETCHSIZE(@_) }
382 sub pop { (shift)->POP(@_) }
383 sub push { (shift)->PUSH(@_) }
384 sub unshift { (shift)->UNSHIFT(@_) }
385 sub splice { (shift)->SPLICE(@_) }
387 # This must be last otherwise we have to qualify all other calls to shift
388 # as calls to CORE::shift
389 sub shift { (CORE::shift)->SHIFT(@_) }