Added setup_fh that handles inodes separate from open()
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Array.pm
index 1a0ec8e..0087ccc 100644 (file)
@@ -1,15 +1,19 @@
 package DBM::Deep::Array;
 
-$NEGATIVE_INDICES = 1;
-
 use strict;
 
+# This is to allow DBM::Deep::Array to handle negative indices on
+# its own. Otherwise, Perl would intercept the call to negative
+# indices for us. This was causing bugs for negative index handling.
+use vars qw( $NEGATIVE_INDICES );
+$NEGATIVE_INDICES = 1;
+
 use base 'DBM::Deep';
 
 use Scalar::Util ();
 
 sub _get_self {
-    eval { tied( @{$_[0]} ) } || $_[0]
+    eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
 }
 
 sub TIEARRAY {
@@ -39,7 +43,7 @@ sub FETCH {
             }
         }
 
-        $key = pack($DBM::Deep::LONG_PACK, $key);
+        $key = pack($DBM::Deep::Engine::LONG_PACK, $key);
     }
 
     my $rv = $self->SUPER::FETCH( $key );
@@ -56,25 +60,29 @@ sub STORE {
     $self->lock( $self->LOCK_EX );
 
     my $orig = $key;
-    my $size = $self->FETCHSIZE;
 
+    my $size;
     my $numeric_idx;
-    if ( $key =~ /^-?\d+$/ ) {
+    if ( $key =~ /^\-?\d+$/ ) {
         $numeric_idx = 1;
         if ( $key < 0 ) {
+            $size = $self->FETCHSIZE;
             $key += $size;
             if ( $key < 0 ) {
                 die( "Modification of non-creatable array value attempted, subscript $orig" );
             }
         }
 
-        $key = pack($DBM::Deep::LONG_PACK, $key);
+        $key = pack($DBM::Deep::Engine::LONG_PACK, $key);
     }
 
     my $rv = $self->SUPER::STORE( $key, $value );
 
-    if ( $numeric_idx && $rv == 2 && $orig >= $size ) {
-        $self->STORESIZE( $orig + 1 );
+    if ( $numeric_idx && $rv == 2 ) {
+        $size = $self->FETCHSIZE unless defined $size;
+        if ( $orig >= $size ) {
+            $self->STORESIZE( $orig + 1 );
+        }
     }
 
     $self->unlock;
@@ -88,7 +96,7 @@ sub EXISTS {
 
        $self->lock( $self->LOCK_SH );
 
-    if ( $key =~ /^-?\d+$/ ) {
+    if ( $key =~ /^\-?\d+$/ ) {
         if ( $key < 0 ) {
             $key += $self->FETCHSIZE;
             unless ( $key >= 0 ) {
@@ -97,7 +105,7 @@ sub EXISTS {
             }
         }
 
-        $key = pack($DBM::Deep::LONG_PACK, $key);
+        $key = pack($DBM::Deep::Engine::LONG_PACK, $key);
     }
 
     my $rv = $self->SUPER::EXISTS( $key );
@@ -125,7 +133,7 @@ sub DELETE {
             }
         }
 
-        $key = pack($DBM::Deep::LONG_PACK, $key);
+        $key = pack($DBM::Deep::Engine::LONG_PACK, $key);
     }
 
     my $rv = $self->SUPER::DELETE( $key );
@@ -147,17 +155,17 @@ sub FETCHSIZE {
 
     $self->lock( $self->LOCK_SH );
 
-       my $SAVE_FILTER = $self->root->{filter_fetch_value};
-       $self->root->{filter_fetch_value} = undef;
+       my $SAVE_FILTER = $self->_root->{filter_fetch_value};
+       $self->_root->{filter_fetch_value} = undef;
        
        my $packed_size = $self->FETCH('length');
        
-       $self->root->{filter_fetch_value} = $SAVE_FILTER;
+       $self->_root->{filter_fetch_value} = $SAVE_FILTER;
        
     $self->unlock;
 
        if ($packed_size) {
-        return int(unpack($DBM::Deep::LONG_PACK, $packed_size));
+        return int(unpack($DBM::Deep::Engine::LONG_PACK, $packed_size));
     }
 
        return 0;
@@ -172,12 +180,12 @@ sub STORESIZE {
        
     $self->lock( $self->LOCK_EX );
 
-       my $SAVE_FILTER = $self->root->{filter_store_value};
-       $self->root->{filter_store_value} = undef;
+       my $SAVE_FILTER = $self->_root->{filter_store_value};
+       $self->_root->{filter_store_value} = undef;
        
-       my $result = $self->STORE('length', pack($DBM::Deep::LONG_PACK, $new_length));
+       my $result = $self->STORE('length', pack($DBM::Deep::Engine::LONG_PACK, $new_length));
        
-       $self->root->{filter_store_value} = $SAVE_FILTER;
+       $self->_root->{filter_store_value} = $SAVE_FILTER;
        
     $self->unlock;
 
@@ -302,7 +310,8 @@ sub SPLICE {
        ##
        # Calculate offset and length of splice
        ##
-       my $offset = shift || 0;
+       my $offset = shift;
+    $offset = 0 unless defined $offset;
        if ($offset < 0) { $offset += $length; }
        
        my $splice_length;
@@ -316,10 +325,9 @@ sub SPLICE {
        my @new_elements = @_;
        my $new_size = scalar @new_elements;
        
-       my @old_elements = ();
-       for (my $i = $offset; $i < $offset + $splice_length; $i++) {
-               push @old_elements, $self->FETCH( $i );
-       }
+    my @old_elements = map {
+        $self->FETCH( $_ )
+    } $offset .. ($offset + $splice_length - 1);
        
        ##
        # Adjust array length, and shift elements to accomodate new section.