[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.2, Sun Aug 14 23:32:08 2005 UTC revision 1.10, Mon Jan 30 21:52:04 2006 UTC
# Line 12  Line 12 
12    
13  =head2 Introduction  =head2 Introduction
14    
15  This object is designed to assist with loading an ERDB data relation. The user  This object is designed to assist with creating the load file for an ERDB
16  constructs the object by specifying an ERDB object and a relation name. This  data relation. The user constructs the object by specifying an ERDB object
17  re-creates the relevant relation. The client then passes in data lines which  and a relation name. This create the load file for the relevant relation. The client
18  are written to a file. When the file gets big enough, it is loaded into the  then passes in data lines which are written to a file, and calls
19  table. Calling the L</Finish> method loads any leftover data and optionally  L</Finish> to close the file and get the statistics.
 creates the index.  
20    
21  This module makes use of the internal ERDB property C<_dbh> and the internal  This module makes use of the internal ERDB method C<_IsPrimary>.
 method C<_IsPrimary>.  
22    
23  =cut  =cut
24    
# Line 48  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 59  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      # Decide whether or not we should specify estimated rows.      # Declare the file handle variable.
74      my $rowEstimate = ($FIG_Config::estimate_rows ? $estimatedRows : undef);      my $fileHandle;
75      # Create the target table. If this is a pre-index DBMS, we      # Determine whether or not this is a primary relation.
76      # also create the indexes. If the table already exists,      my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
77      # it will be dropped.      # Check to see if this is a load-only, ignore, or a generate-and-load.
78      $erdb->CreateTable($relationName, $FIG_Config::preIndex, $rowEstimate);      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, sort the output to remove
86            # duplicate keys.
87            my $fileString = ($erdb->IsEntity($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 83  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 110  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            # Insure the field values are okay.
151            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.
153      my @fields = map { Tracer::Escape($_) } @_;          my @fields = map { Tracer::Escape($_) } @rawFields;
154      # If this is a primary relation, append the new-record field.      # If this is a primary relation, append the new-record field.
155      if ($self->{primary}) {      if ($self->{primary}) {
156          push @fields, '0';          push @fields, '0';
157      }      }
158      # Form a data line from the fields.      # Form a data line from the fields.
159      my $line = join("\t", @fields) . "\n";      my $line = join("\t", @fields) . "\n";
     # Determine how long this will make the load file.  
     my $lineLength = length $line;  
     if ($lineLength > (200000000 - $self->{fileSize})) {  
         # Here it would be too long, so we force a load.  
         $self->Flush();  
     }  
160      # Write the new record to the load file.      # Write the new record to the load file.
161      my $fh = $self->{fh};      my $fh = $self->{fh};
162      print $fh $line;      print $fh $line;
163            # Determine how long this will make the load file.
164            my $lineLength = length $line;
165      # Update the statistics.      # Update the statistics.
166      $self->{fileSize} += $lineLength;      $self->{fileSize} += $lineLength;
167      $self->{lineCount} ++;      $self->{lineCount} ++;
168            $self->Add("lineOut");
169            if ($truncates > 0) {
170                $self->Add("truncated", $truncates);
171            }
172  }  }
173    }
174    
175    =head3 Add
176    
177  =head3 Flush  C<< my  = $stats->Add($statName, $value); >>
178    
179  C<< $erload->Flush(); >>  Increment the specified statistic.
180    
181  Load all the data currently in the load file into the database. This clears the load  =over 4
182  file and re-opens it.  
183    =item statName
184    
185    Name of the statistic to increment.
186    
187    =item value (optional)
188    
189    Value by which to increment it. If omitted, C<1> is assumed.
190    
191    =back
192    
193  =cut  =cut
194  #: Return Type ;  #: Return Type ;
195  sub Flush {  sub Add {
196      # Get the parameters.      # Get the parameters.
197      my ($self) = @_;      my ($self, $statName, $value) = @_;
198      # Flush the data in the load file.      # Fix the value.
199      $self->_FlushData();      if (! defined $value) {
200      # Re-open the file so it can accept more data.          $value = 1;
201      $self->_ReOpen();      }
202        # Increment the statistic.
203        $self->{stats}->Add($statName, $value);
204  }  }
205    
206  =head3 Finish  =head3 Finish
# Line 173  Line 224 
224  sub Finish {  sub Finish {
225      # Get this object instance.      # Get this object instance.
226      my ($self) = @_;      my ($self) = @_;
227      # Flush out the data in the load file.      if ($self->{fh}) {
228      $self->_FlushData();          # Close the load file.
229      # If this is a post-index DBMS, create the indexes.          close $self->{fh};
     if (! $FIG_Config::preIndex) {  
         $self->{dbh}->CreateIndex($self->RelName);  
230      }      }
     # Delete the load file.  
     unlink $self->{fileName};  
231      # Return the statistics object.      # Return the statistics object.
232      return $self->{stats};      return $self->{stats};
233  }  }
# Line 200  Line 247 
247      return $self->{relName};      return $self->{relName};
248  }  }
249    
 =head2 Internal Methods  
   
 =head3 ReOpen  
   
 Re-open the load file.  
   
 This is an instance method.  
   
 =cut  
   
 sub _ReOpen {  
     # Get this instance.  
     my ($self) = @_;  
     # Open the file with the current filehandle in truncate mode.  
     Open($self->{fh}, ">" . $self->{fileName});  
     # Denote the file is empty.  
     $self->{fileSize} = 0;  
     $self->{lineCount} = 0;  
 }  
   
 =head3 FlushData  
   
 Close the load file and load all its data into the table.  
   
 This is an instance method.  
   
 =cut  
   
 sub _FlushData {  
     # Get this instance.  
     my ($self) = @_;  
     # Get the relation name.  
     my $relName = $self->RelName;  
     Trace("Flushing data to table $relName.") if T(2);  
     # Close the load file.  
     close $self->{fh};  
     # We must use the load file to load the table. First, we get the DBKernel  
     # handle and the statistics object.  
     my $stats = $self->{stats};  
     my $dbh = $self->{dbh}->{_dbh};  
     # Begin a database transaction. This is not actually for integrity reasons; it  
     # speeds up the slow load process.  
     $dbh->begin_tran();  
     # Load the database table safely.  
     my $rv;  
     eval {  
         Trace("Loading file into relation $relName.") if T(3);  
         $rv = $dbh->load_table(file => $self->{fileName}, tbl => $relName);  
     };  
     # Check to see if we succeeded.  
     if (!defined $rv) {  
         # We've failed. Format a useful message. If we have an error message from  
         # EVAL, we use it.  
         my $msg = "Table load failed for $relName" . ($@ ? ": $@" : ".");  
         $stats->AddMessage($msg);  
         Trace($msg) if T(1);  
     } else {  
         # Here we successfully loaded the table. Trace the number of records loaded.  
         my $lineCount = $self->{lineCount};  
         my $byteCount = $self->{fileSize};  
         Trace("$lineCount records ($byteCount bytes) loaded into $relName.") if T(2);  
         # Accumulate the statistics.  
         $stats->Add("records", $lineCount);  
         $stats->Add("bytes", $byteCount);  
     }  
     # Close the database transaction.  
     $dbh->commit_tran();  
 }  
   
250  1;  1;
251    

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.10

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3