[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.9, Thu Jan 19 09:28:33 2006 UTC revision 1.21, Wed Jan 23 00:56:31 2008 UTC
# Line 28  Line 28 
28    
29  =head3 new  =head3 new
30    
31  C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >>      my $erload = ERDBLoad->new($erdb, $relationName, $directory, $loadOnly, $ignore);
32    
33  Begin loading an ERDB relation.  Begin loading an ERDB relation.
34    
# Line 51  Line 51 
51  TRUE if the data is to be loaded from an existing file, FALSE if a file is  TRUE if the data is to be loaded from an existing file, FALSE if a file is
52  to be created.  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    
62  =cut  =cut
63    
64  sub new {  sub new {
65      # Get the parameters.      # Get the parameters.
66      my ($class, $erdb, $relationName, $directory, $loadOnly) = @_;      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.");
# Line 68  Line 74 
74      my $fileHandle;      my $fileHandle;
75      # Determine whether or not this is a primary relation.      # Determine whether or not this is a primary relation.
76      my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);      my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
77      # Check to see if this is a load-only or a generate-and-load.      # Check to see if this is a load-only, ignore, or a generate-and-load.
78      if ($loadOnly) {      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);          Trace("Relation $relationName will be loaded from $fileName.") if T(2);
83          $fileHandle = "";          $fileHandle = "";
84      } else {      } else {
85          # If this is a primary entity relation, sort the output to remove          # Determine the sort for this relation.
86          # duplicate keys.          my $fileString = "| " . $erdb->SortNeeded($relationName) . " >$fileName";
87          my $fileString = ($erdb->IsEntity($relationName) ?          Trace("Load file creation string is \"$fileString\".") if T(3);
                             "| sort +0 -1 -u -t \"\t\" >$fileName" :  
                             ">$fileName");  
88          # Open the output file and remember its handle.          # Open the output file and remember its handle.
89          $fileHandle = Open(undef, $fileString);          $fileHandle = Open(undef, $fileString);
90          Trace("Relation $relationName load file created with primary flag $primary.") if T(2);          Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
# Line 91  Line 98 
98                    fileSize => 0,                    fileSize => 0,
99                    lineCount => 0,                    lineCount => 0,
100                    stats => Stats->new(),                    stats => Stats->new(),
101                    primary => $primary                    primary => $primary,
102                      ignore => ($ignore ? 1 : 0)
103                   };                   };
104      # Bless and return it.      # Bless and return it.
105      bless $retVal, $class;      bless $retVal, $class;
106      return $retVal;      return $retVal;
107  }  }
108    
109    =head3 Ignore
110    
111        my $flag = $erload->Ignore;
112    
113    Return TRUE if we are ignoring this table, else FALSE.
114    
115    =cut
116    #: Return Type $;
117    sub Ignore {
118        # Get the parameters.
119        my ($self) = @_;
120        # Return the result.
121        return $self->{ignore};
122    }
123    
124  =head3 Put  =head3 Put
125    
126  C<< my  = $erload->Put($field1, $field2, ..., $fieldN); >>      my  = $erload->Put($field1, $field2, ..., $fieldN);
127    
128  Write a line of data to the load file. This may also cause the load file to be closed  Write a line of data to the load file. This may also cause the load file to be closed
129  and data read into the table.  and data read into the table.
# Line 120  Line 143 
143  sub Put {  sub Put {
144      # Get the ERDBLoad instance and the field list.      # Get the ERDBLoad instance and the field list.
145      my ($self, @rawFields) = @_;      my ($self, @rawFields) = @_;
146        # Only proceed if we're not ignoring.
147        if (! $self->{ignore}) {
148            # Convert the hash-string fields to their digested value.
149            $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
150      # Insure the field values are okay.      # Insure the field values are okay.
151      my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);      my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
152      # Run through the list of field values, escaping them.      # Run through the list of field values, escaping them.
# Line 143  Line 170 
170          $self->Add("truncated", $truncates);          $self->Add("truncated", $truncates);
171      }      }
172  }  }
173    }
174    
175  =head3 Add  =head3 Add
176    
177  C<< my  = $stats->Add($statName, $value); >>      my  = $stats->Add($statName, $value);
178    
179  Increment the specified statistic.  Increment the specified statistic.
180    
# Line 177  Line 205 
205    
206  =head3 Finish  =head3 Finish
207    
208  C<< my $stats = $erload->Finish(); >>      my $stats = $erload->Finish();
209    
210  Finish loading the table. This closes the load file and loads its contents into the database.  Finish loading the table. This closes the load file.
 It also creates the indexes if the DBMS uses post-indexing.  
211    
212  =over 4  =over 4
213    
# Line 196  Line 223 
223  sub Finish {  sub Finish {
224      # Get this object instance.      # Get this object instance.
225      my ($self) = @_;      my ($self) = @_;
226        if ($self->{fh}) {
227      # Close the load file.      # Close the load file.
228      close $self->{fh};      close $self->{fh};
229        }
230      # Return the statistics object.      # Return the statistics object.
231      return $self->{stats};      return $self->{stats};
232  }  }
233    
234    =head3 FinishAndLoad
235    
236        my $stats = $erload->FinishAndLoad();
237    
238    Finish the load and load the table, returning the statistics.
239    
240    =cut
241    
242    sub FinishAndLoad {
243        # Get the parameters.
244        my ($self) = @_;
245        # Finish the load file.
246        my $retVal = $self->Finish();
247        # Load the table.
248        my $newStats = $self->LoadTable();
249        # Accumulate the stats.
250        $retVal->Accumulate($newStats);
251        # Return the result.
252        return $retVal;
253    }
254    
255  =head3 RelName  =head3 RelName
256    
257  C<< my $name = $erload->RelName; >>      my $name = $erload->RelName;
258    
259  Name of the relation being loaded by this object.  Name of the relation being loaded by this object.
260    
# Line 217  Line 267 
267      return $self->{relName};      return $self->{relName};
268  }  }
269    
270    =head3 LoadTable
271    
272        my $stats = $erload->LoadTable();
273    
274    Load the database table from the load file and return a statistics object.
275    
276    =cut
277    
278    sub LoadTable {
279        # Get the parameters.
280        my ($self) = @_;
281        # Get the database object, the file name, and the relation name.
282        my $erdb = $self->{dbh};
283        my $fileName = $self->{fileName};
284        my $relName = $self->{relName};
285        # Load the table. The third parameter indicates this is a drop and reload.
286        my $retVal = $erdb->LoadTable($fileName, $relName, truncate => 1);
287        # Return the result.
288        return $retVal;
289    }
290    
291  1;  1;
292    

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.21

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3