my $self = $_[0]->_get_self;
my $key = $_[1];
+ $self->lock( $self->LOCK_SH );
+
if ( $key =~ /^-?\d+$/ ) {
if ( $key < 0 ) {
$key += $self->FETCHSIZE;
- return unless $key >= 0;
+ unless ( $key >= 0 ) {
+ $self->unlock;
+ return;
+ }
}
$key = pack($DBM::Deep::LONG_PACK, $key);
}
- return $self->SUPER::FETCH( $key );
+ my $rv = $self->SUPER::FETCH( $key );
+
+ $self->unlock;
+
+ return $rv;
}
sub STORE {
my $self = shift->_get_self;
my ($key, $value) = @_;
+ $self->lock( $self->LOCK_EX );
+
my $orig = $key;
my $size = $self->FETCHSIZE;
$self->STORESIZE( $orig + 1 );
}
+ $self->unlock;
+
return $rv;
}
my $self = $_[0]->_get_self;
my $key = $_[1];
+ $self->lock( $self->LOCK_SH );
+
if ( $key =~ /^-?\d+$/ ) {
if ( $key < 0 ) {
$key += $self->FETCHSIZE;
- return unless $key >= 0;
+ unless ( $key >= 0 ) {
+ $self->unlock;
+ return;
+ }
+ }
+
+ $key = pack($DBM::Deep::LONG_PACK, $key);
+ }
+
+ my $rv = $self->SUPER::EXISTS( $key );
+
+ $self->unlock;
+
+ return $rv;
+}
+
+sub DELETE {
+ my $self = $_[0]->_get_self;
+ my $key = $_[1];
+
+ my $unpacked_key = $key;
+
+ $self->lock( $self->LOCK_EX );
+
+ my $size = $self->FETCHSIZE;
+ if ( $key =~ /^-?\d+$/ ) {
+ if ( $key < 0 ) {
+ $key += $size;
+ unless ( $key >= 0 ) {
+ $self->unlock;
+ return;
+ }
}
$key = pack($DBM::Deep::LONG_PACK, $key);
}
- return $self->SUPER::EXISTS( $key );
+ my $rv = $self->SUPER::DELETE( $key );
+
+ if ($rv && $unpacked_key == $size - 1) {
+ $self->STORESIZE( $unpacked_key );
+ }
+
+ $self->unlock;
+
+ return $rv;
}
sub FETCHSIZE {
##
# Return the length of the array
##
- my $self = $_[0]->_get_self;
-
+ my $self = shift->_get_self;
+
+ $self->lock( $self->LOCK_SH );
+
my $SAVE_FILTER = $self->root->{filter_fetch_value};
$self->root->{filter_fetch_value} = undef;
$self->root->{filter_fetch_value} = $SAVE_FILTER;
+ $self->unlock;
+
if ($packed_size) {
return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
}
my $self = $_[0]->_get_self;
my $new_length = $_[1];
+ $self->lock( $self->LOCK_EX );
+
my $SAVE_FILTER = $self->root->{filter_store_value};
$self->root->{filter_store_value} = undef;
$self->root->{filter_store_value} = $SAVE_FILTER;
+ $self->unlock;
+
return $result;
}
# Remove and return the last element on the array
##
my $self = $_[0]->_get_self;
+
+ $self->lock( $self->LOCK_EX );
+
my $length = $self->FETCHSIZE();
if ($length) {
my $content = $self->FETCH( $length - 1 );
$self->DELETE( $length - 1 );
+
+ $self->unlock;
+
return $content;
}
else {
+ $self->unlock;
return;
}
}
# Add new element(s) to the end of the array
##
my $self = shift->_get_self;
- my $length = $self->FETCHSIZE();
+ $self->lock( $self->LOCK_EX );
+
+ my $length = $self->FETCHSIZE();
+
while (my $content = shift @_) {
$self->STORE( $length, $content );
$length++;
}
+ $self->unlock;
+
return $length;
}
# Shift over remaining elements to take up space.
##
my $self = $_[0]->_get_self;
+
+ $self->lock( $self->LOCK_EX );
+
my $length = $self->FETCHSIZE();
if ($length) {
$self->STORE( $i, $self->FETCH($i + 1) );
}
$self->DELETE( $length - 1 );
+
+ $self->unlock;
return $content;
}
else {
+ $self->unlock;
return;
}
}
##
my $self = shift->_get_self;
my @new_elements = @_;
+
+ $self->lock( $self->LOCK_EX );
+
my $length = $self->FETCHSIZE();
my $new_size = scalar @new_elements;
$self->STORE( $i, $new_elements[$i] );
}
+ $self->unlock;
+
return $length + $new_size;
}
# Returns deleted section, or last element deleted in scalar context.
##
my $self = shift->_get_self;
+
+ $self->lock( $self->LOCK_EX );
+
my $length = $self->FETCHSIZE();
##
$self->STORE( $i, shift @new_elements );
}
+ $self->unlock;
+
##
# Return deleted section, or last element in scalar context.
##
return wantarray ? @old_elements : $old_elements[-1];
}
-#XXX We don't need to define it.
+#XXX We don't need to define it, yet.
#XXX It will be useful, though, when we split out HASH and ARRAY
#sub EXTEND {
##
$deleted = $db->delete(-2);
is( $db->length, 3, "... and we still have three after deleting" );
is( $db->[0], undef, "0th element still undef" );
-TODO: {
- local $TODO = "delete on a negative array element should work";
- is( $db->[1], undef, "1st element now undef" );
-}
+is( $db->[1], undef, "1st element now undef" );
is( $db->[2], 'elem3', "2nd element still there after deleting" );
-TODO: {
- local $TODO = "delete on a negative array element should return the deleted value";
- is( $deleted, 'elem2', "Deleted value is correct" );
-}
+is( $deleted, 'elem2', "Deleted value is correct" );
$db->[1] = 'elem2';