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

Diff of /Sprout/ERDBLoad.pm

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

revision 1.3, Fri Sep 9 14:52:21 2005 UTC revision 1.13, Wed Apr 19 03:34:15 2006 UTC
# Line 28  Line 28 
28    
29  =head3 new  =head3 new
30    
31  C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >>  C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory, $loadOnly, $ignore); >>
32    
33  Begin loading an ERDB relation.  Begin loading an ERDB relation.
34    
# Line 46  Line 46 
46    
47  Name of the directory to use for the load files, WITHOUT a trailing slash.  Name of the directory to use for the load files, WITHOUT a trailing slash.
48    
49  =item estimatedRows (optional)  =item loadOnly
50    
51  Estimated maximum number of table rows. If omitted, the table will be created in  TRUE if the data is to be loaded from an existing file, FALSE if a file is
52  a format that permits an essentially unlimited number of rows.  to be created.
53    
54    =item ignore
55    
56    TRUE if the data is to be discarded. This is used to save time when only
57    a subset of the tables need to be loaded: the data for the ignored tables
58    is simply discarded.
59    
60  =back  =back
61    
# Line 57  Line 63 
63    
64  sub new {  sub new {
65      # Get the parameters.      # Get the parameters.
66      my ($class, $erdb, $relationName, $directory, $estimatedRows) = @_;      my ($class, $erdb, $relationName, $directory, $loadOnly, $ignore) = @_;
67      # Validate the directory name.      # Validate the directory name.
68      if (! -d $directory) {      if (! -d $directory) {
69          Confess("Load directory \"$directory\" not found.");          Confess("Load directory \"$directory\" not found.");
70      }      }
71      # Determine the name for this relation's load file.      # Determine the name for this relation's load file.
72      my $fileName = "$directory/$relationName.dtx";      my $fileName = "$directory/$relationName.dtx";
73        # Declare the file handle variable.
74        my $fileHandle;
75        # Determine whether or not this is a primary relation.
76        my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
77        # Check to see if this is a load-only, ignore, or a generate-and-load.
78        if ($ignore) {
79            Trace("Relation $relationName will be ignored.") if T(2);
80            $fileHandle = "";
81        } elsif ($loadOnly) {
82            Trace("Relation $relationName will be loaded from $fileName.") if T(2);
83            $fileHandle = "";
84        } else {
85            # If this is a primary entity relation whose key is not a hash string, sort
86            # the output to remove duplicate keys.
87            my $fileString = ($erdb->SortNeeded($relationName) ?
88                                "| sort +0 -1 -u -t \"\t\" >$fileName" :
89                                ">$fileName");
90      # Open the output file and remember its handle.      # Open the output file and remember its handle.
91      my $fileHandle = Open(undef, ">$fileName");          $fileHandle = Open(undef, $fileString);
92            Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
93        }
94      # Create the $erload object.      # Create the $erload object.
95      my $retVal = {      my $retVal = {
96                    dbh => $erdb,                    dbh => $erdb,
# Line 75  Line 100 
100                    fileSize => 0,                    fileSize => 0,
101                    lineCount => 0,                    lineCount => 0,
102                    stats => Stats->new(),                    stats => Stats->new(),
103                    primary => $erdb->_IsPrimary($relationName)                    primary => $primary,
104                      ignore => ($ignore ? 1 : 0)
105                   };                   };
106      # Bless and return it.      # Bless and return it.
107      bless $retVal, $class;      bless $retVal, $class;
108      return $retVal;      return $retVal;
109  }  }
110    
111    =head3 Ignore
112    
113    C<< my $flag = $erload->Ignore; >>
114    
115    Return TRUE if we are ignoring this table, else FALSE.
116    
117    =cut
118    #: Return Type $;
119    sub Ignore {
120        # Get the parameters.
121        my ($self) = @_;
122        # Return the result.
123        return $self->{ignore};
124    }
125    
126  =head3 Put  =head3 Put
127    
128  C<< my  = $erload->Put($field1, $field2, ..., $fieldN); >>  C<< my  = $erload->Put($field1, $field2, ..., $fieldN); >>
# Line 102  Line 143 
143  =cut  =cut
144  #: Return Type ;  #: Return Type ;
145  sub Put {  sub Put {
146      # Get the ERDBLoad instance.      # Get the ERDBLoad instance and the field list.
147      my $self = shift @_;      my ($self, @rawFields) = @_;
148        # Only proceed if we're not ignoring.
149        if (! $self->{ignore}) {
150            # Convert the hash-string fields to their digested value.
151            $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
152            # Insure the field values are okay.
153            my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
154      # Run through the list of field values, escaping them.      # Run through the list of field values, escaping them.
155      my @fields = map { Tracer::Escape($_) } @_;          my @fields = map { Tracer::Escape($_) } @rawFields;
156      # If this is a primary relation, append the new-record field.      # If this is a primary relation, append the new-record field.
157      if ($self->{primary}) {      if ($self->{primary}) {
158          push @fields, '0';          push @fields, '0';
# Line 120  Line 167 
167      # Update the statistics.      # Update the statistics.
168      $self->{fileSize} += $lineLength;      $self->{fileSize} += $lineLength;
169      $self->{lineCount} ++;      $self->{lineCount} ++;
170            $self->Add("lineOut");
171            if ($truncates > 0) {
172                $self->Add("truncated", $truncates);
173            }
174        }
175    }
176    
177    =head3 Add
178    
179    C<< my  = $stats->Add($statName, $value); >>
180    
181    Increment the specified statistic.
182    
183    =over 4
184    
185    =item statName
186    
187    Name of the statistic to increment.
188    
189    =item value (optional)
190    
191    Value by which to increment it. If omitted, C<1> is assumed.
192    
193    =back
194    
195    =cut
196    #: Return Type ;
197    sub Add {
198        # Get the parameters.
199        my ($self, $statName, $value) = @_;
200        # Fix the value.
201        if (! defined $value) {
202            $value = 1;
203        }
204        # Increment the statistic.
205        $self->{stats}->Add($statName, $value);
206  }  }
207    
208  =head3 Finish  =head3 Finish
# Line 143  Line 226 
226  sub Finish {  sub Finish {
227      # Get this object instance.      # Get this object instance.
228      my ($self) = @_;      my ($self) = @_;
229        if ($self->{fh}) {
230      # Close the load file.      # Close the load file.
231      close $self->{fh};      close $self->{fh};
232        }
233      # Return the statistics object.      # Return the statistics object.
234      return $self->{stats};      return $self->{stats};
235  }  }

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3