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

View of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.21 - (download) (as text) (annotate)
Wed Jan 23 00:56:31 2008 UTC (12 years ago) by parrello
Branch: MAIN
CVS Tags: rast_rel_2008_06_18, rast_rel_2008_06_16, rast_rel_2008_07_21, rast_rel_2008_04_23, mgrast_rel_2008_0806
Changes since 1.20: +1 -1 lines
Added support for concurrent and low-priority loading.

#!/usr/bin/perl -w

package ERDBLoad;

    use strict;
    use Tracer;
    use PageBuilder;
    use ERDB;
    use Stats;

=head1 ERDB Table Load Utility Object

=head2 Introduction

This object is designed to assist with creating the load file for an ERDB
data relation. The user constructs the object by specifying an ERDB object
and a relation name. This create the load file for the relevant relation. The client
then passes in data lines which are written to a file, and calls
L</Finish> to close the file and get the statistics.

This module makes use of the internal ERDB method C<_IsPrimary>.



=head2 Public Methods

=head3 new

    my $erload = ERDBLoad->new($erdb, $relationName, $directory, $loadOnly, $ignore);

Begin loading an ERDB relation.

=over 4

=item erdb

ERDB object representing the target database.

=item relationName

Name of the relation being loaded.

=item directory

Name of the directory to use for the load files, WITHOUT a trailing slash.

=item loadOnly

TRUE if the data is to be loaded from an existing file, FALSE if a file is
to be created.

=item ignore

TRUE if the data is to be discarded. This is used to save time when only
a subset of the tables need to be loaded: the data for the ignored tables
is simply discarded.



sub new {
    # Get the parameters.
    my ($class, $erdb, $relationName, $directory, $loadOnly, $ignore) = @_;
    # Validate the directory name.
    if (! -d $directory) {
        Confess("Load directory \"$directory\" not found.");
    # Determine the name for this relation's load file.
    my $fileName = "$directory/$relationName.dtx";
    # Declare the file handle variable.
    my $fileHandle;
    # Determine whether or not this is a primary relation.
    my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
    # Check to see if this is a load-only, ignore, or a generate-and-load.
    if ($ignore) {
        Trace("Relation $relationName will be ignored.") if T(2);
        $fileHandle = "";
    } elsif ($loadOnly) {
        Trace("Relation $relationName will be loaded from $fileName.") if T(2);
        $fileHandle = "";
    } else {
        # Determine the sort for this relation.
        my $fileString = "| " . $erdb->SortNeeded($relationName) . " >$fileName";
        Trace("Load file creation string is \"$fileString\".") if T(3);
        # Open the output file and remember its handle.
        $fileHandle = Open(undef, $fileString);
        Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
    # Create the $erload object.
    my $retVal = {
                  dbh => $erdb,
                  fh => $fileHandle,
                  fileName => $fileName,
                  relName => $relationName,
                  fileSize => 0,
                  lineCount => 0,
                  stats => Stats->new(),
                  primary => $primary,
                  ignore => ($ignore ? 1 : 0)
    # Bless and return it.
    bless $retVal, $class;
    return $retVal;

=head3 Ignore

    my $flag = $erload->Ignore;

Return TRUE if we are ignoring this table, else FALSE.

#: Return Type $;
sub Ignore {
    # Get the parameters.
    my ($self) = @_;
    # Return the result.
    return $self->{ignore};

=head3 Put

    my  = $erload->Put($field1, $field2, ..., $fieldN);

Write a line of data to the load file. This may also cause the load file to be closed
and data read into the table.

=over 4

=item field1, field2, ..., fieldN

List of field values to be put into the data line. The field values must be in the
order determined shown in the documentation for the table. Internal tabs and
new-lines will automatically be escaped before the data line is formatted.


#: Return Type ;
sub Put {
    # Get the ERDBLoad instance and the field list.
    my ($self, @rawFields) = @_;
    # Only proceed if we're not ignoring.
    if (! $self->{ignore}) {
        # Convert the hash-string fields to their digested value.
        $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
        # Insure the field values are okay.
        my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
        # Run through the list of field values, escaping them.
        my @fields = map { Tracer::Escape($_) } @rawFields;
        # If this is a primary relation, append the new-record field.
        if ($self->{primary}) {
            push @fields, '0';
        # Form a data line from the fields.
        my $line = join("\t", @fields) . "\n";
        # Write the new record to the load file.
        my $fh = $self->{fh};
        print $fh $line;
        # Determine how long this will make the load file.
        my $lineLength = length $line;
        # Update the statistics.
        $self->{fileSize} += $lineLength;
        $self->{lineCount} ++;
        if ($truncates > 0) {
            $self->Add("truncated", $truncates);

=head3 Add

    my  = $stats->Add($statName, $value);

Increment the specified statistic.

=over 4

=item statName

Name of the statistic to increment.

=item value (optional)

Value by which to increment it. If omitted, C<1> is assumed.


#: Return Type ;
sub Add {
    # Get the parameters.
    my ($self, $statName, $value) = @_;
    # Fix the value.
    if (! defined $value) {
        $value = 1;
    # Increment the statistic.
    $self->{stats}->Add($statName, $value);

=head3 Finish

    my $stats = $erload->Finish();

Finish loading the table. This closes the load file.

=over 4

=item RETURN

Returns a statistics object describing what happened during the load and containing any
error messages.



sub Finish {
    # Get this object instance.
    my ($self) = @_;
    if ($self->{fh}) {
        # Close the load file.
        close $self->{fh};
    # Return the statistics object.
    return $self->{stats};

=head3 FinishAndLoad

    my $stats = $erload->FinishAndLoad();

Finish the load and load the table, returning the statistics.


sub FinishAndLoad {
    # Get the parameters.
    my ($self) = @_;
    # Finish the load file.
    my $retVal = $self->Finish();
    # Load the table.
    my $newStats = $self->LoadTable();
    # Accumulate the stats.
    # Return the result.
    return $retVal;

=head3 RelName

    my $name = $erload->RelName;

Name of the relation being loaded by this object.


sub RelName {
    # Get the object instance.
    my ($self) = @_;
    # Return the relation name.
    return $self->{relName};

=head3 LoadTable

    my $stats = $erload->LoadTable();

Load the database table from the load file and return a statistics object.


sub LoadTable {
    # Get the parameters.
    my ($self) = @_;
    # Get the database object, the file name, and the relation name.
    my $erdb = $self->{dbh};
    my $fileName = $self->{fileName};
    my $relName = $self->{relName};
    # Load the table. The third parameter indicates this is a drop and reload.
    my $retVal = $erdb->LoadTable($fileName, $relName, truncate => 1);
    # Return the result.
    return $retVal;


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3