rollback and commit both work. Need to add MORE and MORE tests
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep.pm
index 98d5df9..29318f6 100644 (file)
@@ -29,13 +29,17 @@ package DBM::Deep;
 #    modify it under the same terms as Perl itself.
 ##
 
+use 5.6.0;
+
 use strict;
+use warnings;
 
 use Fcntl qw( :DEFAULT :flock :seek );
 use Digest::MD5 ();
 use Scalar::Util ();
 
 use DBM::Deep::Engine;
+use DBM::Deep::File;
 
 use vars qw( $VERSION );
 $VERSION = q(0.99_01);
@@ -102,12 +106,23 @@ sub _init {
     my $class = shift;
     my ($args) = @_;
 
+    $args->{fileobj} = DBM::Deep::File->new( $args )
+        unless exists $args->{fileobj};
+
+    # locking implicitly enables autoflush
+    if ($args->{locking}) { $args->{autoflush} = 1; }
+
     # These are the defaults to be optionally overridden below
     my $self = bless {
         type        => TYPE_HASH,
-        engine      => DBM::Deep::Engine->new( $args ),
         base_offset => undef,
+
+        parent      => undef,
+        parent_key  => undef,
+
+        fileobj     => undef,
     }, $class;
+    $self->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } );
 
     # Grab the parameters we want to use
     foreach my $param ( keys %$self ) {
@@ -115,15 +130,10 @@ sub _init {
         $self->{$param} = $args->{$param};
     }
 
-    # locking implicitly enables autoflush
-    if ($args->{locking}) { $args->{autoflush} = 1; }
-
-    $self->{root} = exists $args->{root}
-        ? $args->{root}
-        : DBM::Deep::_::Root->new( $args );
-
     $self->{engine}->setup_fh( $self );
 
+    $self->{fileobj}->set_db( $self );
+
     return $self;
 }
 
@@ -139,66 +149,14 @@ sub TIEARRAY {
     return DBM::Deep::Array->TIEARRAY( @_ );
 }
 
-#XXX Unneeded now ...
-#sub DESTROY {
-#}
-
 sub lock {
-    ##
-    # If db locking is set, flock() the db file.  If called multiple
-    # times before unlock(), then the same number of unlocks() must
-    # be called before the lock is released.
-    ##
     my $self = shift->_get_self;
-    my ($type) = @_;
-    $type = LOCK_EX unless defined $type;
-
-    if (!defined($self->_fh)) { return; }
-
-    if ($self->_root->{locking}) {
-        if (!$self->_root->{locked}) {
-            flock($self->_fh, $type);
-
-            # refresh end counter in case file has changed size
-            my @stats = stat($self->_fh);
-            $self->_root->{end} = $stats[7];
-
-            # double-check file inode, in case another process
-            # has optimize()d our file while we were waiting.
-            if ($stats[1] != $self->_root->{inode}) {
-                $self->{engine}->close_fh( $self );
-                $self->{engine}->setup_fh( $self );
-                flock($self->_fh, $type); # re-lock
-
-                # This may not be necessary after re-opening
-                $self->_root->{end} = (stat($self->_fh))[7]; # re-end
-            }
-        }
-        $self->_root->{locked}++;
-
-        return 1;
-    }
-
-    return;
+    return $self->_fileobj->lock( $self, @_ );
 }
 
 sub unlock {
-    ##
-    # If db locking is set, unlock the db file.  See note in lock()
-    # regarding calling lock() multiple times.
-    ##
     my $self = shift->_get_self;
-
-    if (!defined($self->_fh)) { return; }
-
-    if ($self->_root->{locking} && $self->_root->{locked} > 0) {
-        $self->_root->{locked}--;
-        if (!$self->_root->{locked}) { flock($self->_fh, LOCK_UN); }
-
-        return 1;
-    }
-
-    return;
+    return $self->_fileobj->unlock( $self, @_ );
 }
 
 sub _copy_value {
@@ -276,12 +234,12 @@ sub optimize {
     my $self = shift->_get_self;
 
 #XXX Need to create a new test for this
-#    if ($self->_root->{links} > 1) {
+#    if ($self->_fileobj->{links} > 1) {
 #        $self->_throw_error("Cannot optimize: reference count is greater than 1");
 #    }
 
     my $db_temp = DBM::Deep->new(
-        file => $self->_root->{file} . '.tmp',
+        file => $self->_fileobj->{file} . '.tmp',
         type => $self->_type
     );
 
@@ -296,8 +254,8 @@ sub optimize {
     my $perms = $stats[2] & 07777;
     my $uid = $stats[4];
     my $gid = $stats[5];
-    chown( $uid, $gid, $self->_root->{file} . '.tmp' );
-    chmod( $perms, $self->_root->{file} . '.tmp' );
+    chown( $uid, $gid, $self->_fileobj->{file} . '.tmp' );
+    chmod( $perms, $self->_fileobj->{file} . '.tmp' );
 
     # q.v. perlport for more information on this variable
     if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
@@ -308,17 +266,18 @@ sub optimize {
         # with a soft copy.
         ##
         $self->unlock();
-        $self->{engine}->close_fh( $self );
+        $self->_fileobj->close;
     }
 
-    if (!rename $self->_root->{file} . '.tmp', $self->_root->{file}) {
-        unlink $self->_root->{file} . '.tmp';
+    if (!rename $self->_fileobj->{file} . '.tmp', $self->_fileobj->{file}) {
+        unlink $self->_fileobj->{file} . '.tmp';
         $self->unlock();
         $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
     }
 
     $self->unlock();
-    $self->{engine}->close_fh( $self );
+    $self->_fileobj->close;
+    $self->_fileobj->open;
     $self->{engine}->setup_fh( $self );
 
     return 1;
@@ -333,7 +292,7 @@ sub clone {
     return DBM::Deep->new(
         type => $self->_type,
         base_offset => $self->_base_offset,
-        root => $self->_root
+        fileobj => $self->_fileobj,
     );
 }
 
@@ -354,7 +313,7 @@ sub clone {
         my $func = shift;
 
         if ( $is_legal_filter{$type} ) {
-            $self->_root->{"filter_$type"} = $func;
+            $self->_fileobj->{"filter_$type"} = $func;
             return 1;
         }
 
@@ -364,50 +323,44 @@ sub clone {
 
 sub begin_work {
     my $self = shift->_get_self;
+    $self->_fileobj->begin_transaction;
+    return 1;
 }
 
 sub rollback {
     my $self = shift->_get_self;
+    $self->_fileobj->end_transaction;
+    return 1;
 }
 
-#sub commit {
-#    my $self = shift->_get_self;
-#}
+sub commit {
+    my $self = shift->_get_self;
+    $self->_fileobj->commit_transaction;
+    return 1;
+}
 
 ##
 # Accessor methods
 ##
 
-sub _root {
-    ##
-    # Get access to the root structure
-    ##
+sub _fileobj {
     my $self = $_[0]->_get_self;
-    return $self->{root};
+    return $self->{fileobj};
 }
 
 sub _type {
-    ##
-    # Get type of current node (TYPE_HASH or TYPE_ARRAY)
-    ##
     my $self = $_[0]->_get_self;
     return $self->{type};
 }
 
 sub _base_offset {
-    ##
-    # Get base_offset of current node (TYPE_HASH or TYPE_ARRAY)
-    ##
     my $self = $_[0]->_get_self;
     return $self->{base_offset};
 }
 
 sub _fh {
-    ##
-    # Get access to the raw fh
-    ##
     my $self = $_[0]->_get_self;
-    return $self->_root->{fh};
+    return $self->_fileobj->{fh};
 }
 
 ##
@@ -428,17 +381,89 @@ sub _is_writable {
 #    (O_RDONLY | O_RDWR) & fcntl( $fh, F_GETFL, my $slush = 0);
 #}
 
+sub _find_parent {
+    my $self = shift;
+
+    my $base = '';
+    if ( my $parent = $self->{parent} ) {
+        my $child = $self;
+        while ( $parent->{parent} ) {
+            $base = (
+                $parent->_type eq TYPE_HASH
+                    ? "\{$child->{parent_key}\}"
+                    : "\[$child->{parent_key}\]"
+#                "->get('$child->{parent_key}')"
+            ) . $base;
+
+            $child = $parent;
+            $parent = $parent->{parent};
+#            last unless $parent;
+        }
+        if ( $base ) {
+            $base = "\$db->get( '$child->{parent_key}' )->" . $base;
+        }
+        else {
+            $base = "\$db->get( '$child->{parent_key}' )";
+        }
+    }
+#    return '$db->' . $base;
+#    return '$db' . $base;
+    return $base;
+}
+
 sub STORE {
     ##
     # Store single hash key/value or array element in database.
     ##
     my $self = shift->_get_self;
-    my ($key, $value) = @_;
+    my ($key, $value, $orig_key) = @_;
+
 
     if ( $^O ne 'MSWin32' && !_is_writable( $self->_fh ) ) {
         $self->_throw_error( 'Cannot write to a readonly filehandle' );
     }
 
+    if ( defined $orig_key ) {
+        my $rhs;
+
+        my $r = Scalar::Util::reftype( $value ) || '';
+        if ( $r eq 'HASH' ) {
+            $rhs = '{}';
+        }
+        elsif ( $r eq 'ARRAY' ) {
+            $rhs = '[]';
+        }
+        elsif ( defined $value ) {
+            $rhs = "'$value'";
+        }
+        else {
+            $rhs = "undef";
+        }
+
+        if ( my $c = Scalar::Util::blessed( $value ) ) {
+            $rhs = "bless $rhs, '$c'";
+        }
+
+        my $lhs = $self->_find_parent;
+        if ( $lhs ) {
+            if ( $self->_type eq TYPE_HASH ) {
+                $lhs .= "->\{$orig_key\}";
+            }
+            else {
+                $lhs .= "->\[$orig_key\]";
+            }
+
+            $lhs .= "=$rhs;";
+        }
+        else {
+            $lhs = "\$db->put('$orig_key',$rhs);";
+        }
+
+#        $self->_fileobj->audit( "$lhs = $rhs;" );
+#        $self->_fileobj->audit( "$lhs $rhs);" );
+        $self->_fileobj->audit($lhs);
+    }
+
     ##
     # Request exclusive lock for writing
     ##
@@ -446,18 +471,18 @@ sub STORE {
 
     my $md5 = $self->{engine}{digest}->($key);
 
-    my $tag = $self->{engine}->find_bucket_list( $self, $md5, { create => 1 } );
+    my $tag = $self->{engine}->find_bucket_list( $self->_base_offset, $md5, { create => 1 } );
 
     # User may be storing a hash, in which case we do not want it run
     # through the filtering system
-    if ( !ref($value) && $self->_root->{filter_store_value} ) {
-        $value = $self->_root->{filter_store_value}->( $value );
+    if ( !ref($value) && $self->_fileobj->{filter_store_value} ) {
+        $value = $self->_fileobj->{filter_store_value}->( $value );
     }
 
     ##
     # Add key/value to bucket list
     ##
-    my $result = $self->{engine}->add_bucket( $self, $tag, $md5, $key, $value );
+    my $result = $self->{engine}->add_bucket( $tag, $md5, $key, $value, undef, $orig_key ); 
 
     $self->unlock();
 
@@ -469,7 +494,7 @@ sub FETCH {
     # Fetch single value or element given plain key or array index
     ##
     my $self = shift->_get_self;
-    my ($key) = @_;
+    my ($key, $orig_key) = @_;
 
     my $md5 = $self->{engine}{digest}->($key);
 
@@ -478,7 +503,7 @@ sub FETCH {
     ##
     $self->lock( LOCK_SH );
 
-    my $tag = $self->{engine}->find_bucket_list( $self, $md5 );
+    my $tag = $self->{engine}->find_bucket_list( $self->_base_offset, $md5 );
     if (!$tag) {
         $self->unlock();
         return;
@@ -487,14 +512,14 @@ sub FETCH {
     ##
     # Get value from bucket list
     ##
-    my $result = $self->{engine}->get_bucket_value( $self, $tag, $md5 );
+    my $result = $self->{engine}->get_bucket_value( $tag, $md5, $orig_key );
 
     $self->unlock();
 
     # Filters only apply to scalar values, so the ref check is making
     # sure the fetched bucket is a scalar, not a child hash or array.
-    return ($result && !ref($result) && $self->_root->{filter_fetch_value})
-        ? $self->_root->{filter_fetch_value}->($result)
+    return ($result && !ref($result) && $self->_fileobj->{filter_fetch_value})
+        ? $self->_fileobj->{filter_fetch_value}->($result)
         : $result;
 }
 
@@ -502,13 +527,32 @@ sub DELETE {
     ##
     # Delete single key/value pair or element given plain key or array index
     ##
-    my $self = $_[0]->_get_self;
-    my $key = $_[1];
+    my $self = shift->_get_self;
+    my ($key, $orig_key) = @_;
 
     if ( $^O ne 'MSWin32' && !_is_writable( $self->_fh ) ) {
         $self->_throw_error( 'Cannot write to a readonly filehandle' );
     }
 
+    if ( defined $orig_key ) {
+        my $lhs = $self->_find_parent;
+#        if ( $self->_type eq TYPE_HASH ) {
+#            $lhs .= "\{$orig_key\}";
+#        }
+#        else {
+#            $lhs .= "\[$orig_key]\]";
+#        }
+
+#        $self->_fileobj->audit( "delete $lhs;" );
+#        $self->_fileobj->audit( "$lhs->delete('$orig_key');" );
+        if ( $lhs ) {
+            $self->_fileobj->audit( "delete $lhs;" );
+        }
+        else {
+            $self->_fileobj->audit( "\$db->delete('$orig_key');" );
+        }
+    }
+
     ##
     # Request exclusive lock for writing
     ##
@@ -516,7 +560,7 @@ sub DELETE {
 
     my $md5 = $self->{engine}{digest}->($key);
 
-    my $tag = $self->{engine}->find_bucket_list( $self, $md5 );
+    my $tag = $self->{engine}->find_bucket_list( $self->_base_offset, $md5 );
     if (!$tag) {
         $self->unlock();
         return;
@@ -525,13 +569,13 @@ sub DELETE {
     ##
     # Delete bucket
     ##
-    my $value = $self->{engine}->get_bucket_value($self,  $tag, $md5 );
+    my $value = $self->{engine}->get_bucket_value( $tag, $md5 );
 
-    if (defined $value && !ref($value) && $self->_root->{filter_fetch_value}) {
-        $value = $self->_root->{filter_fetch_value}->($value);
+    if (defined $value && !ref($value) && $self->_fileobj->{filter_fetch_value}) {
+        $value = $self->_fileobj->{filter_fetch_value}->($value);
     }
 
-    my $result = $self->{engine}->delete_bucket( $self, $tag, $md5 );
+    my $result = $self->{engine}->delete_bucket( $tag, $md5, $orig_key );
 
     ##
     # If this object is an array and the key deleted was on the end of the stack,
@@ -547,8 +591,8 @@ sub EXISTS {
     ##
     # Check if a single key or element exists given plain key or array index
     ##
-    my $self = $_[0]->_get_self;
-    my $key = $_[1];
+    my $self = shift->_get_self;
+    my ($key) = @_;
 
     my $md5 = $self->{engine}{digest}->($key);
 
@@ -557,7 +601,7 @@ sub EXISTS {
     ##
     $self->lock( LOCK_SH );
 
-    my $tag = $self->{engine}->find_bucket_list( $self, $md5 );
+    my $tag = $self->{engine}->find_bucket_list( $self->_base_offset, $md5 );
     if (!$tag) {
         $self->unlock();
 
@@ -570,7 +614,7 @@ sub EXISTS {
     ##
     # Check if bucket exists and return 1 or ''
     ##
-    my $result = $self->{engine}->bucket_exists( $self, $tag, $md5 ) || '';
+    my $result = $self->{engine}->bucket_exists( $tag, $md5 ) || '';
 
     $self->unlock();
 
@@ -581,28 +625,33 @@ sub CLEAR {
     ##
     # Clear all keys from hash, or all elements from array.
     ##
-    my $self = $_[0]->_get_self;
+    my $self = shift->_get_self;
 
     if ( $^O ne 'MSWin32' && !_is_writable( $self->_fh ) ) {
         $self->_throw_error( 'Cannot write to a readonly filehandle' );
     }
 
+    {
+        my $lhs = $self->_find_parent;
+
+        if ( $self->_type eq TYPE_HASH ) {
+            $lhs = '%{' . $lhs . '}';
+        }
+        else {
+            $lhs = '@{' . $lhs . '}';
+        }
+
+        $self->_fileobj->audit( "$lhs = ();" );
+    }
+
     ##
     # Request exclusive lock for writing
     ##
     $self->lock( LOCK_EX );
 
-    my $fh = $self->_fh;
-
-    seek($fh, $self->_base_offset + $self->_root->{file_offset}, SEEK_SET);
-    if (eof $fh) {
-        $self->unlock();
-        return;
-    }
-
 #XXX This needs updating to use _release_space
     $self->{engine}->write_tag(
-        $self, $self->_base_offset, $self->_type,
+        $self->_base_offset, $self->_type,
         chr(0)x$self->{engine}{index_size},
     );
 
@@ -622,49 +671,6 @@ sub delete { (shift)->DELETE( @_ ) }
 sub exists { (shift)->EXISTS( @_ ) }
 sub clear { (shift)->CLEAR( @_ ) }
 
-package DBM::Deep::_::Root;
-
-sub new {
-    my $class = shift;
-    my ($args) = @_;
-
-    my $self = bless {
-        autobless          => undef,
-        autoflush          => undef,
-        end                => 0,
-        fh                 => undef,
-        file               => undef,
-        file_offset        => 0,
-        locking            => undef,
-        locked             => 0,
-        filter_store_key   => undef,
-        filter_store_value => undef,
-        filter_fetch_key   => undef,
-        filter_fetch_value => undef,
-    }, $class;
-
-    # Grab the parameters we want to use
-    foreach my $param ( keys %$self ) {
-        next unless exists $args->{$param};
-        $self->{$param} = $args->{$param};
-    }
-
-    if ( $self->{fh} && !$self->{file_offset} ) {
-        $self->{file_offset} = tell( $self->{fh} );
-    }
-
-    return $self;
-}
-
-sub DESTROY {
-    my $self = shift;
-    return unless $self;
-
-    close $self->{fh} if $self->{fh};
-
-    return;
-}
-
 1;
 __END__
 
@@ -821,7 +827,16 @@ If you pass in fh and do not set this, it will be set appropriately.
 =item * type
 
 This parameter specifies what type of object to create, a hash or array.  Use
-one of these two constants: C<DBM::Deep-E<gt>TYPE_HASH> or C<DBM::Deep-E<gt>TYPE_ARRAY>.
+one of these two constants:
+
+=over 4
+
+=item * C<DBM::Deep-E<gt>TYPE_HASH>
+
+=item * C<DBM::Deep-E<gt>TYPE_ARRAY>.
+
+=back
+
 This only takes effect when beginning a new file.  This is an optional
 parameter, and defaults to C<DBM::Deep-E<gt>TYPE_HASH>.
 
@@ -843,16 +858,15 @@ Pass any true value to enable.  This is an optional parameter, and defaults to 0
 
 =item * autobless
 
-If I<autobless> mode is enabled, DBM::Deep will preserve blessed hashes, and
-restore them when fetched.  This is an B<experimental> feature, and does have
-side-effects.  Basically, when hashes are re-blessed into their original
-classes, they are no longer blessed into the DBM::Deep class!  So you won't be
-able to call any DBM::Deep methods on them.  You have been warned.
-This is an optional parameter, and defaults to 0 (disabled).
+If I<autobless> mode is enabled, DBM::Deep will preserve the class something
+is blessed into, and restores it when fetched.  This is an optional parameter, and defaults to 1 (enabled).
+
+B<Note:> If you use the OO-interface, you will not be able to call any methods
+of DBM::Deep on the blessed item. This is considered to be a feature.
 
 =item * filter_*
 
-See L<FILTERS> below.
+See L</FILTERS> below.
 
 =back
 
@@ -1420,10 +1434,10 @@ you can call the C<_fh()> method, which returns the handle:
 This method can be called on the root level of the datbase, or any child
 hashes or arrays.  All levels share a I<root> structure, which contains things
 like the filehandle, a reference counter, and all the options specified
-when you created the object.  You can get access to this root structure by
-calling the C<root()> method.
+when you created the object.  You can get access to this file object by
+calling the C<_fileobj()> method.
 
-    my $root = $db->_root();
+    my $file_obj = $db->_fileobj();
 
 This is useful for changing options after the object has already been created,
 such as enabling/disabling locking.  You can also store your own temporary user