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 );
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 );
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;
138 $self->lock( $self->LOCK_EX );
140 my $size = $self->FETCHSIZE;
141 if ( $key =~ /^-?\d+$/ ) {
144 unless ( $key >= 0 ) {
150 $key = pack($self->{engine}{long_pack}, $key);
153 my $rv = $self->SUPER::DELETE( $key );
155 if ($rv && $unpacked_key == $size - 1) {
156 $self->STORESIZE( $unpacked_key );
165 my $self = shift->_get_self;
167 $self->lock( $self->LOCK_SH );
169 my $SAVE_FILTER = $self->_fileobj->{filter_fetch_value};
170 $self->_fileobj->{filter_fetch_value} = undef;
172 my $packed_size = $self->FETCH('length');
174 $self->_fileobj->{filter_fetch_value} = $SAVE_FILTER;
179 return int(unpack($self->{engine}{long_pack}, $packed_size));
186 my $self = shift->_get_self;
187 my ($new_length) = @_;
189 $self->lock( $self->LOCK_EX );
191 my $SAVE_FILTER = $self->_fileobj->{filter_store_value};
192 $self->_fileobj->{filter_store_value} = undef;
194 my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length));
196 $self->_fileobj->{filter_store_value} = $SAVE_FILTER;
204 my $self = shift->_get_self;
206 $self->lock( $self->LOCK_EX );
208 my $length = $self->FETCHSIZE();
211 my $content = $self->FETCH( $length - 1 );
212 $self->DELETE( $length - 1 );
225 my $self = shift->_get_self;
227 $self->lock( $self->LOCK_EX );
229 my $length = $self->FETCHSIZE();
231 while (my $content = shift @_) {
232 $self->STORE( $length, $content );
242 my $self = shift->_get_self;
244 $self->lock( $self->LOCK_EX );
246 my $length = $self->FETCHSIZE();
249 my $content = $self->FETCH( 0 );
251 for (my $i = 0; $i < $length - 1; $i++) {
252 $self->STORE( $i, $self->FETCH($i + 1) );
254 $self->DELETE( $length - 1 );
267 my $self = shift->_get_self;
268 my @new_elements = @_;
270 $self->lock( $self->LOCK_EX );
272 my $length = $self->FETCHSIZE();
273 my $new_size = scalar @new_elements;
276 for (my $i = $length - 1; $i >= 0; $i--) {
277 $self->STORE( $i + $new_size, $self->FETCH($i) );
281 for (my $i = 0; $i < $new_size; $i++) {
282 $self->STORE( $i, $new_elements[$i] );
287 return $length + $new_size;
291 my $self = shift->_get_self;
293 $self->lock( $self->LOCK_EX );
295 my $length = $self->FETCHSIZE();
298 # Calculate offset and length of splice
301 $offset = 0 unless defined $offset;
302 if ($offset < 0) { $offset += $length; }
305 if (scalar @_) { $splice_length = shift; }
306 else { $splice_length = $length - $offset; }
307 if ($splice_length < 0) { $splice_length += ($length - $offset); }
310 # Setup array with new elements, and copy out old elements for return
312 my @new_elements = @_;
313 my $new_size = scalar @new_elements;
315 my @old_elements = map {
317 } $offset .. ($offset + $splice_length - 1);
320 # Adjust array length, and shift elements to accomodate new section.
322 if ( $new_size != $splice_length ) {
323 if ($new_size > $splice_length) {
324 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
325 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
329 for (my $i = $offset + $splice_length; $i < $length; $i++) {
330 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
332 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
333 $self->DELETE( $length - 1 );
340 # Insert new elements into array
342 for (my $i = $offset; $i < $offset + $new_size; $i++) {
343 $self->STORE( $i, shift @new_elements );
349 # Return deleted section, or last element in scalar context.
351 return wantarray ? @old_elements : $old_elements[-1];
354 # We don't need to define it, yet.
355 # It will be useful, though, when we split out HASH and ARRAY
358 # Perl will call EXTEND() when the array is likely to grow.
359 # We don't care, but include it because it gets called at times.
367 my $length = $self->length();
368 for (my $index = 0; $index < $length; $index++) {
369 my $value = $self->get($index);
370 $self->_copy_value( \$db_temp->[$index], $value );
377 # Public method aliases
379 sub length { (shift)->FETCHSIZE(@_) }
380 sub pop { (shift)->POP(@_) }
381 sub push { (shift)->PUSH(@_) }
382 sub unshift { (shift)->UNSHIFT(@_) }
383 sub splice { (shift)->SPLICE(@_) }
385 # This must be last otherwise we have to qualify all other calls to shift
386 # as calls to CORE::shift
387 sub shift { (CORE::shift)->SHIFT(@_) }