1 package DBM::Deep::Array;
8 our $VERSION = '0.99_04';
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;[ @_ ] }
29 $self->push( @$struct );
36 my $args = $class->_get_args( @_ );
38 $args->{type} = $class->TYPE_ARRAY;
40 return $class->_init($args);
44 my $self = shift->_get_self;
47 $self->lock( $self->LOCK_SH );
49 if ( !defined $key ) {
50 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
52 elsif ( $key =~ /^-?\d+$/ ) {
54 $key += $self->FETCHSIZE;
55 unless ( $key >= 0 ) {
61 elsif ( $key ne 'length' ) {
63 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
66 my $rv = $self->SUPER::FETCH( $key );
74 my $self = shift->_get_self;
75 my ($key, $value) = @_;
77 $self->lock( $self->LOCK_EX );
81 if ( !defined $key ) {
82 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
84 elsif ( $key =~ /^-?\d+$/ ) {
87 $size = $self->FETCHSIZE;
88 if ( $key + $size < 0 ) {
89 die( "Modification of non-creatable array value attempted, subscript $key" );
94 elsif ( $key ne 'length' ) {
96 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
99 my $rv = $self->SUPER::STORE( $key, $value );
101 if ( $idx_is_numeric ) {
102 $size = $self->FETCHSIZE unless defined $size;
103 if ( $key >= $size ) {
104 $self->STORESIZE( $key + 1 );
114 my $self = shift->_get_self;
117 $self->lock( $self->LOCK_SH );
119 if ( !defined $key ) {
120 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
122 elsif ( $key =~ /^-?\d+$/ ) {
124 $key += $self->FETCHSIZE;
125 unless ( $key >= 0 ) {
131 elsif ( $key ne 'length' ) {
133 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
136 my $rv = $self->SUPER::EXISTS( $key );
144 my $self = shift->_get_self;
147 $self->lock( $self->LOCK_EX );
149 my $size = $self->FETCHSIZE;
150 if ( !defined $key ) {
151 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
153 elsif ( $key =~ /^-?\d+$/ ) {
156 unless ( $key >= 0 ) {
162 elsif ( $key ne 'length' ) {
164 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
167 my $rv = $self->SUPER::DELETE( $key );
169 if ($rv && $key == $size - 1) {
170 $self->STORESIZE( $key );
178 # Now that we have a real Reference sector, we should store arrayzize there. However,
179 # arraysize needs to be transactionally-aware, so a simple location to store it isn't
182 my $self = shift->_get_self;
184 $self->lock( $self->LOCK_SH );
186 my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
187 $self->_storage->{filter_fetch_value} = undef;
189 my $size = $self->FETCH('length') || 0;
191 $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
199 my $self = shift->_get_self;
200 my ($new_length) = @_;
202 $self->lock( $self->LOCK_EX );
204 my $SAVE_FILTER = $self->_storage->{filter_store_value};
205 $self->_storage->{filter_store_value} = undef;
207 my $result = $self->STORE('length', $new_length, 'length');
209 $self->_storage->{filter_store_value} = $SAVE_FILTER;
217 my $self = shift->_get_self;
219 $self->lock( $self->LOCK_EX );
221 my $length = $self->FETCHSIZE();
224 my $content = $self->FETCH( $length - 1 );
225 $self->DELETE( $length - 1 );
238 my $self = shift->_get_self;
240 $self->lock( $self->LOCK_EX );
242 my $length = $self->FETCHSIZE();
244 while (my $content = shift @_) {
245 $self->STORE( $length, $content );
255 my $self = shift->_get_self;
257 $self->lock( $self->LOCK_EX );
259 my $length = $self->FETCHSIZE();
262 my $content = $self->FETCH( 0 );
264 for (my $i = 0; $i < $length - 1; $i++) {
265 $self->STORE( $i, $self->FETCH($i + 1) );
267 $self->DELETE( $length - 1 );
280 my $self = shift->_get_self;
281 my @new_elements = @_;
283 $self->lock( $self->LOCK_EX );
285 my $length = $self->FETCHSIZE();
286 my $new_size = scalar @new_elements;
289 for (my $i = $length - 1; $i >= 0; $i--) {
290 $self->STORE( $i + $new_size, $self->FETCH($i) );
294 for (my $i = 0; $i < $new_size; $i++) {
295 $self->STORE( $i, $new_elements[$i] );
300 return $length + $new_size;
304 my $self = shift->_get_self;
306 $self->lock( $self->LOCK_EX );
308 my $length = $self->FETCHSIZE();
311 # Calculate offset and length of splice
314 $offset = 0 unless defined $offset;
315 if ($offset < 0) { $offset += $length; }
318 if (scalar @_) { $splice_length = shift; }
319 else { $splice_length = $length - $offset; }
320 if ($splice_length < 0) { $splice_length += ($length - $offset); }
323 # Setup array with new elements, and copy out old elements for return
325 my @new_elements = @_;
326 my $new_size = scalar @new_elements;
328 my @old_elements = map {
330 } $offset .. ($offset + $splice_length - 1);
333 # Adjust array length, and shift elements to accomodate new section.
335 if ( $new_size != $splice_length ) {
336 if ($new_size > $splice_length) {
337 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
338 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
342 for (my $i = $offset + $splice_length; $i < $length; $i++) {
343 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
345 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
346 $self->DELETE( $length - 1 );
353 # Insert new elements into array
355 for (my $i = $offset; $i < $offset + $new_size; $i++) {
356 $self->STORE( $i, shift @new_elements );
362 # Return deleted section, or last element in scalar context.
364 return wantarray ? @old_elements : $old_elements[-1];
367 # We don't need to populate it, yet.
368 # It will be useful, though, when we split out HASH and ARRAY
371 # Perl will call EXTEND() when the array is likely to grow.
372 # We don't care, but include it because it gets called at times.
380 my $length = $self->length();
381 for (my $index = 0; $index < $length; $index++) {
382 my $value = $self->get($index);
383 $self->_copy_value( \$db_temp->[$index], $value );
390 # Public method aliases
392 sub length { (shift)->FETCHSIZE(@_) }
393 sub pop { (shift)->POP(@_) }
394 sub push { (shift)->PUSH(@_) }
395 sub unshift { (shift)->UNSHIFT(@_) }
396 sub splice { (shift)->SPLICE(@_) }
398 # This must be last otherwise we have to qualify all other calls to shift
399 # as calls to CORE::shift
400 sub shift { (CORE::shift)->SHIFT(@_) }