[Bio] / Sprout / ERDBLoadGroup.pm Repository:
ViewVC logotype

Diff of /Sprout/ERDBLoadGroup.pm

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

revision 1.6, Thu Apr 2 01:37:07 2009 UTC revision 1.8, Tue Jun 30 19:53:01 2009 UTC
# Line 59  Line 59 
59  names.  names.
60    
61  Any working or status files generated by a subclass should have a prefix of C<dt>-something.  Any working or status files generated by a subclass should have a prefix of C<dt>-something.
62  This will insure they are deleted by the C<clear> option of [[ERDBGeneratorPl]].  This will insure they are deleted by the C<clear> option of L<ERDBGenerator.pl>.
63    
64  The fields in this object are as follows.  The fields in this object are as follows.
65    
# Line 67  Line 67 
67    
68  =item db  =item db
69    
70  [[ErdbPm]] object for accessing the target database  L<ERDB> object for accessing the target database
71    
72  =item directory  =item directory
73    
# Line 87  Line 87 
87    
88  =item loaders  =item loaders
89    
90  hash mapping the names of the group's tables to [[ERDBGeneratePm]] objects  hash mapping the names of the group's tables to L<ERDBGenerate> objects
91    
92  =item stats  =item stats
93    
# Line 123  Line 123 
123    
124  =item db  =item db
125    
126  The [[ErdbPm]] object for the database being loaded.  The L<ERDB> object for the database being loaded.
127    
128  =item options  =item options
129    
# Line 171  Line 171 
171      return $retVal;      return $retVal;
172  }  }
173    
174    =head3 TRAILER
175    
176    This is a string constant that always compares high against real data.
177    
178    =cut
179    
180    use constant TRAILER => "\xFF";
181    
182  =head2 Subclass Methods  =head2 Subclass Methods
183    
184  =head3 Put  =head3 Put
# Line 453  Line 461 
461      return $self->{db};      return $self->{db};
462  }  }
463    
464    =head3 FilterRelationship
465    
466        my $stats = $edbl->FilterRelationship($type => $relationshipName);
467    
468    This method will compare a relationship's load file to a target entity
469    file and remove rows for which no target entity exists. This is useful
470    when a relationship and entity are created by different load groups, so
471    there is no opportunity in the generator to verify that the relationship
472    records are relevant to this database. Typically, this method is called
473    during post-processing, between generation by L<ERDBGenerator.pl> and the
474    actual database table loads.
475    
476    =over 4
477    
478    =item type
479    
480    Relevant relationship direction-- C<from> or C<to>.
481    
482    =item relationshipName
483    
484    Name of the relationship whose load file is to be filtered.
485    
486    =item RETURN
487    
488    
489    
490    =back
491    
492    =cut
493    
494    sub FilterRelationship {
495        # Get the parameters.
496        my ($self, $type, $relationshipName) = @_;
497        # Declare the return variable.
498        my $retVal = Stats->new();
499        # Get the database object.
500        my $erdb = $self->db();
501        # Get the relationship's descriptor. We need this to find the relevant entity.
502        my $relData = $erdb->FindRelationship($relationshipName);
503        if (! defined $relData) {
504            Confess("Relationship $relationshipName not found in this database.");
505        } else {
506            # We have the relationship, so get the name of the target entity.
507            my $entityName = $relData->{$type};
508            # We need to find where the entity's ID will be in the relationship's
509            # load file. FROM is always first, then TO.
510            my $fieldPos = ($type eq 'from' ? 1 : 2);
511            Trace("Filtering relationship $relationshipName against $entityName using field $type($fieldPos).") if T(3);
512            # We will be reading from the entity and relationship load files in
513            # parallel, with both sorted by the entity ID. The output will be
514            # sort-piped to a temporary file.
515            my $relationshipFileName = ERDBGenerate::CreateFileName($relationshipName,
516                                                                    undef, 'data');
517            my $relationshipTempName = ERDBGenerate::CreateFileName($relationshipName,
518                                                                    undef, 'temp');
519            my $entityFileName =       ERDBGenerate::CreateFileName($entityName,
520                                                                    undef, 'data');
521            # Get the desired sort for the relationship file. We use this for
522            # the relationship output.
523            my $sortOut = $erdb->SortNeeded($relationshipName);
524            # Now we can open our files.
525            my $rih = Open(undef, "sort -k$fieldPos,$fieldPos <$relationshipFileName |");
526            my $eih = Open(undef, "sort -k1,1 <$entityFileName |");
527            my $roh = Open(undef, "| $sortOut >$relationshipTempName");
528            # Convert the field position from 1-based (for the sort) to 0-based (for PERL).
529            $fieldPos--;
530            # Get the first record in each file.
531            my ($rKey, $relRecord) = GetRecord($rih, $fieldPos);
532            my ($eKey) = GetRecord($eih, 0);
533            # Loop until we run out of records in the relationship file.
534            while ($rKey lt TRAILER) {
535                # Roll the entity file forward until we find the spot for
536                # this relationship.
537                while ($rKey gt $eKey) {
538                    ($eKey) = GetRecord($eih, 0);
539                }
540                # If we have a match, we output the relationship record.
541                # At this point eKey could be TRAILER, but rKey cannot, because
542                # it hasn't changed since the while condition was evaluated.
543                if ($eKey eq $rKey) {
544                    Tracer::PutLine($roh, $relRecord);
545                    $retVal->Add("kept-$relationshipName" => 1);
546                } else {
547                    $retVal->Add("rejected-$relationshipName" => 1);
548                }
549                # Get the next relationship record.
550                ($rKey, $relRecord) = GetRecord($rih, $fieldPos);
551            }
552            # Now we close everything and move the temp file over the top of the
553            # real relationship file.
554            Trace("Closing files.") if T(3);
555            close $rih;
556            close $eih;
557            close $roh;
558            Trace("Renaming filtered relationship file for $relationshipName.") if T(3);
559            unlink $relationshipFileName;
560            rename $relationshipTempName, $relationshipFileName;
561        }
562        # Return the result.
563        return $retVal;
564    }
565    
566    =head3 GetTables
567    
568        my @tables = ERDBLoadGroup::GetTables($erdb, $group);
569    
570    or
571    
572        my @tables = $edbl->GetTables();
573    
574    Return the list of tables belonging to the specified load group.
575    
576    =over 4
577    
578    =item erdb
579    
580    L<ERDB> subclass object for the relevant database.
581    
582    =item group
583    
584    Name of the relevant group.
585    
586    =item RETURN
587    
588    Returns a list of a tables loaded by the specified group.
589    
590    =back
591    
592    =cut
593    
594    sub GetTables {
595        # Get the parameters.
596        my ($self, $group) = @_;
597        # We need a loader. If the caller gave us an ERDB object instead, we need to
598        # convert it.
599        if (! $self->isa(__PACKAGE__)) {
600            $self = $self->Loader($group, undef, {});
601        }
602        # Extract the list of tables.
603        my @retVal = @{$self->{tables}};
604        # Return the result.
605        return @retVal;
606    }
607    
608  =head2 Internal Methods  =head2 Internal Methods
609    
610  =head3 ProcessSection  =head3 ProcessSection
# Line 522  Line 674 
674          };          };
675          # Did it work?          # Did it work?
676          if ($@) {          if ($@) {
677              # No, so emit an error message and abort all the loaders.              # No, so we need to emit an error message and abort all the loaders.
678              $self->{stats}->AddMessage("Error loading section $section: $@");              # First, we need to clean the new-line from the message (if any).
679                my $msg = $@;
680                chomp $msg;
681                # Figure out what we were doing at the time of the error.
682                my $place = "Error in section $section";
683              if (defined $self->{lastKey}) {              if (defined $self->{lastKey}) {
684                  $self->{stats}->AddMessage("Error occurred while processing \"$self->{lastKey}\".");                  $place .= "($self->{lastKey})";
685              }              }
686                # Format the message and denote we have a section failure.
687                $self->{stats}->AddMessage("$place: $msg");
688              $self->Add("section-errors" => 1);              $self->Add("section-errors" => 1);
689                # Abort the loaders.
690              for my $loader (values %{$self->{loaders}}) {              for my $loader (values %{$self->{loaders}}) {
691                  $loader->Abort();                  $loader->Abort();
692              }              }
693          } else {          } else {
694              # Yes! Finish all the loaders.              # It did work! Finish all the loaders.
695              for my $loader (values %{$self->{loaders}}) {              for my $loader (values %{$self->{loaders}}) {
696                  $loader->Finish();                  $loader->Finish();
697              }              }
# Line 559  Line 718 
718      return $self->{stats}->Show();      return $self->{stats}->Show();
719  }  }
720    
721    =head3 AccumulateStats
722    
723        $edbl->AccumulateStats($stats);
724    
725    Add this load's statistics into the caller-specified statistics object.
726    
727    =over 4
728    
729    =item stats
730    
731    L<Stats> object into which this load's statistics will be accumulated.
732    
733    =back
734    
735    =cut
736    
737    sub AccumulateStats {
738        # Get the parameters.
739        my ($self, $stats) = @_;
740        # Roll up our statistics in the caller's object.
741        $stats->Accumulate($self->{stats});
742    }
743    
744    
745  =head3 GetGroupHash  =head3 GetGroupHash
746    
747      my $groupHash = ERDBLoadGroup::GetGroupHash($erdb);      my $groupHash = ERDBLoadGroup::GetGroupHash($erdb);
# Line 571  Line 754 
754    
755  =item erdb  =item erdb
756    
757  [[ErdbPm]] database whose load information is desired.  L<ERDB> database whose load information is desired.
758    
759  =item RETURN  =item RETURN
760    
# Line 596  Line 779 
779      return $retVal;      return $retVal;
780  }  }
781    
 =head3 GetTables  
   
     my @tables = ERDBLoadGroup::GetTables($group);  
   
 Return the list of tables belonging to the specified load group.  
   
 =over 4  
   
 =item erdb  
   
 Return the list of tables for the specified load group.  
   
 =item group  
   
 Name of relevant group.  
   
 =item RETURN  
   
 Returns a list of a tables loaded by the specified group.  
   
 =back  
   
 =cut  
   
 sub GetTables {  
     # Get the parameters.  
     my ($erdb, $group) = @_;  
     # Create a loader for the specified group.  
     my $loader = $erdb->Loader($group, undef, {});  
     # Extract the list of tables.  
     my @retVal = @{$loader->{tables}};  
     # Return the result.  
     return @retVal;  
 }  
   
   
782  =head3 ComputeGroups  =head3 ComputeGroups
783    
784      my @groupList = ERDBLoadGroup::ComputeGroups($erdb, \@groups);      my @groupList = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
# Line 642  Line 789 
789    
790  =item erdb  =item erdb
791    
792  [[ErdbPm]] object for the database being loaded.  L<ERDB> object for the database being loaded.
793    
794  =item groups  =item groups
795    
# Line 706  Line 853 
853      my $fileName = ERDBLoadGroup::KillFileName($erdb, $directory);      my $fileName = ERDBLoadGroup::KillFileName($erdb, $directory);
854    
855  Compute the kill file name for the specified database in the specified  Compute the kill file name for the specified database in the specified
856  directory. When the [[ERDBGeneratorPl]] script sees the kill file, it will  directory. When the L<ERDBGenerator.pl> script sees the kill file, it will
857  terminate itself at the end of the current section.  terminate itself at the end of the current section.
858    
859  =over 4  =over 4
# Line 792  Line 939 
939      return $retVal;      return $retVal;
940  }  }
941    
942    =head3 GetRecord
943    
944        my ($key, $record) = ERDBLoadGroup::GetRelRecord($ih, $fieldPos);
945    
946    Read the next record from a tab-delimited file, returning the key field
947    in the specified position and a reference to a list of all the fields. If
948    end-of-file has been reached, the value TRAILER and an empty list
949    reference will be returned.
950    
951    =over 4
952    
953    =item ih
954    
955    Open handle of the input file containing the records.
956    
957    =item fieldPos
958    
959    Ordinal position in the record of the desired key field. This should be
960    C<0> for the first field, C<1> for the second, and so forth.
961    
962    =item RETURN
963    
964    Returns a two-element list, the first of which contains the indicated key
965    field and the second of which is a reference to a list of all fields in the
966    record (including the key). If end-of-file is reached, the returned key will
967    be TRAILER and the returned list will be empty.
968    
969    =back
970    
971    =cut
972    
973    sub GetRecord {
974        # Get the parameters.
975        my ($ih, $fieldPos) = @_;
976        # Declare the return variables.
977        my ($key, $record) = (TRAILER, []);
978        # Only proceed if we're NOT at end of file.
979        if (! eof $ih) {
980            # Read the record.
981            my @fields = Tracer::GetLine($ih);
982            # Extract the key and form the list.
983            $key = $fields[$fieldPos];
984            $record = \@fields;
985        }
986        # Return the results.
987        return ($key, $record);
988    }
989    
990  =head2 Virtual Methods  =head2 Virtual Methods
991    
# Line 809  Line 1003 
1003      Confess("Pure virtual method Generate called.");      Confess("Pure virtual method Generate called.");
1004  }  }
1005    
1006    =head3 PostProcess
1007    
1008        my $stats = $edbl->PostProcess();
1009    
1010    Post-process the load files for this group. This method is called after all
1011    of the load files have been assembled, but before anything is actually loaded.
1012    It allows a final pass through the data to do filtering between groups or to
1013    accumulate totals and counters. The default is to do nothing.
1014    
1015    This method returns a statistics object describing the post-processing activity,
1016    or an undefined value if nothing happened.
1017    
1018    =cut
1019    
1020    sub PostProcess { }
1021    
1022  1;  1;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.8

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3