? $self->root->{filter_store_value}->($_[2])
: $_[2];
- my $unpacked_key = $key;
- if (($self->type eq TYPE_ARRAY) && ($key =~ /^\d+$/)) { $key = pack($LONG_PACK, $key); }
-
my $md5 = $DIGEST_FUNC->($key);
##
##
my $result = $self->_add_bucket( $tag, $md5, $key, $value );
- ##
- # If this object is an array, and bucket was not a replace, and key is numerical,
- # and index is equal or greater than current length, advance length variable.
- ##
- if (($result == 2) && ($self->type eq TYPE_ARRAY) && ($unpacked_key =~ /^\d+$/) && ($unpacked_key >= $self->FETCHSIZE())) {
- $self->STORESIZE( $unpacked_key + 1 );
- }
-
$self->unlock();
return $result;
##
# Fetch single value or element given plain key or array index
##
- my $self = $_[0]->_get_self;
-
- my $key = $_[1];
- if ( $self->type eq TYPE_HASH ) {
- if ( my $filter = $self->root->{filter_store_key} ) {
- $key = $filter->( $key );
- }
- }
-
- my $md5 = $DIGEST_FUNC->($key);
+ my $self = shift->_get_self;
+ my $key = shift;
##
# Make sure file is open
##
if (!defined($self->fh)) { $self->_open(); }
+ my $md5 = $DIGEST_FUNC->($key);
+
##
# Request shared lock for reading
##
$self->unlock();
#XXX What is ref() checking here?
- return ($result && !ref($result) && $self->root->{filter_fetch_value}) ? $self->root->{filter_fetch_value}->($result) : $result;
+ return ($result && !ref($result) && $self->root->{filter_fetch_value})
+ ? $self->root->{filter_fetch_value}->($result)
+ : $result;
}
sub DELETE {
return $class->_init($args);
}
-##
-# The following methods are for arrays only
-##
-
sub FETCH {
my $self = $_[0]->_get_self;
my $key = $_[1];
return $self->SUPER::FETCH( $key );
}
+sub STORE {
+ my $self = shift->_get_self;
+ my ($key, $value) = @_;
+
+ my $unpacked_key = $key;
+ my $size = $self->FETCHSIZE;
+
+ my $numeric_idx;
+ if ( $key =~ /^-?\d+$/ ) {
+ $numeric_idx = 1;
+ if ( $key < 0 ) {
+ $key += $size;
+ #XXX What to do here?
+# return unless $key >= 0;
+ }
+
+ $key = pack($DBM::Deep::LONG_PACK, $key);
+ }
+
+ my $rv = $self->SUPER::STORE( $key, $value );
+
+ if ( $numeric_idx && $rv == 2 && $unpacked_key >= $size ) {
+ $self->STORESIZE( $unpacked_key + 1 );
+ }
+
+ return $rv;
+}
+
sub FETCHSIZE {
##
# Return the length of the array
if ($packed_size) {
return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
}
- else { return 0; }
+
+ return 0;
}
sub STORESIZE {
return $class->_init($args);
}
+sub FETCH {
+ my $self = shift->_get_self;
+ my $key = ($self->root->{filter_store_key})
+ ? $self->root->{filter_store_key}->($_[0])
+ : $_[0];
+
+ return $self->SUPER::FETCH( $key );
+}
+
sub STORE {
my $self = shift->_get_self;
my $key = ($self->root->{filter_store_key})
# DBM::Deep Test
##
use strict;
-use Test::More tests => 90;
+use Test::More tests => 94;
use Test::Exception;
use_ok( 'DBM::Deep' );
is( $db->[-6], undef, "-6th index is undef" );
is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
+$db->[-1] = 'elem4.1';
+is( $db->[-1], 'elem4.1' );
+is( $db->[4], 'elem4.1' );
+is( $db->get(4), 'elem4.1' );
+is( $db->fetch(4), 'elem4.1' );
+
my $popped = $db->pop;
is( $db->length, 4, "... and we have four after popping" );
is( $db->[0], 'elem0', "0th element still there after popping" );
is( $db->[1], 'elem1', "1st element still there after popping" );
is( $db->[2], 'elem2', "2nd element still there after popping" );
is( $db->[3], 'elem3', "3rd element still there after popping" );
-is( $popped, 'elem4', "Popped value is correct" );
+is( $popped, 'elem4.1', "Popped value is correct" );
my $shifted = $db->shift;
is( $db->length, 3, "... and we have three after shifting" );