4 |
# Author : Ulrich Pfeifer |
# Author : Ulrich Pfeifer |
5 |
# Created On : Thu Aug 8 13:05:10 1996 |
# Created On : Thu Aug 8 13:05:10 1996 |
6 |
# Last Modified By: Ulrich Pfeifer |
# Last Modified By: Ulrich Pfeifer |
7 |
# Last Modified On: Fri May 19 14:51:14 2000 |
# Last Modified On: Wed Jan 23 14:15:15 2002 |
8 |
# Language : CPerl |
# Language : CPerl |
9 |
# Update Count : 133 |
# Update Count : 152 |
10 |
# Status : Unknown, Use with caution! |
# Status : Unknown, Use with caution! |
11 |
# |
# |
12 |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
165 |
croak "Could not 'mkdir $self->{file}': $!\n"; |
croak "Could not 'mkdir $self->{file}': $!\n"; |
166 |
} |
} |
167 |
|
|
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1); |
|
|
# aquire a write lock |
|
|
$self->{write_lock} = $lockmgr->lock($self->{file} . '/write') |
|
|
or die "Can't lock '$self->{file}/write'"; |
|
|
|
|
168 |
$self->{djk} = $parm{djk} if defined $parm{djk}; |
$self->{djk} = $parm{djk} if defined $parm{djk}; |
169 |
$self->{layout} = $parm{layout} || new WAIT::Parse::Base; |
$self->{layout} = $parm{layout} || new WAIT::Parse::Base; |
170 |
$self->{access} = $parm{access} if defined $parm{access}; |
$self->{access} = $parm{access} if defined $parm{access}; |
173 |
$self->{indexes} = {}; |
$self->{indexes} = {}; |
174 |
|
|
175 |
bless $self, $type; |
bless $self, $type; |
176 |
|
|
177 |
|
# Checking for readers is not necessary, but let's go with the |
178 |
|
# generic method. |
179 |
|
$self->getlock(O_RDWR|O_CREAT); # dies when failing |
180 |
|
|
181 |
# Call create_index() and create_index() for compatibility |
# Call create_index() and create_index() for compatibility |
182 |
for (@{$self->{keyset}||[]}) { |
for (@{$self->{keyset}||[]}) { |
183 |
#carp "Specification of indexes at table create time is deprecated"; |
#carp "Specification of indexes at table create time is deprecated"; |
323 |
|
|
324 |
sub drop { |
sub drop { |
325 |
my $self = shift; |
my $self = shift; |
326 |
|
|
327 |
|
unless ($self->{write_lock}){ |
328 |
|
warn "Cannot drop table without write lock. Nothing done"; |
329 |
|
return; |
330 |
|
} |
331 |
|
|
332 |
if ((caller)[0] eq 'WAIT::Database') { # database knows about this |
if ((caller)[0] eq 'WAIT::Database') { # database knows about this |
333 |
$self->close; # just make sure |
$self->close; # just make sure |
334 |
|
|
335 |
my $file = $self->{file}; |
my $file = $self->{file}; |
336 |
|
|
337 |
for (values %{$self->{indexes}}) { |
for (values %{$self->{indexes}}) { |
338 |
$_->drop; |
$_->drop; |
339 |
} |
} |
340 |
unlink "$file/records"; |
unlink "$file/records"; |
341 |
|
rmdir "$file/read" or warn "Could not rmdir '$file/read'"; |
342 |
|
|
343 |
# $self->unlock; |
# $self->unlock; |
344 |
! (!-e $file or rmdir $file); |
! (!-e $file or rmdir $file); |
345 |
} else { |
} else { |
381 |
} |
} |
382 |
require WAIT::InvertedIndex; |
require WAIT::InvertedIndex; |
383 |
} |
} |
384 |
|
|
385 |
|
$self->getlock($self->{mode}); |
386 |
|
|
387 |
unless (defined $self->{dbh}) { |
unless (defined $self->{dbh}) { |
388 |
if ($USE_RECNO) { |
if ($USE_RECNO) { |
389 |
$self->{dbh} = tie(@{$self->{db}}, 'DB_File', $file, |
$self->{dbh} = tie(@{$self->{db}}, 'DB_File', $file, |
394 |
$self->{mode}, 0664, $DB_BTREE); |
$self->{mode}, 0664, $DB_BTREE); |
395 |
} |
} |
396 |
} |
} |
397 |
|
|
398 |
# Locking |
|
|
# |
|
|
# We allow multiple readers to coexists. But write access excludes |
|
|
# all read access vice versa. In practice read access on tables |
|
|
# open for writing will mostly work ;-) |
|
|
|
|
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1); |
|
|
|
|
|
# aquire a write lock. We might hold one acquired in create() already |
|
|
$self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write') |
|
|
or die "Can't lock '$self->{file}/write'"; |
|
|
|
|
|
my $lockdir = $self->{file} . '/read'; |
|
|
unless (-d $lockdir) { |
|
|
mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; |
|
|
} |
|
|
|
|
|
if ($self->{mode} & O_RDWR) { |
|
|
# this is a hack. We do not check for reopening ... |
|
|
return $self if $self->{write_lock}; |
|
|
|
|
|
# If we actually want to write we must check if there are any readers |
|
|
local *DIR; |
|
|
opendir DIR, $lockdir or |
|
|
die "Could not opendir '$lockdir': $!"; |
|
|
for my $lockfile (grep { -f "$lockdir/$_" } readdir DIR) { |
|
|
# check if the locks are still valid. |
|
|
# Since we are protected by a write lock, we could use a pline file. |
|
|
# But we want to use the stale testing from LockFile::Simple. |
|
|
if (my $lck = $lockmgr->trylock("$lockdir/$lockfile")) { |
|
|
warn "Removing stale lockfile '$lockdir/$lockfile'"; |
|
|
$lck->release; |
|
|
} else { |
|
|
$self->{write_lock}->release; |
|
|
die "Cannot write table '$file' while it's in use"; |
|
|
} |
|
|
} |
|
|
closedir DIR; |
|
|
} else { |
|
|
# this is a hack. We do not check for reopening ... |
|
|
return $self if $self->{read_lock}; |
|
|
|
|
|
# We are a reader. So we release the write lock |
|
|
my $id = time; |
|
|
while (-f "$lockdir/$id.lock") { # here assume ".lock" format! |
|
|
$id++; |
|
|
} |
|
|
$self->{read_lock} = $lockmgr->lock("$lockdir/$id"); |
|
|
$self->{write_lock}->release; |
|
|
delete $self->{write_lock}; |
|
|
} |
|
|
|
|
399 |
$self; |
$self; |
400 |
} |
} |
401 |
|
|
597 |
warn "Cannot set iattr[$iattr] without write lock. Nothing done"; |
warn "Cannot set iattr[$iattr] without write lock. Nothing done"; |
598 |
return; |
return; |
599 |
} |
} |
600 |
|
|
601 |
|
# in the rare case that they haven't written a single record yet, we |
602 |
|
# make sure, the inverted inherits our $self->{mode}: |
603 |
|
defined $self->{db} or $self->open; |
604 |
|
|
605 |
for my $att (keys %{$self->{inverted}}) { |
for my $att (keys %{$self->{inverted}}) { |
606 |
if ($] > 5.003) { # avoid bug in perl up to 5.003_05 |
require WAIT::InvertedIndex; |
607 |
|
if ($^V gt v5.003) { # avoid bug in perl up to 5.003_05 |
608 |
my $idx; |
my $idx; |
609 |
for $idx (@{$self->{inverted}->{$att}}) { |
for $idx (@{$self->{inverted}->{$att}}) { |
610 |
$idx->set($iattr, $value); |
$idx->set($iattr, $value); |
660 |
1; |
1; |
661 |
} |
} |
662 |
|
|
663 |
|
# Locking |
664 |
|
# |
665 |
|
# We allow multiple readers to coexists. But write access excludes |
666 |
|
# all read access and vice versa. In practice read access on tables |
667 |
|
# open for writing will mostly work ;-) |
668 |
|
|
669 |
|
# If a "write" lock is requested, an existing "read" lock will be |
670 |
|
# released. If a "read" lock ist requested, an existing "write" lock |
671 |
|
# will be released. Requiring a lock already hold has no effect. |
672 |
|
|
673 |
|
sub getlock { |
674 |
|
my ($self, $mode) = @_; |
675 |
|
|
676 |
|
# autoclean cleans on DESTROY, stale sends SIGZERO to the owner |
677 |
|
# |
678 |
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1, -stale => 1); |
679 |
|
my $file = $self->{file} . '/records'; |
680 |
|
my $lockdir = $self->{file} . '/read'; |
681 |
|
|
682 |
|
unless (-d $lockdir) { |
683 |
|
mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; |
684 |
|
} |
685 |
|
|
686 |
|
if ($mode & O_RDWR) { # Get a write lock. Release it again |
687 |
|
# and die if there is any valid |
688 |
|
# readers. |
689 |
|
|
690 |
|
# Have a write lock already |
691 |
|
return $self if $self->{write_lock}; |
692 |
|
|
693 |
|
if ($self->{read_lock}) { # We are a becoming a writer now. So |
694 |
|
# we release the read lock to avoid |
695 |
|
# blocking ourselves. |
696 |
|
$self->{read_lock}->release; |
697 |
|
delete $self->{read_lock}; |
698 |
|
} |
699 |
|
|
700 |
|
# Get the preliminary write lock |
701 |
|
$self->{write_lock} = $lockmgr->lock($self->{file} . '/write') |
702 |
|
or die "Can't lock '$self->{file}/write'"; |
703 |
|
|
704 |
|
# If we actually want to write we must check if there are any |
705 |
|
# readers. The write lock is confirmed if wen cannot find any |
706 |
|
# valid readers. |
707 |
|
|
708 |
|
local *DIR; |
709 |
|
opendir DIR, $lockdir or |
710 |
|
die "Could not opendir '$lockdir': $!"; |
711 |
|
for my $lockfile (grep { -f "$lockdir/$_" } readdir DIR) { |
712 |
|
# Check if the locks are still valid. Since we are protected by |
713 |
|
# a write lock, we could use a plain file. But we want to use |
714 |
|
# the stale testing from LockFile::Simple. |
715 |
|
if (my $lck = $lockmgr->trylock("$lockdir/$lockfile")) { |
716 |
|
warn "Removing stale lockfile '$lockdir/$lockfile'"; |
717 |
|
$lck->release; |
718 |
|
} else { # Found an active reader, rats! |
719 |
|
$self->{write_lock}->release; |
720 |
|
die "Cannot write table '$file' while it's in use"; |
721 |
|
} |
722 |
|
} |
723 |
|
closedir DIR; |
724 |
|
} else { |
725 |
|
# Have a read lock already |
726 |
|
return $self if $self->{read_lock}; |
727 |
|
|
728 |
|
# Get the preliminary write lock to protect the directory |
729 |
|
# operations. |
730 |
|
|
731 |
|
my $write_lock = $lockmgr->lock($self->{file} . '/read/write') |
732 |
|
or die "Can't lock '$self->{file}/read/write'"; |
733 |
|
|
734 |
|
# Find a new read slot. Maybe the plain file would be better? |
735 |
|
my $id = time; |
736 |
|
while (-f "$lockdir/$id.lock") { # here assume ".lock" format! |
737 |
|
$id++; |
738 |
|
} |
739 |
|
|
740 |
|
$self->{read_lock} = $lockmgr->lock("$lockdir/$id") |
741 |
|
or die "Can't lock '$lockdir/$id'"; |
742 |
|
|
743 |
|
# We are a reader now. So we release the write lock |
744 |
|
$write_lock->release; |
745 |
|
} |
746 |
|
return $self; |
747 |
|
} |
748 |
|
|
749 |
sub unlock { |
sub unlock { |
750 |
my $self = shift; |
my $self = shift; |
751 |
|
|
767 |
sub DESTROY { |
sub DESTROY { |
768 |
my $self = shift; |
my $self = shift; |
769 |
|
|
770 |
warn "Table handle destroyed without closing it first" |
if ($self->{write_lock} || $self->{read_lock}) { |
771 |
if $self->{write_lock} || $self->{read_lock}; |
warn "Table handle destroyed without closing it first"; |
772 |
|
$self->unlock; |
773 |
|
} |
774 |
} |
} |
775 |
|
|
776 |
sub open_scan { |
sub open_scan { |