[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.3, Thu Oct 2 16:32:42 2008 UTC revision 1.6, Thu Apr 2 01:37:07 2009 UTC
# Line 77  Line 77 
77    
78  name of this load group  name of this load group
79    
80    =item label
81    
82    name of this worker process
83    
84  =item lastKey  =item lastKey
85    
86  ID of the last major object processed  ID of the last major object processed
# Line 111  Line 115 
115    
116  =head3 new  =head3 new
117    
118      my $edbl = ERDBLoadGroup->new($source, $db, $directory, $options, @tables);      my $edbl = ERDBLoadGroup->new($db, $directory, $options, @tables);
119    
120  Construct a new ERDBLoadGroup object. The following parameters are expected:  Construct a new ERDBLoadGroup object. The following parameters are expected:
121    
122  =over 4  =over 4
123    
 =item source  
   
 The object to be used by the subclass to access the source data. If this parameter  
 is undefined, the source object will be retrieved from the database object as soon  
 as the client calls the L</source> method.  
   
124  =item db  =item db
125    
126  The [[ErdbPm]] object for the database being loaded.  The [[ErdbPm]] object for the database being loaded.
# Line 142  Line 140 
140    
141  sub new {  sub new {
142      # Get the parameters.      # Get the parameters.
143      my ($class, $source, $db, $options, @tables) = @_;      my ($class, $db, $options, @tables) = @_;
144      # Create a statistics object      # Create a statistics object
145      my $stats = Stats->new();      my $stats = Stats->new();
146      # Compute the group name from the class name. It is the first word in      # Compute the group name from the class name. It is the first word in
# Line 160  Line 158 
158                      directory => $directory,                      directory => $directory,
159                      group => $group,                      group => $group,
160                      stats => $stats,                      stats => $stats,
161                      source => $source,                      source => undef,
162                        label => ($options->{label} || $$),
163                      lastKey => undef,                      lastKey => undef,
164                      loaders => {},                      loaders => {},
165                      tables => \@tables,                      tables => \@tables,
# Line 214  Line 213 
213      }      }
214  }  }
215    
216    =head3 PutE
217    
218        $edbl->PutE($table => $id, %fields);
219    
220    Place an entity-based table record in a load file. The first field
221    specified after the table name is the ID.
222    
223    =over 4
224    
225    =item table
226    
227    Name of the relevant table.
228    
229    =item id
230    
231    ID of the relevant entity.
232    
233    =item fields
234    
235    Hash mapping field names to values.
236    
237    =back
238    
239    =cut
240    
241    sub PutE {
242        # Get the parameters.
243        my ($self, $table, $id, %fields) = @_;
244        # Put the record.
245        $self->Put($table, id => $id, %fields);
246        # Record that we've done a putE.
247        $self->Add(putE => 1);
248    }
249    
250    =head3 PutR
251    
252        $edbl->PutR($table => $from, $to, %fields);
253    
254    Place a relationship record in a load file. The first two fields
255    specified after the table name are the from-link and the to-link,
256    respectively.
257    
258    =over 4
259    
260    =item table
261    
262    Name of the relevant relationship.
263    
264    =item from
265    
266    ID of the from-entity.
267    
268    =item to
269    
270    ID of the to-entity.
271    
272    =item fields
273    
274    Hash mapping field names to field values.
275    
276    =back
277    
278    =cut
279    
280    sub PutR {
281        # Get the parameters.
282        my ($self, $table, $from, $to, %fields) = @_;
283        # Put the record.
284        $self->Put($table, 'from-link' => $from, 'to-link' => $to, %fields);
285        # Record that we've done a PutR.
286        $self->Add(putR => 1);
287    }
288    
289    
290  =head3 Add  =head3 Add
291    
292      $edbl->Add($statName => $count);      $edbl->Add($statName => $count);
# Line 243  Line 316 
316      $self->{stats}->Add($statName => $count);      $self->{stats}->Add($statName => $count);
317  }  }
318    
319    =head3 AddWarning
320    
321        $edbl->AddWarning($errorType => $message);
322    
323    Record a warning. Warnings indicate possible errors in the incoming data.
324    The first warning of a specified type is added as a message to the load
325    statistic. All warnings are also traced at level 3.
326    
327    =over 4
328    
329    =item errorType
330    
331    Type of error indicated by the warning. This is used as the label when the
332    warning is counted in the statistics object.
333    
334    =item message
335    
336    Message describing the reason for the warning.
337    
338    =back
339    
340    =cut
341    
342    sub AddWarning {
343        # Get the parameters.
344        my ($self, $errorType, $message) = @_;
345        # Count the warning.
346        my $count = $self->Add($errorType);
347        # Is this the first one of this type?
348        if ($count == 1) {
349            # Yes, add it to the messages for the end.
350            $self->{stats}->AddMessage($errorType);
351        } else {
352            # No, just trace it.
353            Trace("Data warning: $message") if T(3);
354        }
355    }
356    
357  =head3 Track  =head3 Track
358    
359      $edbl->Track($statName => $key, $period);      $edbl->Track($statName => $key, $period);
# Line 263  Line 374 
374  =item statName  =item statName
375    
376  Name of the statistic to be incremented. This should be a plural noun  Name of the statistic to be incremented. This should be a plural noun
377  describing the object whose kep is coming in.  describing the object whose key is coming in.
378    
379  =item key  =item key
380    
# Line 288  Line 399 
399      # Do we need to output a progress message?      # Do we need to output a progress message?
400      if ($period && T(3) && ($newValue % $period == 0)) {      if ($period && T(3) && ($newValue % $period == 0)) {
401          # Yes.          # Yes.
402          Trace("$newValue $statName processed for $self->{group} group.");          MemTrace("$newValue $statName processed by $self->{label} for $self->{group} group.");
403      }      }
404  }  }
405    
# Line 375  Line 486 
486      $self->{section} = $section;      $self->{section} = $section;
487      # Get the database object.      # Get the database object.
488      my $db = $self->db();      my $db = $self->db();
     # Start a timer and protect ourselves from errors.  
     my $startTime = time();  
     eval {  
489          # Get the list of tables for this group.          # Get the list of tables for this group.
490          my @tables = @{$self->{tables}};          my @tables = @{$self->{tables}};
491        # Should we skip this section?
492        if ($self->SkipIndicated($section, \@tables)) {
493            Trace("Resume mode: section $section skipped for group $self->{group}.") if T(3);
494            $self->Add("section-skips" => 1);
495        } else {
496            # Not skipping. Start a timer and protect ourselves from errors.
497            my $startTime = time();
498            eval {
499          # Get the loader hash.          # Get the loader hash.
500          my $loaderHash = $self->{loaders};          my $loaderHash = $self->{loaders};
501          # Initialize the loaders for the necessary tables.          # Initialize the loaders for the necessary tables.
# Line 388  Line 504 
504              my $loader = $loaderHash->{$table};              my $loader = $loaderHash->{$table};
505              # If it doesn't exist yet, create it.              # If it doesn't exist yet, create it.
506              if (! defined $loader) {              if (! defined $loader) {
507                  $loader = ERDBGenerate->new($db, $self->{directory}, $table);                      $loader = ERDBGenerate->new($db, $self->{directory}, $table, $self->{stats});
508                  # Save it for future use.                  # Save it for future use.
509                  $loaderHash->{$table} = $loader;                  $loaderHash->{$table} = $loader;
510                  # Count it.                  # Count it.
# Line 397  Line 513 
513              $loader->Start($section);              $loader->Start($section);
514          }          }
515          # Generate the data to put in the newly-created load files.          # Generate the data to put in the newly-created load files.
         Trace("Calling generator.") if T(3);  
516          $self->Generate();          $self->Generate();
517                # Release our hold on the source object. This allows the database object to
518                # decide whether or not we need a new one.
519                delete $self->{source};
520                # Clean up the database object.
521                $db->Cleanup();
522      };      };
523      # Did it work?      # Did it work?
524      if ($@) {      if ($@) {
# Line 416  Line 536 
536          for my $loader (values %{$self->{loaders}}) {          for my $loader (values %{$self->{loaders}}) {
537              $loader->Finish();              $loader->Finish();
538          }          }
539          # Update the load count and the timer.              # Update the load count.
540          $self->Add("section-loads" => 1);          $self->Add("section-loads" => 1);
541            }
542            # Update the timer.
543          $self->Add(duration => (time() - $startTime));          $self->Add(duration => (time() - $startTime));
544      }      }
545  }  }
# Line 468  Line 590 
590      # Loop through the list of load groups.      # Loop through the list of load groups.
591      for my $group ($erdb->LoadGroupList()) {      for my $group ($erdb->LoadGroupList()) {
592          # Stash the loader's tables in the output hash.          # Stash the loader's tables in the output hash.
593          $retVal->{$group} = GetTables($erdb, $group);          $retVal->{$group} = [ GetTables($erdb, $group) ];
594      }      }
595      # Return the result.      # Return the result.
596      return $retVal;      return $retVal;
# Line 574  Line 696 
696          }          }
697      }      }
698      # Normalize the group names and return them.      # Normalize the group names and return them.
699      return map { ucfirst $_ } @retVal;      @retVal = map { ucfirst $_ } @retVal;
700        Trace("Final group list is " . join(" ", @retVal) . ".") if T(2);
701        return @retVal;
702  }  }
703    
704  =head3 KillFileName  =head3 KillFileName
# Line 616  Line 740 
740      return $retVal;      return $retVal;
741  }  }
742    
743    =head3 SkipIndicated
744    
745        my $flag = $edbl->SkipIndicated($section, \@tables);
746    
747    Return FALSE if the current group should be run for the current section.
748    If the C<resume> option is not set, this method always returns FALSE;
749    otherwise, it will look at the files currently in the load directory and
750    if enough of them are present, it will return TRUE, indicating there's
751    no point in generating data for the indicated tables with respect to the
752    current section. In other words, it will return TRUE if, for every table,
753    there is either a load file for that table or a load file for the
754    specified section of that table.
755    
756    =over 4
757    
758    =item section
759    
760    ID of the relevant section.
761    
762    =item tables
763    
764    List of tables to check.
765    
766    =item RETURN
767    
768    Returns TRUE if load files are already generated for the specified section, else FALSE.
769    
770    =back
771    
772    =cut
773    
774    sub SkipIndicated {
775        # Get the parameters.
776        my ($self, $section, $tables) = @_;
777        # Declare the return variable. It's FALSE if there's no resume parameter.
778        my $retVal = $self->{options}->{resume};
779        # Loop through the table names while $retval is TRUE.
780        for my $table (@$tables) { last if ! $retVal;
781            # Compute the file names.
782            my @files = map { ERDBGenerate::CreateFileName($table, $_, data => $self->{directory}) }
783                (undef, $section);
784            # If neither is present, we can't skip. So, if the grep below returns an empty
785            # list, we set $retVal FALSE, which stops the loop.
786            if (scalar(grep { -f $_ } @files) == 0) {
787                $retVal = 0;
788                Trace("Section $section not found for $table in $self->{group}. Regeneration required.") if T(3);
789            }
790        }
791        # Return the result.
792        return $retVal;
793    }
794    
795    
796  =head2 Virtual Methods  =head2 Virtual Methods
797    

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3