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 May 30 20:42:30 1999 |
# Last Modified On: Tue Nov 14 16:19:17 2000 |
8 |
# Language : CPerl |
# Language : CPerl |
9 |
# Update Count : 56 |
# Update Count : 149 |
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 |
34 |
# use autouse Carp => qw( croak($) ); |
# use autouse Carp => qw( croak($) ); |
35 |
use DB_File; |
use DB_File; |
36 |
use Fcntl; |
use Fcntl; |
37 |
|
use LockFile::Simple (); |
38 |
|
|
39 |
my $USE_RECNO = 0; |
my $USE_RECNO = 0; |
40 |
|
|
164 |
} elsif (!mkdir($self->{file}, 0775)) { |
} elsif (!mkdir($self->{file}, 0775)) { |
165 |
croak "Could not 'mkdir $self->{file}': $!\n"; |
croak "Could not 'mkdir $self->{file}': $!\n"; |
166 |
} |
} |
167 |
|
|
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"; |
195 |
} |
} |
196 |
$self->create_inverted_index(attribute => $att, pipeline => \@spec, @opt); |
$self->create_inverted_index(attribute => $att, pipeline => \@spec, @opt); |
197 |
} |
} |
198 |
|
|
199 |
$self; |
$self; |
200 |
# end of backwarn compatibility stuff |
# end of backwarn compatibility stuff |
201 |
} |
} |
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; |
344 |
! (!-e $file or rmdir $file); |
! (!-e $file or rmdir $file); |
345 |
} else { |
} else { |
346 |
croak ref($self)."::drop called directly"; |
croak ref($self)."::drop called directly"; |
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 |
|
|
399 |
$self; |
$self; |
400 |
} |
} |
401 |
|
|
455 |
my $tuple = join($;, map($parm{$_} || '', @{$self->{attr}})); |
my $tuple = join($;, map($parm{$_} || '', @{$self->{attr}})); |
456 |
my $key; |
my $key; |
457 |
my @deleted = keys %{$self->{deleted}}; |
my @deleted = keys %{$self->{deleted}}; |
458 |
|
my $gotkey = 0; |
459 |
|
|
460 |
if (@deleted) { |
if (@deleted) { |
461 |
$key = pop @deleted; |
$key = pop @deleted; |
462 |
delete $self->{deleted}->{$key}; |
delete $self->{deleted}->{$key}; |
463 |
|
# Sanity check |
464 |
|
if ($key && $key>0) { |
465 |
|
$gotkey=1; |
466 |
} else { |
} else { |
467 |
|
warn(sprintf("WAIT database inconsistency during insert ". |
468 |
|
"key[%s]: Please rebuild index\n", |
469 |
|
$key |
470 |
|
)); |
471 |
|
} |
472 |
|
} |
473 |
|
unless ($gotkey) { |
474 |
$key = $self->{nextk}++; |
$key = $self->{nextk}++; |
475 |
} |
} |
476 |
if ($USE_RECNO) { |
if ($USE_RECNO) { |
484 |
if ($key == $self->{nextk}-1) { |
if ($key == $self->{nextk}-1) { |
485 |
$self->{nextk}--; |
$self->{nextk}--; |
486 |
} else { |
} else { |
487 |
|
# warn "setting key[$key] deleted during insert"; |
488 |
$self->{deleted}->{$key}=1; |
$self->{deleted}->{$key}=1; |
489 |
} |
} |
490 |
my $idx; |
my $idx; |
539 |
my $self = shift; |
my $self = shift; |
540 |
my $key = shift; |
my $key = shift; |
541 |
|
|
542 |
|
unless ($key) { |
543 |
|
Carp::cluck "Warning: delete_by_key called without key. Looks like a bug in WAIT?"; |
544 |
|
return; |
545 |
|
} |
546 |
|
|
547 |
return $self->{deleted}->{$key} if defined $self->{deleted}->{$key}; |
return $self->{deleted}->{$key} if defined $self->{deleted}->{$key}; |
548 |
my %tuple = $self->fetch($key); |
my %tuple = $self->fetch($key); |
549 |
for (values %{$self->{indexes}}) { |
for (values %{$self->{indexes}}) { |
560 |
} |
} |
561 |
} |
} |
562 |
} |
} |
563 |
|
# warn "setting key[$key] deleted during delete_by_key"; |
564 |
++$self->{deleted}->{$key}; |
++$self->{deleted}->{$key}; |
565 |
} |
} |
566 |
|
|
567 |
sub delete { |
sub delete { |
568 |
my $self = shift; |
my $self = shift; |
569 |
my $tkey = $self->have(@_); |
my $tkey = $self->have(@_); |
570 |
|
# warn "tkey[$tkey]\@_[@_]"; |
571 |
defined $tkey && $self->delete_by_key($tkey, @_); |
defined $tkey && $self->delete_by_key($tkey, @_); |
572 |
} |
} |
573 |
|
|
574 |
sub unpack { |
sub unpack { |
575 |
my $self = shift; |
my($self, $tuple) = @_; |
576 |
my $tuple = shift; |
|
577 |
|
unless (defined $tuple){ |
578 |
|
# require Carp; # unfortunately gives us "bizarre copy...." :-((((( |
579 |
|
warn("Debug: somebody called unpack without argument tuple!"); |
580 |
|
return; |
581 |
|
} |
582 |
|
|
583 |
my $att; |
my $att; |
584 |
my @result; |
my @result; |
590 |
@result; |
@result; |
591 |
} |
} |
592 |
|
|
593 |
|
sub set { |
594 |
|
my ($self, $iattr, $value) = @_; |
595 |
|
|
596 |
|
unless ($self->{write_lock}){ |
597 |
|
warn "Cannot set iattr[$iattr] without write lock. Nothing done"; |
598 |
|
return; |
599 |
|
} |
600 |
|
for my $att (keys %{$self->{inverted}}) { |
601 |
|
if ($] > 5.003) { # avoid bug in perl up to 5.003_05 |
602 |
|
my $idx; |
603 |
|
for $idx (@{$self->{inverted}->{$att}}) { |
604 |
|
$idx->set($iattr, $value); |
605 |
|
} |
606 |
|
} else { |
607 |
|
map $_->set($iattr, $value), @{$self->{inverted}->{$att}}; |
608 |
|
} |
609 |
|
} |
610 |
|
|
611 |
|
1; |
612 |
|
} |
613 |
|
|
614 |
sub close { |
sub close { |
615 |
my $self = shift; |
my $self = shift; |
616 |
|
|
617 |
if (exists $self->{'access'}) { |
if (exists $self->{'access'}) { |
618 |
eval {$self->{'access'}->close}; # dont bother if not opened |
eval {$self->{'access'}->close}; # dont bother if not opened |
619 |
} |
} |
620 |
for (values %{$self->{indexes}}) { |
if ($WAIT::Index::VERSION) { |
621 |
$_->close(); |
for (values %{$self->{indexes}}) { |
622 |
|
$_->close(); |
623 |
|
} |
624 |
} |
} |
625 |
if (defined $self->{inverted}) { |
if (defined $self->{inverted} && $WAIT::InvertedIndex::VERSION) { |
626 |
|
# require WAIT::InvertedIndex; Uli: we can avoid closing indexes: |
627 |
|
# if WAIT::InvertedIndex has not been loaded, they cannot have |
628 |
|
# been altered so far |
629 |
my $att; |
my $att; |
630 |
for $att (keys %{$self->{inverted}}) { |
for $att (keys %{$self->{inverted}}) { |
631 |
if ($] > 5.003) { # avoid bug in perl up to 5.003_05 |
if ($] > 5.003) { # avoid bug in perl up to 5.003_05 |
649 |
delete $self->{db}; |
delete $self->{db}; |
650 |
} |
} |
651 |
|
|
652 |
|
$self->unlock; |
653 |
|
|
654 |
1; |
1; |
655 |
} |
} |
656 |
|
|
657 |
|
# Locking |
658 |
|
# |
659 |
|
# We allow multiple readers to coexists. But write access excludes |
660 |
|
# all read access and vice versa. In practice read access on tables |
661 |
|
# open for writing will mostly work ;-) |
662 |
|
|
663 |
|
# If a "write" lock is requested, an existing "read" lock will be |
664 |
|
# released. If a "read" lock ist requested, an existing "write" lock |
665 |
|
# will be released. Requiring a lock already hold has no effect. |
666 |
|
|
667 |
|
sub getlock { |
668 |
|
my ($self, $mode) = @_; |
669 |
|
|
670 |
|
# autoclean cleans on DESTROY, stale sends SIGZERO to the owner |
671 |
|
# |
672 |
|
my $lockmgr = LockFile::Simple->make(-autoclean => 1, -stale => 1); |
673 |
|
my $file = $self->{file} . '/records'; |
674 |
|
my $lockdir = $self->{file} . '/read'; |
675 |
|
|
676 |
|
unless (-d $lockdir) { |
677 |
|
mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!"; |
678 |
|
} |
679 |
|
|
680 |
|
if ($mode & O_RDWR) { # Get a write lock. Release it again |
681 |
|
# and die if there is any valid |
682 |
|
# readers. |
683 |
|
|
684 |
|
# Have a write lock already |
685 |
|
return $self if $self->{write_lock}; |
686 |
|
|
687 |
|
if ($self->{read_lock}) { # We are a becoming a writer now. So |
688 |
|
# we release the read lock to avoid |
689 |
|
# blocking ourselves. |
690 |
|
$self->{read_lock}->release; |
691 |
|
delete $self->{read_lock}; |
692 |
|
} |
693 |
|
|
694 |
|
# Get the preliminary write lock |
695 |
|
$self->{write_lock} = $lockmgr->lock($self->{file} . '/write') |
696 |
|
or die "Can't lock '$self->{file}/write'"; |
697 |
|
|
698 |
|
# If we actually want to write we must check if there are any |
699 |
|
# readers. The write lock is confirmed if wen cannot find any |
700 |
|
# valid readers. |
701 |
|
|
702 |
|
local *DIR; |
703 |
|
opendir DIR, $lockdir or |
704 |
|
die "Could not opendir '$lockdir': $!"; |
705 |
|
for my $lockfile (grep { -f "$lockdir/$_" } readdir DIR) { |
706 |
|
# Check if the locks are still valid. Since we are protected by |
707 |
|
# a write lock, we could use a plain file. But we want to use |
708 |
|
# the stale testing from LockFile::Simple. |
709 |
|
if (my $lck = $lockmgr->trylock("$lockdir/$lockfile")) { |
710 |
|
warn "Removing stale lockfile '$lockdir/$lockfile'"; |
711 |
|
$lck->release; |
712 |
|
} else { # Found an active reader, rats! |
713 |
|
$self->{write_lock}->release; |
714 |
|
die "Cannot write table '$file' while it's in use"; |
715 |
|
} |
716 |
|
} |
717 |
|
closedir DIR; |
718 |
|
} else { |
719 |
|
# Have a read lock already |
720 |
|
return $self if $self->{read_lock}; |
721 |
|
|
722 |
|
# Get the preliminary write lock to protect the directory |
723 |
|
# operations. If we already have a write lock, it will go. |
724 |
|
|
725 |
|
$self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write') |
726 |
|
or die "Can't lock '$self->{file}/write'"; |
727 |
|
|
728 |
|
# Find a new read slot. Maybe the plain file would be better? |
729 |
|
my $id = time; |
730 |
|
while (-f "$lockdir/$id.lock") { # here assume ".lock" format! |
731 |
|
$id++; |
732 |
|
} |
733 |
|
|
734 |
|
$self->{read_lock} = $lockmgr->lock("$lockdir/$id") |
735 |
|
or die "Can't lock '$lockdir/$id'"; |
736 |
|
|
737 |
|
# We are a reader now. So we release the write lock |
738 |
|
$self->{write_lock}->release; |
739 |
|
delete $self->{write_lock}; |
740 |
|
} |
741 |
|
return $self; |
742 |
|
} |
743 |
|
|
744 |
|
sub unlock { |
745 |
|
my $self = shift; |
746 |
|
|
747 |
|
# Either we have a read or a write lock (or we close the table already) |
748 |
|
# unless ($self->{read_lock} || $self->{write_lock}) { |
749 |
|
# warn "WAIT::Table::unlock: Table aparently hold's no lock" |
750 |
|
# } |
751 |
|
if ($self->{write_lock}) { |
752 |
|
$self->{write_lock}->release(); |
753 |
|
delete $self->{write_lock}; |
754 |
|
} |
755 |
|
if ($self->{read_lock}) { |
756 |
|
$self->{read_lock}->release(); |
757 |
|
delete $self->{read_lock}; |
758 |
|
} |
759 |
|
|
760 |
|
} |
761 |
|
|
762 |
sub DESTROY { |
sub DESTROY { |
763 |
my $self = shift; |
my $self = shift; |
764 |
|
|
765 |
warn "Table handle destroyed without closing it first" |
if ($self->{write_lock} || $self->{read_lock}) { |
766 |
if $self->{db} and $self->{mode}&O_RDWR; |
warn "Table handle destroyed without closing it first"; |
767 |
|
$self->unlock; |
768 |
|
} |
769 |
} |
} |
770 |
|
|
771 |
sub open_scan { |
sub open_scan { |
823 |
} |
} |
824 |
|
|
825 |
sub search { |
sub search { |
826 |
my $self = shift; |
my $self = shift; |
827 |
my $attr = shift; |
my ($query, $attr, $cont, $raw); |
828 |
my $cont = shift; |
if (ref $_[0]) { |
829 |
my $raw = shift; |
$query = shift; |
830 |
|
|
831 |
|
$attr = $query->{attr}; |
832 |
|
$cont = $query->{cont}; |
833 |
|
$raw = $query->{raw}; |
834 |
|
} else { |
835 |
|
require Carp; |
836 |
|
Carp::cluck("Using three argument search interface is deprecated, use hashref interface instead"); |
837 |
|
$attr = shift; |
838 |
|
$cont = shift; |
839 |
|
$raw = shift; |
840 |
|
$query = { |
841 |
|
attr => $attr, |
842 |
|
cont => $cont, |
843 |
|
raw => $raw, |
844 |
|
}; |
845 |
|
} |
846 |
|
|
847 |
my %result; |
my %result; |
848 |
|
|
849 |
defined $self->{db} or $self->open; # require layout |
defined $self->{db} or $self->open; # require layout |
853 |
my $name = $_->name; |
my $name = $_->name; |
854 |
if (exists $raw->{$name} and @{$raw->{$name}}) { |
if (exists $raw->{$name} and @{$raw->{$name}}) { |
855 |
my $scale = 1/scalar(@{$raw->{$name}}); |
my $scale = 1/scalar(@{$raw->{$name}}); |
856 |
my %r = $_->search_raw(@{$raw->{$name}}); |
my %r = $_->search_raw($query, @{$raw->{$name}}); |
857 |
my ($key, $val); |
my ($key, $val); |
858 |
while (($key, $val) = each %r) { |
while (($key, $val) = each %r) { |
859 |
if (exists $result{$key}) { |
if (exists $result{$key}) { |
867 |
} |
} |
868 |
if (defined $cont and $cont ne '') { |
if (defined $cont and $cont ne '') { |
869 |
for (@{$self->{inverted}->{$attr}}) { |
for (@{$self->{inverted}->{$attr}}) { |
870 |
my %r = $_->search($cont); |
my %r = $_->search($query, $cont); |
871 |
my ($key, $val); |
my ($key, $val); |
872 |
while (($key, $val) = each %r) { |
while (($key, $val) = each %r) { |
873 |
if (exists $result{$key}) { |
if (exists $result{$key}) { |