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: Sun Nov 12 15:21:19 2000 |
# Last Modified On: Sun Nov 12 17:51:56 2000 |
8 |
# Language : CPerl |
# Language : CPerl |
9 |
# Update Count : 135 |
# Update Count : 148 |
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, since we are creating the table, no readers |
|
|
# could possibly be active. |
|
|
$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 { |
391 |
$self->{mode}, 0664, $DB_BTREE); |
$self->{mode}, 0664, $DB_BTREE); |
392 |
} |
} |
393 |
} |
} |
394 |
|
|
395 |
# Locking |
$self->getlock($self->{mode}); |
396 |
# |
|
|
# We allow multiple readers to coexists. But write access excludes |
|
|
# all read access and vice versa. In practice read access on tables |
|
|
# open for writing will mostly work ;-) |
|
|
|
|
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1); |
|
|
|
|
|
my $lockdir = $self->{file} . '/read'; |
|
|
unless (-d $lockdir) { |
|
|
mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; |
|
|
} |
|
|
|
|
|
if ($self->{mode} & O_RDWR) { |
|
|
# Get a write lock. Release it again and die if there is any |
|
|
# valid reader. |
|
|
|
|
|
# this is a hack. We do not check for reopening ... |
|
|
return $self if $self->{write_lock}; |
|
|
|
|
|
if ($self->{read_lock}) { |
|
|
# We are a becoming a writer now. So we release the read lock to |
|
|
# avoid blocking ourselves. |
|
|
$self->{read_lock}->release; |
|
|
delete $self->{read_lock}; |
|
|
} |
|
|
|
|
|
# Get the preliminary write lock |
|
|
$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 |
|
|
# valid 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 plain 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}; |
|
|
|
|
|
# Get the preliminary write lock to protect the directory |
|
|
# operations. |
|
|
|
|
|
$self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write') |
|
|
or die "Can't lock '$self->{file}/write'"; |
|
|
|
|
|
# find a new read slot |
|
|
my $id = time; |
|
|
while (-f "$lockdir/$id.lock") { # here assume ".lock" format! |
|
|
$id++; |
|
|
} |
|
|
|
|
|
$self->{read_lock} = $lockmgr->lock("$lockdir/$id") |
|
|
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}; |
|
|
} |
|
|
|
|
397 |
$self; |
$self; |
398 |
} |
} |
399 |
|
|
652 |
1; |
1; |
653 |
} |
} |
654 |
|
|
655 |
|
# Locking |
656 |
|
# |
657 |
|
# We allow multiple readers to coexists. But write access excludes |
658 |
|
# all read access and vice versa. In practice read access on tables |
659 |
|
# open for writing will mostly work ;-) |
660 |
|
|
661 |
|
# If a "write" lock is requested, an existing "read" lock will be |
662 |
|
# released. If a "read" lock ist requested, an existing "write" lock |
663 |
|
# will be released. Requiring a lock already hold has no effect. |
664 |
|
|
665 |
|
sub getlock { |
666 |
|
my ($self, $mode) = @_; |
667 |
|
|
668 |
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1); |
669 |
|
my $file = $self->{file} . '/records'; |
670 |
|
my $lockdir = $self->{file} . '/read'; |
671 |
|
|
672 |
|
unless (-d $lockdir) { |
673 |
|
mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; |
674 |
|
} |
675 |
|
|
676 |
|
if ($mode & O_RDWR) { # Get a write lock. Release it again |
677 |
|
# and die if there is any valid |
678 |
|
# readers. |
679 |
|
|
680 |
|
# Have a write lock already |
681 |
|
return $self if $self->{write_lock}; |
682 |
|
|
683 |
|
if ($self->{read_lock}) { # We are a becoming a writer now. So |
684 |
|
# we release the read lock to avoid |
685 |
|
# blocking ourselves. |
686 |
|
$self->{read_lock}->release; |
687 |
|
delete $self->{read_lock}; |
688 |
|
} |
689 |
|
|
690 |
|
# Get the preliminary write lock |
691 |
|
$self->{write_lock} = $lockmgr->lock($self->{file} . '/write') |
692 |
|
or die "Can't lock '$self->{file}/write'"; |
693 |
|
|
694 |
|
# If we actually want to write we must check if there are any |
695 |
|
# readers. The write lock is confirmed if wen cannot find any |
696 |
|
# valid readers. |
697 |
|
|
698 |
|
local *DIR; |
699 |
|
opendir DIR, $lockdir or |
700 |
|
die "Could not opendir '$lockdir': $!"; |
701 |
|
for my $lockfile (grep { -f "$lockdir/$_" } readdir DIR) { |
702 |
|
# Check if the locks are still valid. Since we are protected by |
703 |
|
# a write lock, we could use a plain file. But we want to use |
704 |
|
# the stale testing from LockFile::Simple. |
705 |
|
if (my $lck = $lockmgr->trylock("$lockdir/$lockfile")) { |
706 |
|
warn "Removing stale lockfile '$lockdir/$lockfile'"; |
707 |
|
$lck->release; |
708 |
|
} else { # Found an active reader, rats! |
709 |
|
$self->{write_lock}->release; |
710 |
|
die "Cannot write table '$file' while it's in use"; |
711 |
|
} |
712 |
|
} |
713 |
|
closedir DIR; |
714 |
|
} else { |
715 |
|
# Have a read lock already |
716 |
|
return $self if $self->{read_lock}; |
717 |
|
|
718 |
|
# Get the preliminary write lock to protect the directory |
719 |
|
# operations. If we already have a write lock, it will go. |
720 |
|
|
721 |
|
$self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write') |
722 |
|
or die "Can't lock '$self->{file}/write'"; |
723 |
|
|
724 |
|
# Find a new read slot. Maybe the plain file would be better? |
725 |
|
my $id = time; |
726 |
|
while (-f "$lockdir/$id.lock") { # here assume ".lock" format! |
727 |
|
$id++; |
728 |
|
} |
729 |
|
|
730 |
|
$self->{read_lock} = $lockmgr->lock("$lockdir/$id") |
731 |
|
or die "Can't lock '$lockdir/$id'"; |
732 |
|
|
733 |
|
# We are a reader now. So we release the write lock |
734 |
|
$self->{write_lock}->release; |
735 |
|
delete $self->{write_lock}; |
736 |
|
} |
737 |
|
return $self; |
738 |
|
} |
739 |
|
|
740 |
sub unlock { |
sub unlock { |
741 |
my $self = shift; |
my $self = shift; |
742 |
|
|
758 |
sub DESTROY { |
sub DESTROY { |
759 |
my $self = shift; |
my $self = shift; |
760 |
|
|
761 |
warn "Table handle destroyed without closing it first" |
if ($self->{write_lock} || $self->{read_lock}) { |
762 |
if $self->{write_lock} || $self->{read_lock}; |
warn "Table handle destroyed without closing it first"; |
763 |
|
$self->unlock; |
764 |
|
} |
765 |
} |
} |
766 |
|
|
767 |
sub open_scan { |
sub open_scan { |