From: rkinyon Date: Tue, 28 Feb 2006 19:45:24 +0000 (+0000) Subject: Moved _close() into the engine X-Git-Tag: 0-99_01~115 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cd59cad8dbdb2a674496ec03a4e75a154aacd7d5;p=dbsrgits%2FDBM-Deep.git Moved _close() into the engine --- diff --git a/lib/DBM/Deep.pm b/lib/DBM/Deep.pm index 6c08cca..baca8cc 100644 --- a/lib/DBM/Deep.pm +++ b/lib/DBM/Deep.pm @@ -203,15 +203,6 @@ sub TIEARRAY { #sub DESTROY { #} -sub _close { - ## - # Close database fh - ## - my $self = $_[0]->_get_self; - close $self->_root->{fh} if $self->_root->{fh}; - $self->_root->{fh} = undef; -} - sub _create_tag { ## # Given offset, signature and content, create tag and write to disk @@ -843,7 +834,7 @@ sub lock { # 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}->open($self); # re-open + $self->{engine}->open( $self ); # re-open flock($self->_fh, $type); # re-lock $self->_root->{end} = (stat($self->_fh))[7]; # re-end } @@ -1025,7 +1016,7 @@ sub optimize { # with a soft copy. ## $self->unlock(); - $self->_close(); + $self->{engine}->close( $self ); } if (!rename $self->_root->{file} . '.tmp', $self->_root->{file}) { @@ -1035,8 +1026,8 @@ sub optimize { } $self->unlock(); - $self->_close(); - $self->{engine}->open($self); + $self->{engine}->close( $self ); + $self->{engine}->open( $self ); return 1; } diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index ff262fc..1dd7dd6 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -9,10 +9,10 @@ sub open { # Open a fh to the database, create if nonexistent. # Make sure file signature matches DBM::Deep spec. ## - shift; my $self = shift; + my $obj = shift; - if (defined($self->_fh)) { $self->_close(); } + if (defined($obj->_fh)) { $self->close( $obj ); } eval { local $SIG{'__DIE__'}; @@ -21,27 +21,27 @@ sub open { my $flags = O_RDWR | O_CREAT | O_BINARY; my $fh; - sysopen( $fh, $self->_root->{file}, $flags ) + sysopen( $fh, $obj->_root->{file}, $flags ) or $fh = undef; - $self->_root->{fh} = $fh; - }; if ($@ ) { $self->_throw_error( "Received error: $@\n" ); } - if (! defined($self->_fh)) { - return $self->_throw_error("Cannot sysopen file: " . $self->_root->{file} . ": $!"); + $obj->_root->{fh} = $fh; + }; if ($@ ) { $obj->_throw_error( "Received error: $@\n" ); } + if (! defined($obj->_fh)) { + return $obj->_throw_error("Cannot sysopen file: " . $obj->_root->{file} . ": $!"); } - my $fh = $self->_fh; + my $fh = $obj->_fh; #XXX Can we remove this by using the right sysopen() flags? # Maybe ... q.v. above binmode $fh; # for win32 - if ($self->_root->{autoflush}) { + if ($obj->_root->{autoflush}) { my $old = select $fh; $|=1; select $old; } - seek($fh, 0 + $self->_root->{file_offset}, SEEK_SET); + seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET); my $signature; my $bytes_read = read( $fh, $signature, length(DBM::Deep->SIG_FILE)); @@ -50,9 +50,9 @@ sub open { # File is empty -- write signature and master index ## if (!$bytes_read) { - seek($fh, 0 + $self->_root->{file_offset}, SEEK_SET); + seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET); print( $fh DBM::Deep->SIG_FILE); - $self->_create_tag($self->_base_offset, $self->_type, chr(0) x $DBM::Deep::INDEX_SIZE); + $obj->_create_tag($obj->_base_offset, $obj->_type, chr(0) x $DBM::Deep::INDEX_SIZE); my $plain_key = "[base]"; print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($plain_key)) . $plain_key ); @@ -63,8 +63,8 @@ sub open { select $old_fh; my @stats = stat($fh); - $self->_root->{inode} = $stats[1]; - $self->_root->{end} = $stats[7]; + $obj->_root->{inode} = $stats[1]; + $obj->_root->{end} = $stats[7]; return 1; } @@ -73,31 +73,43 @@ sub open { # Check signature was valid ## unless ($signature eq DBM::Deep->SIG_FILE) { - $self->_close(); - return $self->_throw_error("Signature not found -- file is not a Deep DB"); + $self->close( $obj ); + return $obj->_throw_error("Signature not found -- file is not a Deep DB"); } my @stats = stat($fh); - $self->_root->{inode} = $stats[1]; - $self->_root->{end} = $stats[7]; + $obj->_root->{inode} = $stats[1]; + $obj->_root->{end} = $stats[7]; ## # Get our type from master index signature ## - my $tag = $self->_load_tag($self->_base_offset); + my $tag = $obj->_load_tag($obj->_base_offset); #XXX We probably also want to store the hash algorithm name and not assume anything #XXX The cool thing would be to allow a different hashing algorithm at every level if (!$tag) { - return $self->_throw_error("Corrupted file, no master index record"); + return $obj->_throw_error("Corrupted file, no master index record"); } - if ($self->{type} ne $tag->{signature}) { - return $self->_throw_error("File type mismatch"); + if ($obj->{type} ne $tag->{signature}) { + return $obj->_throw_error("File type mismatch"); } return 1; } +sub close { + my $self = shift; + my $obj = shift; + + if ( my $fh = $obj->_root->{fh} ) { + close $fh; + } + $obj->_root->{fh} = undef; + + return 1; +} + 1; __END__ diff --git a/t/23_misc.t b/t/23_misc.t index 4c376e6..3d12cf2 100644 --- a/t/23_misc.t +++ b/t/23_misc.t @@ -34,7 +34,7 @@ throws_ok { file => 't/test.db', locking => 1, ); - $db->_close; + $db->_get_self->{engine}->close( $db->_get_self ); ok( !$db->lock ); } @@ -44,6 +44,6 @@ throws_ok { locking => 1, ); $db->lock; - $db->_close; + $db->_get_self->{engine}->close( $db->_get_self ); ok( !$db->unlock ); }