1 package DBM::Deep::Array;
8 our $VERSION = '0.99_01';
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]
23 sub _repr { shift;[ @_ ] }
30 local $SIG{'__DIE__'};
31 $self->push( @$struct );
33 $self->_throw_error("Cannot import: type mismatch");
40 my $args = $class->_get_args( @_ );
42 $args->{type} = $class->TYPE_ARRAY;
44 return $class->_init($args);
48 my $self = shift->_get_self;
51 $self->lock( $self->LOCK_SH );
53 # my $orig_key = $key eq 'length' ? undef : $key;
55 if ( $key =~ /^-?\d+$/ ) {
57 $key += $self->FETCHSIZE;
58 unless ( $key >= 0 ) {
64 $key = pack($self->_engine->{long_pack}, $key);
67 my $rv = $self->SUPER::FETCH( $key, $orig_key );
75 my $self = shift->_get_self;
76 my ($key, $value) = @_;
78 $self->lock( $self->LOCK_EX );
80 # my $orig = $key eq 'length' ? undef : $key;
85 if ( $key =~ /^\-?\d+$/ ) {
88 $size = $self->FETCHSIZE;
91 die( "Modification of non-creatable array value attempted, subscript $orig_key" );
95 $key = pack($self->_engine->{long_pack}, $key);
98 my $rv = $self->SUPER::STORE( $key, $value, $orig_key );
100 if ( $numeric_idx ) {
101 $size = $self->FETCHSIZE unless defined $size;
102 if ( $orig_key >= $size ) {
103 $self->STORESIZE( $orig_key + 1 );
113 my $self = shift->_get_self;
116 $self->lock( $self->LOCK_SH );
118 if ( $key =~ /^\-?\d+$/ ) {
120 $key += $self->FETCHSIZE;
121 unless ( $key >= 0 ) {
127 $key = pack($self->_engine->{long_pack}, $key);
130 my $rv = $self->SUPER::EXISTS( $key );
138 my $self = shift->_get_self;
141 my $unpacked_key = $key;
142 my $orig = $key eq 'length' ? undef : $key;
144 $self->lock( $self->LOCK_EX );
146 my $size = $self->FETCHSIZE;
147 if ( $key =~ /^-?\d+$/ ) {
150 unless ( $key >= 0 ) {
156 $key = pack($self->_engine->{long_pack}, $key);
159 my $rv = $self->SUPER::DELETE( $key, $orig );
161 if ($rv && $unpacked_key == $size - 1) {
162 $self->STORESIZE( $unpacked_key );
171 my $self = shift->_get_self;
173 $self->lock( $self->LOCK_SH );
175 my $SAVE_FILTER = $self->_fileobj->{filter_fetch_value};
176 $self->_fileobj->{filter_fetch_value} = undef;
178 my $packed_size = $self->FETCH('length');
180 $self->_fileobj->{filter_fetch_value} = $SAVE_FILTER;
185 return int(unpack($self->_engine->{long_pack}, $packed_size));
192 my $self = shift->_get_self;
193 my ($new_length) = @_;
195 $self->lock( $self->LOCK_EX );
197 my $SAVE_FILTER = $self->_fileobj->{filter_store_value};
198 $self->_fileobj->{filter_store_value} = undef;
200 my $result = $self->STORE('length', pack($self->_engine->{long_pack}, $new_length), 'length');
202 $self->_fileobj->{filter_store_value} = $SAVE_FILTER;
210 my $self = shift->_get_self;
212 $self->lock( $self->LOCK_EX );
214 my $length = $self->FETCHSIZE();
217 my $content = $self->FETCH( $length - 1 );
218 $self->DELETE( $length - 1 );
231 my $self = shift->_get_self;
233 $self->lock( $self->LOCK_EX );
235 my $length = $self->FETCHSIZE();
237 while (my $content = shift @_) {
238 $self->STORE( $length, $content );
248 my $self = shift->_get_self;
250 $self->lock( $self->LOCK_EX );
252 my $length = $self->FETCHSIZE();
255 my $content = $self->FETCH( 0 );
257 for (my $i = 0; $i < $length - 1; $i++) {
258 $self->STORE( $i, $self->FETCH($i + 1) );
260 $self->DELETE( $length - 1 );
273 my $self = shift->_get_self;
274 my @new_elements = @_;
276 $self->lock( $self->LOCK_EX );
278 my $length = $self->FETCHSIZE();
279 my $new_size = scalar @new_elements;
282 for (my $i = $length - 1; $i >= 0; $i--) {
283 $self->STORE( $i + $new_size, $self->FETCH($i) );
287 for (my $i = 0; $i < $new_size; $i++) {
288 $self->STORE( $i, $new_elements[$i] );
293 return $length + $new_size;
297 my $self = shift->_get_self;
299 $self->lock( $self->LOCK_EX );
301 my $length = $self->FETCHSIZE();
304 # Calculate offset and length of splice
307 $offset = 0 unless defined $offset;
308 if ($offset < 0) { $offset += $length; }
311 if (scalar @_) { $splice_length = shift; }
312 else { $splice_length = $length - $offset; }
313 if ($splice_length < 0) { $splice_length += ($length - $offset); }
316 # Setup array with new elements, and copy out old elements for return
318 my @new_elements = @_;
319 my $new_size = scalar @new_elements;
321 my @old_elements = map {
323 } $offset .. ($offset + $splice_length - 1);
326 # Adjust array length, and shift elements to accomodate new section.
328 if ( $new_size != $splice_length ) {
329 if ($new_size > $splice_length) {
330 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
331 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
335 for (my $i = $offset + $splice_length; $i < $length; $i++) {
336 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
338 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
339 $self->DELETE( $length - 1 );
346 # Insert new elements into array
348 for (my $i = $offset; $i < $offset + $new_size; $i++) {
349 $self->STORE( $i, shift @new_elements );
355 # Return deleted section, or last element in scalar context.
357 return wantarray ? @old_elements : $old_elements[-1];
360 # We don't need to define it, yet.
361 # It will be useful, though, when we split out HASH and ARRAY
364 # Perl will call EXTEND() when the array is likely to grow.
365 # We don't care, but include it because it gets called at times.
373 my $length = $self->length();
374 for (my $index = 0; $index < $length; $index++) {
375 my $value = $self->get($index);
376 $self->_copy_value( \$db_temp->[$index], $value );
383 # Public method aliases
385 sub length { (shift)->FETCHSIZE(@_) }
386 sub pop { (shift)->POP(@_) }
387 sub push { (shift)->PUSH(@_) }
388 sub unshift { (shift)->UNSHIFT(@_) }
389 sub splice { (shift)->SPLICE(@_) }
391 # This must be last otherwise we have to qualify all other calls to shift
392 # as calls to CORE::shift
393 sub shift { (CORE::shift)->SHIFT(@_) }