[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.5, Wed Oct 12 02:58:20 2005 UTC revision 1.16, Sun Jul 9 21:16:12 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      # If this is a primary entity relation, sort the output to remove      # Declare the file handle variable.
74      # duplicate keys.      my $fileHandle;
75      my $fileString = ($erdb->IsEntity($relationName) ?      # Determine whether or not this is a primary relation.
76                          "| sort +0 -1 -u -t \\t >$fileName" :      my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
77                          ">$fileName");      # 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            # Determine the sort for this relation.
86            my $fileString = "| " . $erdb->SortNeeded($relationName) . ">$fileName";
87      # Open the output file and remember its handle.      # Open the output file and remember its handle.
88      my $fileHandle = Open(undef, $fileString);          $fileHandle = Open(undef, $fileString);
89            Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
90        }
91      # Create the $erload object.      # Create the $erload object.
92      my $retVal = {      my $retVal = {
93                    dbh => $erdb,                    dbh => $erdb,
# Line 80  Line 97 
97                    fileSize => 0,                    fileSize => 0,
98                    lineCount => 0,                    lineCount => 0,
99                    stats => Stats->new(),                    stats => Stats->new(),
100                    primary => $erdb->_IsPrimary($relationName)                    primary => $primary,
101                      ignore => ($ignore ? 1 : 0)
102                   };                   };
103      # Bless and return it.      # Bless and return it.
104      bless $retVal, $class;      bless $retVal, $class;
105      return $retVal;      return $retVal;
106  }  }
107    
108    =head3 Ignore
109    
110    C<< my $flag = $erload->Ignore; >>
111    
112    Return TRUE if we are ignoring this table, else FALSE.
113    
114    =cut
115    #: Return Type $;
116    sub Ignore {
117        # Get the parameters.
118        my ($self) = @_;
119        # Return the result.
120        return $self->{ignore};
121    }
122    
123  =head3 Put  =head3 Put
124    
125  C<< my  = $erload->Put($field1, $field2, ..., $fieldN); >>  C<< my  = $erload->Put($field1, $field2, ..., $fieldN); >>
# Line 107  Line 140 
140  =cut  =cut
141  #: Return Type ;  #: Return Type ;
142  sub Put {  sub Put {
143      # Get the ERDBLoad instance.      # Get the ERDBLoad instance and the field list.
144      my $self = shift @_;      my ($self, @rawFields) = @_;
145        # Only proceed if we're not ignoring.
146        if (! $self->{ignore}) {
147            # Convert the hash-string fields to their digested value.
148            $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
149            # Insure the field values are okay.
150            my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
151      # Run through the list of field values, escaping them.      # Run through the list of field values, escaping them.
152      my @fields = map { Tracer::Escape($_) } @_;          my @fields = map { Tracer::Escape($_) } @rawFields;
153      # If this is a primary relation, append the new-record field.      # If this is a primary relation, append the new-record field.
154      if ($self->{primary}) {      if ($self->{primary}) {
155          push @fields, '0';          push @fields, '0';
# Line 126  Line 165 
165      $self->{fileSize} += $lineLength;      $self->{fileSize} += $lineLength;
166      $self->{lineCount} ++;      $self->{lineCount} ++;
167      $self->Add("lineOut");      $self->Add("lineOut");
168            if ($truncates > 0) {
169                $self->Add("truncated", $truncates);
170            }
171        }
172  }  }
173    
174  =head3 Add  =head3 Add
175    
176  C<< my  = $stats->Add($statName); >>  C<< my  = $stats->Add($statName, $value); >>
177    
178  Increment the specified statistic.  Increment the specified statistic.
179    
# Line 140  Line 183 
183    
184  Name of the statistic to increment.  Name of the statistic to increment.
185    
186    =item value (optional)
187    
188    Value by which to increment it. If omitted, C<1> is assumed.
189    
190  =back  =back
191    
192  =cut  =cut
193  #: Return Type ;  #: Return Type ;
194  sub Add {  sub Add {
195      # Get the parameters.      # Get the parameters.
196      my ($self, $statName) = @_;      my ($self, $statName, $value) = @_;
197        # Fix the value.
198        if (! defined $value) {
199            $value = 1;
200        }
201      # Increment the statistic.      # Increment the statistic.
202      $self->{stats}->Add($statName);      $self->{stats}->Add($statName, $value);
203  }  }
204    
205  =head3 Finish  =head3 Finish
206    
207  C<< my $stats = $erload->Finish(); >>  C<< my $stats = $erload->Finish(); >>
208    
209  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.  
210    
211  =over 4  =over 4
212    
# Line 172  Line 222 
222  sub Finish {  sub Finish {
223      # Get this object instance.      # Get this object instance.
224      my ($self) = @_;      my ($self) = @_;
225        if ($self->{fh}) {
226      # Close the load file.      # Close the load file.
227      close $self->{fh};      close $self->{fh};
228        }
229      # Return the statistics object.      # Return the statistics object.
230      return $self->{stats};      return $self->{stats};
231  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.16

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3