Added dep on 5.6.0 and started breakout of request_space()/release_space()
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep.pm
index 46f1588..5b14412 100644 (file)
@@ -43,9 +43,8 @@ $VERSION = q(0.99_01);
 ##
 # Setup constants for users to pass to new()
 ##
-sub TYPE_HASH   () { DBM::Deep::Engine::SIG_HASH   }
-sub TYPE_ARRAY  () { DBM::Deep::Engine::SIG_ARRAY  }
-sub TYPE_SCALAR () { DBM::Deep::Engine::SIG_SCALAR }
+sub TYPE_HASH   () { DBM::Deep::Engine->SIG_HASH   }
+sub TYPE_ARRAY  () { DBM::Deep::Engine->SIG_ARRAY  }
 
 sub _get_args {
     my $proto = shift;
@@ -102,16 +101,15 @@ sub _init {
     # Setup $self and bless into this class.
     ##
     my $class = shift;
-    my $args = shift;
+    my ($args) = @_;
 
     # These are the defaults to be optionally overridden below
     my $self = bless {
         type        => TYPE_HASH,
         engine      => DBM::Deep::Engine->new,
+        base_offset => undef,
     }, $class;
 
-    $self->{base_offset} = length( $self->{engine}->SIG_FILE );
-
     foreach my $param ( keys %$self ) {
         next unless exists $args->{$param};
         $self->{$param} = delete $args->{$param}
@@ -151,8 +149,8 @@ sub lock {
     # times before unlock(), then the same number of unlocks() must
     # be called before the lock is released.
     ##
-    my $self = $_[0]->_get_self;
-    my $type = $_[1];
+    my $self = shift->_get_self;
+    my ($type) = @_;
     $type = LOCK_EX unless defined $type;
 
     if (!defined($self->_fh)) { return; }
@@ -189,7 +187,7 @@ sub unlock {
     # If db locking is set, unlock the db file.  See note in lock()
     # regarding calling lock() multiple times.
     ##
-    my $self = $_[0]->_get_self;
+    my $self = shift->_get_self;
 
     if (!defined($self->_fh)) { return; }
 
@@ -262,7 +260,7 @@ sub export {
     ##
     # Recursively export into standard Perl hashes and arrays.
     ##
-    my $self = $_[0]->_get_self;
+    my $self = shift->_get_self;
 
     my $temp;
     if ($self->_type eq TYPE_HASH) { $temp = {}; }
@@ -279,19 +277,13 @@ sub import {
     ##
     # Recursively import Perl hash/array structure
     ##
-    #XXX This use of ref() seems to be ok
     if (!ref($_[0])) { return; } # Perl calls import() on use -- ignore
 
-    my $self = $_[0]->_get_self;
-    my $struct = $_[1];
+    my $self = shift->_get_self;
+    my ($struct) = @_;
 
-    #XXX This use of ref() seems to be ok
+    # struct is not a reference, so just import based on our type
     if (!ref($struct)) {
-        ##
-        # struct is not a reference, so just import based on our type
-        ##
-        shift @_;
-
         if ($self->_type eq TYPE_HASH) { $struct = {@_}; }
         elsif ($self->_type eq TYPE_ARRAY) { $struct = [@_]; }
     }
@@ -315,7 +307,7 @@ sub optimize {
     # Rebuild entire database into new file, then move
     # it back on top of original.
     ##
-    my $self = $_[0]->_get_self;
+    my $self = shift->_get_self;
 
 #XXX Need to create a new test for this
 #    if ($self->_root->{links} > 1) {
@@ -326,9 +318,6 @@ sub optimize {
         file => $self->_root->{file} . '.tmp',
         type => $self->_type
     );
-    if (!$db_temp) {
-        $self->_throw_error("Cannot optimize: failed to open temp file: $!");
-    }
 
     $self->lock();
     $self->_copy_node( $db_temp );
@@ -373,7 +362,7 @@ sub clone {
     ##
     # Make copy of object and return
     ##
-    my $self = $_[0]->_get_self;
+    my $self = shift->_get_self;
 
     return DBM::Deep->new(
         type => $self->_type,
@@ -394,9 +383,9 @@ sub clone {
         ##
         # Setup filter function for storing or fetching the key or value
         ##
-        my $self = $_[0]->_get_self;
-        my $type = lc $_[1];
-        my $func = $_[2] ? $_[2] : undef;
+        my $self = shift->_get_self;
+        my $type = lc shift;
+        my $func = shift;
 
         if ( $is_legal_filter{$type} ) {
             $self->_root->{"filter_$type"} = $func;
@@ -419,14 +408,6 @@ sub _root {
     return $self->{root};
 }
 
-sub _fh {
-    ##
-    # Get access to the raw fh
-    ##
-    my $self = $_[0]->_get_self;
-    return $self->_root->{fh};
-}
-
 sub _type {
     ##
     # Get type of current node (TYPE_HASH or TYPE_ARRAY)
@@ -443,6 +424,14 @@ sub _base_offset {
     return $self->{base_offset};
 }
 
+sub _fh {
+    ##
+    # Get access to the raw fh
+    ##
+    my $self = $_[0]->_get_self;
+    return $self->_root->{fh};
+}
+
 ##
 # Utility methods
 ##
@@ -502,7 +491,7 @@ sub FETCH {
     # Fetch single value or element given plain key or array index
     ##
     my $self = shift->_get_self;
-    my $key = shift;
+    my ($key) = @_;
 
     my $md5 = $self->{engine}{digest}->($key);
 
@@ -693,7 +682,6 @@ sub DESTROY {
 }
 
 1;
-
 __END__
 
 =head1 NAME
@@ -1565,6 +1553,56 @@ As of Perl 5.8.7, this bug still exists.  I have walked very carefully through
 the execution path, and Perl indeed passes an empty hash to the STORE() method.
 Probably a bug in Perl.
 
+=head2 REFERENCES
+
+(The reasons given assume a high level of Perl understanding, specifically of
+references. You can safely skip this section.)
+
+Currently, the only references supported are HASH and ARRAY. The other reference
+types (SCALAR, CODE, GLOB, and REF) cannot be supported for various reasons.
+
+=over 4
+
+=item * GLOB
+
+These are things like filehandles and other sockets. They can't be supported
+because it's completely unclear how DBM::Deep should serialize them.
+
+=item * SCALAR / REF
+
+The discussion here refers to the following type of example:
+
+  my $x = 25;
+  $db->{key1} = \$x;
+
+  $x = 50;
+
+  # In some other process ...
+
+  my $val = ${ $db->{key1} };
+
+  is( $val, 50, "What actually gets stored in the DB file?" );
+
+The problem is one of synchronization. When the variable being referred to
+changes value, the reference isn't notified. This means that the new value won't
+be stored in the datafile for other processes to read. There is no TIEREF.
+
+It is theoretically possible to store references to values already within a
+DBM::Deep object because everything already is synchronized, but the change to
+the internals would be quite large. Specifically, DBM::Deep would have to tie
+every single value that is stored. This would bloat the RAM footprint of
+DBM::Deep at least twofold (if not more) and be a significant performance drain,
+all to support a feature that has never been requested.
+
+=item * CODE
+
+L<http://search.cpan.org/search?module=Data::Dump::Streamer> provides a
+mechanism for serializing coderefs, including saving off all closure state.
+However, just as for SCALAR and REF, that closure state may change without
+notifying the DBM::Deep object storing the reference.
+
+=back
+
 =head2 FILE CORRUPTION
 
 The current level of error handling in DBM::Deep is minimal.  Files I<are> checked