--- cvs-head/lib/WAIT/Table.pm 2000/11/15 08:54:25 42 +++ cvs-head/lib/WAIT/Table.pm 2004/05/24 13:41:28 86 @@ -4,9 +4,9 @@ # Author : Ulrich Pfeifer # Created On : Thu Aug 8 13:05:10 1996 # Last Modified By: Ulrich Pfeifer -# Last Modified On: Tue Nov 14 16:19:17 2000 +# Last Modified On: Sat Apr 27 17:20:31 2002 # Language : CPerl -# Update Count : 149 +# Update Count : 172 # Status : Unknown, Use with caution! # # Copyright (c) 1996-1997, Ulrich Pfeifer @@ -32,7 +32,7 @@ use strict; use Carp; # use autouse Carp => qw( croak($) ); -use DB_File; +use BerkeleyDB; use Fcntl; use LockFile::Simple (); @@ -159,10 +159,8 @@ } $self->{file} = $parm{file} or croak "No file specified"; - if (-d $self->{file}){ - warn "Warning: Directory '$self->{file}' already exists\n"; - } elsif (!mkdir($self->{file}, 0775)) { - croak "Could not 'mkdir $self->{file}': $!\n"; + if (-e $self->{file}){ + warn "Warning: file '$self->{file}' already exists\n"; } $self->{djk} = $parm{djk} if defined $parm{djk}; @@ -223,7 +221,7 @@ my $name = join '-', @_; $self->{indexes}->{$name} = - new WAIT::Index file => $self->{file}.'/'.$name, attr => $_; + new WAIT::Index file => $self->{file}, name => $name, attr => $_; } =head2 Creating an inverted index @@ -337,11 +335,9 @@ for (values %{$self->{indexes}}) { $_->drop; } - unlink "$file/records"; - rmdir "$file/read" or warn "Could not rmdir '$file/read'"; - - # $self->unlock; - ! (!-e $file or rmdir $file); + rmdir "$file.read" or warn "Could not rmdir '$file/read'"; + unlink "$file"; + } else { croak ref($self)."::drop called directly"; } @@ -384,14 +380,20 @@ $self->getlock($self->{mode}); + my $dbmode = ($self->{mode} & O_CREAT) ? DB_CREATE : 0; unless (defined $self->{dbh}) { if ($USE_RECNO) { - $self->{dbh} = tie(@{$self->{db}}, 'DB_File', $file, - $self->{mode}, 0664, $DB_RECNO); + tie(%{$self->{db}}, 'BerkeleyDB::Recno', + -Filename => $self->{file}, + -Subname => 'records', + -Flags => $dbmode); } else { $self->{dbh} = - tie(%{$self->{db}}, 'DB_File', $file, - $self->{mode}, 0664, $DB_BTREE); + tie(%{$self->{db}}, 'BerkeleyDB::Btree', + -Filename => $self->{file}, + -Subname => 'records', + -Mode => 0664, + -Flags => $dbmode); } } @@ -597,8 +599,14 @@ warn "Cannot set iattr[$iattr] without write lock. Nothing done"; return; } + + # in the rare case that they haven't written a single record yet, we + # make sure, the inverted inherits our $self->{mode}: + defined $self->{db} or $self->open; + for my $att (keys %{$self->{inverted}}) { - if ($] > 5.003) { # avoid bug in perl up to 5.003_05 + require WAIT::InvertedIndex; + if ($^V gt v5.003) { # avoid bug in perl up to 5.003_05 my $idx; for $idx (@{$self->{inverted}->{$att}}) { $idx->set($iattr, $value); @@ -670,8 +678,8 @@ # autoclean cleans on DESTROY, stale sends SIGZERO to the owner # my $lockmgr = LockFile::Simple->make(-autoclean => 1, -stale => 1); - my $file = $self->{file} . '/records'; - my $lockdir = $self->{file} . '/read'; + my $file = $self->{file}; + my $lockdir = $self->{file} . '.read'; unless (-d $lockdir) { mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; @@ -692,8 +700,8 @@ } # Get the preliminary write lock - $self->{write_lock} = $lockmgr->lock($self->{file} . '/write') - or die "Can't lock '$self->{file}/write'"; + $self->{write_lock} = $lockmgr->lock($self->{file} . '.write') + or die "Can't lock '$self->{file}.write'"; # If we actually want to write we must check if there are any # readers. The write lock is confirmed if wen cannot find any @@ -720,10 +728,10 @@ return $self if $self->{read_lock}; # Get the preliminary write lock to protect the directory - # operations. If we already have a write lock, it will go. + # operations. - $self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write') - or die "Can't lock '$self->{file}/write'"; + my $write_lock = $lockmgr->lock($self->{file} . '.read/write') + or die "Can't lock '$self->{file}.read/write'"; # Find a new read slot. Maybe the plain file would be better? my $id = time; @@ -735,8 +743,7 @@ or die "Can't lock '$lockdir/$id'"; # We are a reader now. So we release the write lock - $self->{write_lock}->release; - delete $self->{write_lock}; + $write_lock->release; } return $self; }