/[wait]/trunk/lib/WAIT/Table.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WAIT/Table.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/CPAN/lib/WAIT/Table.pm revision 13 by ulpfr, Fri Apr 28 15:42:44 2000 UTC cvs-head/lib/WAIT/Table.pm revision 41 by laperla, Mon Nov 13 20:25:49 2000 UTC
# Line 4  Line 4 
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: Sun Nov 12 17:51:56 2000
8  # Language        : CPerl  # Language        : CPerl
9  # Update Count    : 56  # 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
# Line 34  use Carp; Line 34  use Carp;
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    
# Line 163  sub new { Line 164  sub new {
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};
# Line 171  sub new { Line 173  sub new {
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";
# Line 188  sub new { Line 195  sub new {
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  }  }
# Line 315  Must be called via C<WAIT::Database::dro Line 323  Must be called via C<WAIT::Database::dro
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";
# Line 373  sub open { Line 391  sub open {
391                           $self->{mode}, 0664, $DB_BTREE);                           $self->{mode}, 0664, $DB_BTREE);
392      }      }
393    }    }
394      
395      $self->getlock($self->{mode});
396      
397    $self;    $self;
398  }  }
399    
# Line 432  sub insert { Line 453  sub insert {
453    my $tuple = join($;, map($parm{$_} || '', @{$self->{attr}}));    my $tuple = join($;, map($parm{$_} || '', @{$self->{attr}}));
454    my $key;    my $key;
455    my @deleted = keys %{$self->{deleted}};    my @deleted = keys %{$self->{deleted}};
456      my $gotkey = 0;
457    
458    if (@deleted) {    if (@deleted) {
459      $key = pop @deleted;      $key = pop @deleted;
460      delete $self->{deleted}->{$key};      delete $self->{deleted}->{$key};
461        # Sanity check
462        if ($key && $key>0) {
463          $gotkey=1;
464    } else {    } else {
465          warn(sprintf("WAIT database inconsistency during insert ".
466                       "key[%s]: Please rebuild index\n",
467                       $key
468                      ));
469        }
470      }
471      unless ($gotkey) {
472      $key = $self->{nextk}++;      $key = $self->{nextk}++;
473    }    }
474    if ($USE_RECNO) {    if ($USE_RECNO) {
# Line 450  sub insert { Line 482  sub insert {
482        if ($key == $self->{nextk}-1) {        if ($key == $self->{nextk}-1) {
483          $self->{nextk}--;          $self->{nextk}--;
484        } else {        } else {
485            # warn "setting key[$key] deleted during insert";
486          $self->{deleted}->{$key}=1;          $self->{deleted}->{$key}=1;
487        }        }
488        my $idx;        my $idx;
# Line 504  sub delete_by_key { Line 537  sub delete_by_key {
537    my $self  = shift;    my $self  = shift;
538    my $key   = shift;    my $key   = shift;
539    
540      unless ($key) {
541        Carp::cluck "Warning: delete_by_key called without key. Looks like a bug in WAIT?";
542        return;
543      }
544    
545    return $self->{deleted}->{$key} if defined $self->{deleted}->{$key};    return $self->{deleted}->{$key} if defined $self->{deleted}->{$key};
546    my %tuple = $self->fetch($key);    my %tuple = $self->fetch($key);
547    for (values %{$self->{indexes}}) {    for (values %{$self->{indexes}}) {
# Line 520  sub delete_by_key { Line 558  sub delete_by_key {
558        }        }
559      }      }
560    }    }
561      # warn "setting key[$key] deleted during delete_by_key";
562    ++$self->{deleted}->{$key};    ++$self->{deleted}->{$key};
563  }  }
564    
565  sub delete {  sub delete {
566    my $self  = shift;    my $self  = shift;
567    my $tkey = $self->have(@_);    my $tkey = $self->have(@_);
568      # warn "tkey[$tkey]\@_[@_]";
569    defined $tkey && $self->delete_by_key($tkey, @_);    defined $tkey && $self->delete_by_key($tkey, @_);
570  }  }
571    
572  sub unpack {  sub unpack {
573    my $self = shift;    my($self, $tuple) = @_;
574    my $tuple = shift;  
575      unless (defined $tuple){
576        # require Carp; # unfortunately gives us "bizarre copy...." :-(((((
577        warn("Debug: somebody called unpack without argument tuple!");
578        return;
579      }
580    
581    my $att;    my $att;
582    my @result;    my @result;
# Line 544  sub unpack { Line 588  sub unpack {
588    @result;    @result;
589  }  }
590    
591    sub set {
592      my ($self, $iattr, $value) = @_;
593      
594      unless ($self->{write_lock}){
595        warn "Cannot set iattr[$iattr] without write lock. Nothing done";
596        return;
597      }
598      for my $att (keys %{$self->{inverted}}) {
599        if ($] > 5.003) {         # avoid bug in perl up to 5.003_05
600          my $idx;
601          for $idx (@{$self->{inverted}->{$att}}) {
602            $idx->set($iattr, $value);
603          }
604        } else {
605          map $_->set($iattr, $value), @{$self->{inverted}->{$att}};
606        }
607      }
608    
609      1;
610    }
611    
612  sub close {  sub close {
613    my $self = shift;    my $self = shift;
614    
615    if (exists $self->{'access'}) {    if (exists $self->{'access'}) {
616      eval {$self->{'access'}->close}; # dont bother if not opened      eval {$self->{'access'}->close}; # dont bother if not opened
617    }    }
618    for (values %{$self->{indexes}}) {    if ($WAIT::Index::VERSION) {
619      $_->close();      for (values %{$self->{indexes}}) {
620          $_->close();
621        }
622    }    }
623    if (defined $self->{inverted}) {    if (defined $self->{inverted} && $WAIT::InvertedIndex::VERSION) {
624        # require WAIT::InvertedIndex; Uli: we can avoid closing indexes:
625        # if WAIT::InvertedIndex has not been loaded, they cannot have
626        # been altered so far
627      my $att;      my $att;
628      for $att (keys %{$self->{inverted}}) {      for $att (keys %{$self->{inverted}}) {
629        if ($] > 5.003) {         # avoid bug in perl up to 5.003_05        if ($] > 5.003) {         # avoid bug in perl up to 5.003_05
# Line 577  sub close { Line 647  sub close {
647      delete $self->{db};      delete $self->{db};
648    }    }
649    
650      $self->unlock;
651      
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      # autoclean cleans on DESTROY, stale sends SIGZERO to the owner
669      #
670      my $lockmgr = LockFile::Simple->make(-autoclean => 1, -stale => 1);
671      my $file    = $self->{file} . '/records';
672      my $lockdir = $self->{file} . '/read';
673    
674      unless (-d $lockdir) {
675        mkdir $lockdir, 0755 or die "Could not mkdir $lockdir: $!";
676      }
677      
678      if ($mode & O_RDWR) {         # Get a write lock.  Release it again
679                                    # and die if there is any valid
680                                    # readers.
681        
682        # Have a write lock already
683        return $self if $self->{write_lock};
684    
685        if ($self->{read_lock}) {   # We are a becoming a writer now. So
686                                    # we release the read lock to avoid
687                                    # blocking ourselves.
688          $self->{read_lock}->release;
689          delete $self->{read_lock};
690        }
691    
692        # Get the preliminary write lock
693        $self->{write_lock} = $lockmgr->lock($self->{file} . '/write')
694          or die "Can't lock '$self->{file}/write'";
695        
696        # If we actually want to write we must check if there are any
697        # readers.  The write lock is confirmed if wen cannot find any
698        # valid readers.
699        
700        local *DIR;
701        opendir DIR, $lockdir or
702          die "Could not opendir '$lockdir': $!";
703        for my $lockfile (grep { -f "$lockdir/$_" } readdir DIR) {
704          # Check if the locks are still valid.  Since we are protected by
705          # a write lock, we could use a plain file.  But we want to use
706          # the stale testing from LockFile::Simple.
707          if (my $lck = $lockmgr->trylock("$lockdir/$lockfile")) {
708            warn "Removing stale lockfile '$lockdir/$lockfile'";
709            $lck->release;
710          } else {                  # Found an active reader, rats!
711            $self->{write_lock}->release;
712            die "Cannot write table '$file' while it's in use";
713          }
714        }
715        closedir DIR;
716      } else {
717        # Have a read lock already
718        return $self if $self->{read_lock};
719    
720        # Get the preliminary write lock to protect the directory
721        # operations.  If we already have a write lock, it will go.
722    
723        $self->{write_lock} ||= $lockmgr->lock($self->{file} . '/write')
724          or die "Can't lock '$self->{file}/write'";
725    
726        # Find a new read slot.  Maybe the plain file would be better?
727        my $id = time;
728        while (-f "$lockdir/$id.lock") { # here assume ".lock" format!
729          $id++;
730        }
731    
732        $self->{read_lock} = $lockmgr->lock("$lockdir/$id")
733          or die "Can't lock '$lockdir/$id'";
734    
735        # We are a reader now. So we release the write lock
736        $self->{write_lock}->release;
737        delete $self->{write_lock};
738      }
739      return $self;
740    }
741    
742    sub unlock {
743      my $self = shift;
744    
745      # Either we have a read or a write lock (or we close the table already)
746      # unless ($self->{read_lock} || $self->{write_lock}) {
747      #   warn "WAIT::Table::unlock: Table aparently hold's no lock"
748      # }
749      if ($self->{write_lock}) {
750        $self->{write_lock}->release();
751        delete $self->{write_lock};
752      }
753      if ($self->{read_lock}) {
754        $self->{read_lock}->release();
755        delete $self->{read_lock};
756      }
757    
758    }
759    
760  sub DESTROY {  sub DESTROY {
761    my $self = shift;    my $self = shift;
762    
763    warn "Table handle destroyed without closing it first"    if ($self->{write_lock} || $self->{read_lock}) {
764      if $self->{db} and $self->{mode}&O_RDWR;      warn "Table handle destroyed without closing it first";
765        $self->unlock;
766      }
767  }  }
768    
769  sub open_scan {  sub open_scan {
# Line 642  sub intervall { Line 821  sub intervall {
821  }  }
822    
823  sub search {  sub search {
824    my $self = shift;    my $self  = shift;
825    my $attr = shift;    my ($query, $attr, $cont, $raw);
826    my $cont = shift;    if (ref $_[0]) {
827    my $raw  = shift;      $query = shift;
828      
829        $attr = $query->{attr};
830        $cont = $query->{cont};
831        $raw  = $query->{raw};
832      } else {
833        require Carp;
834        Carp::cluck("Using three argument search interface is deprecated, use hashref interface instead");
835        $attr = shift;
836        $cont = shift;
837        $raw  = shift;
838        $query = {
839                  attr => $attr,
840                  cont => $cont,
841                  raw  => $raw,
842                 };
843      }
844    
845    my %result;    my %result;
846    
847    defined $self->{db} or $self->open; # require layout    defined $self->{db} or $self->open; # require layout
# Line 655  sub search { Line 851  sub search {
851        my $name = $_->name;        my $name = $_->name;
852        if (exists $raw->{$name} and @{$raw->{$name}}) {        if (exists $raw->{$name} and @{$raw->{$name}}) {
853          my $scale = 1/scalar(@{$raw->{$name}});          my $scale = 1/scalar(@{$raw->{$name}});
854          my %r = $_->search_raw(@{$raw->{$name}});          my %r = $_->search_raw($query, @{$raw->{$name}});
855          my ($key, $val);          my ($key, $val);
856          while (($key, $val) = each %r) {          while (($key, $val) = each %r) {
857            if (exists $result{$key}) {            if (exists $result{$key}) {
# Line 669  sub search { Line 865  sub search {
865    }    }
866    if (defined $cont and $cont ne '') {    if (defined $cont and $cont ne '') {
867      for (@{$self->{inverted}->{$attr}}) {      for (@{$self->{inverted}->{$attr}}) {
868        my %r = $_->search($cont);        my %r = $_->search($query, $cont);
869        my ($key, $val);        my ($key, $val);
870        while (($key, $val) = each %r) {        while (($key, $val) = each %r) {
871          if (exists $result{$key}) {          if (exists $result{$key}) {

Legend:
Removed from v.13  
changed lines
  Added in v.41

  ViewVC Help
Powered by ViewVC 1.1.26