[Bio] / Sprout / ERDBLoader.pl Repository:
ViewVC logotype

Diff of /Sprout/ERDBLoader.pl

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

revision 1.5, Wed Oct 15 11:41:12 2008 UTC revision 1.10, Mon May 4 18:49:49 2009 UTC
# Line 76  Line 76 
76    
77  If specified, turns on tracing of SQL activity.  If specified, turns on tracing of SQL activity.
78    
79    =item clear
80    
81    If specified, existing load files will be recreated from sections if the sections
82    are present.
83    
84  =item background  =item background
85    
86  Save the standard and error output to files. The files will be created  Save the standard and error output to files. The files will be created
# Line 106  Line 111 
111    
112  Phone number to message when the script is complete.  Phone number to message when the script is complete.
113    
114    =item DBD
115    
116    Fully-qualified name of the DBD file. This option allows the use of an alternate
117    DBD during load so that access to the database by other processes is not
118    compromised.
119    
120    =item loadDirectory
121    
122    Directoty containing the load files. This option allows you to request that
123    load files from another version of the NMPDR be used, which is useful when
124    creating a new NMPDR: we can yank in the data from the previous database while
125    waiting for the new load files to be generated.
126    
127    =item dbName
128    
129    SQL name of the target database. If not specified, the default name is used.
130    This option allows you to specify a backup or alternate database that can
131    be loaded without compromising the main database.
132    
133  =back  =back
134    
135  =cut  =cut
# Line 113  Line 137 
137  # Get the command-line options and parameters.  # Get the command-line options and parameters.
138  my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],  my ($options, @parameters) = StandardSetup([qw(ERDBLoadGroup ERDB Stats) ],
139                                             {                                             {
140                                                  dbName => ["", "if specified, the SQL name of the target database"],
141                                                  clear => ["", "overwrite existing load files if sections are present"],
142                                                sanityCheck => ["", "don't load, trace contents of first N load file records instead"],                                                sanityCheck => ["", "don't load, trace contents of first N load file records instead"],
143                                                trace => ["", "tracing level"],                                                trace => ["2", "tracing level"],
144                                                keepSections => ["", "if specified, section files will not be deleted after being collated"],                                                keepSections => ["", "if specified, section files will not be deleted after being collated"],
145                                                phone => ["", "phone number (international format) to call when load finishes"]                                                phone => ["", "phone number (international format) to call when load finishes"],
146                                                  DBD => ["", "if specified, the name of a DBD file in the FIG directory"],
147                                                  loadDirectory => ["", "if specified, an alternate directory containing the load files"],
148                                             },                                             },
149                                             "<database> <group1> <group2> ...",                                             "<database> <group1> <group2> ...",
150                                             @ARGV);                                             @ARGV);
# Line 126  Line 154 
154  eval {  eval {
155      # Get the parameters.      # Get the parameters.
156      my ($database, @groups) = @parameters;      my ($database, @groups) = @parameters;
157      # Connect to the database.      # Connect to the database and get its load directory.
158      my $erdb = ERDB::GetDatabase($database);      my $erdb = ERDB::GetDatabase($database, undef, %$options);
159      # Fix the group list.      # Fix the group list.
160      my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);      my @realGroups = ERDBLoadGroup::ComputeGroups($erdb, \@groups);
161      # Get the source object and load directory for this database.      # Get the source object and load directory for this database.
162      my $source = $erdb->GetSourceObject();      my $source = $erdb->GetSourceObject();
163      my $directory = $erdb->LoadDirectory();      my $directory = $options->{loadDirectory} || $erdb->LoadDirectory();
164      # Get the list of sections.      # Get the list of sections.
165      my @sectionList = $erdb->SectionList($source);      my @sectionList = $erdb->SectionList($source);
166      # Create a statistics object to track our progress.      # Create a statistics object to track our progress.
# Line 157  Line 185 
185              # Get the data file name.              # Get the data file name.
186              my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);              my $dataFile = ERDBGenerate::CreateFileName($table, undef, 'data', $directory);
187              # Do we have it?              # Do we have it?
188              if (-f $dataFile) {              if (-f $dataFile && ! $options->{clear}) {
189                  # Yes. This is good news.                  # Yes. This is good news.
190                  $stats->Add('tables-found' => 1);                  $stats->Add('tables-found' => 1);
191                  Trace("Table file found for $table.") if T(3);                  Trace("Table file found for $table.") if T(3);
192              } else {              } else {
193                  # No, we must build it. Verify that we have all the sections.                  # No, we must build it. Verify that we have all the sections.
194                  my @missingFiles = grep { ! -f $_ } @sectionFiles;                  my @missingFiles = grep { ! -f $_ } @sectionFiles;
                 # Tell the user about all the missing files.  
                 for my $missingFile (@missingFiles) {  
                     $stats->Add('sections-missing' => 1);  
                     $stats->AddMessage("Data file $missingFile not found for table $table.");  
                 }  
195                  # Did we find everything?                  # Did we find everything?
196                  if (scalar @missingFiles) {                  if (scalar @missingFiles) {
197                      # No! Denote that we have a missing table.                      # No! Denote that we have a missing table.
198                      $missingTable++;                      $missingTable++;
199                      $stats->Add('tables-skipped' => 1);                      $stats->Add('tables-skipped' => 1);
200                        # If the user wants a sanity check, we want to give him some
201                        # data anyway.
202                        if ($sanityCheck) {
203                            # Get some data lines in the sections. Note we stop when we've exceeded
204                            # the number of lines expected by the sanity check.
205                            my @lines;
206                            for my $sectionFile (@sectionFiles) {
207                                if (-s $sectionFile && scalar(@lines) < $sanityCheck) {
208                                    Trace("Reading from $sectionFile for $table.") if T(3);
209                                    push @lines, Tracer::GetFile($sectionFile);
210                                }
211                            }
212                            # Create a new temporary file.
213                            my $tmpFile = "$FIG_Config::temp/$table$$.temp.dtx";
214                            my $oh = Open(undef, ">$tmpFile");
215                            # Put all the data into it.
216                            Trace(scalar(@lines) . " data lines found.") if T(3);
217                            print $oh join("\n", @lines);
218                            close $oh;
219                            # Sanity check the temp file.
220                            CheckLoadFile($erdb, $table, $tmpFile, $sanityCheck);
221                            # Clean it up.
222                            unlink $tmpFile;
223                        } else {
224                            # Otherwise tell the user about all the missing files.
225                            for my $missingFile (@missingFiles) {
226                                $stats->Add('sections-missing' => 1);
227                                $stats->AddMessage("Data file $missingFile not found for table $table.");
228                            }
229                        }
230                  } else {                  } else {
231                      # Yes! Try to assemble the sections into a data file.                      # We have all the sections. Try to assemble them into a data file.
232                      my $sortStart = time();                      my $sortStart = time();
233                      my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";                      my $sortCommand = $erdb->SortNeeded($table) . " >$dataFile";
234                      Trace("Sort command: $sortCommand") if T(3);                      Trace("Sort command: $sortCommand") if T(3);
235                        # Pipe to the sort command. Note that we turn on autoflush
236                        # so there's no buffering.
237                      my $oh = Open(undef, "| $sortCommand");                      my $oh = Open(undef, "| $sortCommand");
238                        select $oh; $| = 1; select STDOUT;
239                        # Loop through the sections.
240                      for my $sectionFile (@sectionFiles) {                      for my $sectionFile (@sectionFiles) {
241                          Trace("Collating $sectionFile.") if T(3);                          Trace("Collating $sectionFile.") if T(3);
242                          $stats->Add("$table-sections" => 1);                          $stats->Add("$table-sections" => 1);
243                          for my $line (Tracer::GetFile($sectionFile)) {                          # Loop through the section file.
244                              print $oh "$line\n";                          my $ih = Open(undef, "<$sectionFile");
245                            while (defined (my $line = <$ih>)) {
246                                print $oh $line;
247                              $stats->Add("$table-collations" => 1);                              $stats->Add("$table-collations" => 1);
248                          }                          }
249                      }                      }
# Line 212  Line 271 
271          if ($missingTable) {          if ($missingTable) {
272              # Yes, skip this group.              # Yes, skip this group.
273              $stats->Add('groups-skipped' => 1);              $stats->Add('groups-skipped' => 1);
274              Trace("Skipping $group group: $missingTable missing tables.") if T(3);              Trace("Skipping $group group: $missingTable missing tables.") if T(2);
275          } else {          } else {
276              # No! Process this group's files.              # No! Process this group's files.
277              if ($sanityCheck eq "") {              if ($sanityCheck eq "") {
# Line 257  Line 316 
316      }      }
317  }  }
318    
319    =head2 Internal Methods
320    
321  =head3 CheckLoadFile  =head3 CheckLoadFile
322    
323      CheckLoadFile($erdb, $table, $fileName, $count);      CheckLoadFile($erdb, $table, $fileName, $count);
# Line 298  Line 359 
359          push @records, [ Tracer::GetLine($ih) ];          push @records, [ Tracer::GetLine($ih) ];
360      }      }
361      my $found = scalar(@records);      my $found = scalar(@records);
362      Trace("$found records for $table found in sanity check.") if T(3);      Trace("$found records for $table found in sanity check using $fileName.") if T(2);
363      # Do we have any data at all?      # Do we have any data at all?
364      if ($found) {      if ($found) {
365          # Yes. Get the table's descriptor. We use this to determine the field names.          # Yes. Get the table's descriptor. We use this to determine the field names.
366          my $relationData = $erdb->FindRelation($table);          my $relationData = $erdb->FindRelation($table);
367          Confess("Relation $table not found in database.") if (! defined $relationData);          Confess("Relation $table not found in database.") if (! defined $relationData);
368          my @fields = @{$relationData->{Fields}};          my @fields = @{$relationData->{Fields}};
369            # If this is a relationship, we need the FROM and TO data.
370            my %ends; ($ends{from}, $ends{to}) = $erdb->GetRelationshipEntities($table);
371          # Loop through the fields. We generate one message per field.          # Loop through the fields. We generate one message per field.
372          for (my $i = 0; $i <= $#fields; $i++) {          for (my $i = 0; $i <= $#fields; $i++) {
373              # Get this field's information.              # Get this field's information.
374              my $fieldInfo = $fields[$i];              my $fieldInfo = $fields[$i];
375              my $type = $fieldInfo->{type};              my $type = $fieldInfo->{type};
376                my $name = $fieldInfo->{name};
377                if ($name =~ /^(from|to)-link$/) {
378                    # Here it's a relationship link, so add the name of the target table to
379                    # the type.
380                    $type .= " ($ends{$1})";
381                }
382              # This is going to be a multi-line trace message. We start with the field name and type.              # This is going to be a multi-line trace message. We start with the field name and type.
383              my @lines = ("Values for $fieldInfo->{name}, type $type:\n");              my @lines = ("Values for $table($name), type $type:\n");
384              # Loop through the records. We generate one line of data per record.              # Loop through the records. We generate one line of data per record.
385              for (my $j = 0; $j < $found; $j++) {              for (my $j = 0; $j < $found; $j++) {
386                  # Get the field value.                  # Get the field value.
# Line 319  Line 388 
388                  # Compute the record label.                  # Compute the record label.
389                  my $line = "Record $j";                  my $line = "Record $j";
390                  # Check for unusual cases.                  # Check for unusual cases.
391                  if (! defined $field) {                  if (! defined $field || $field eq '') {
                     $line .= "= <null>";  
                 } elsif ($field eq '') {  
392                      $line .= "= <empty>";                      $line .= "= <empty>";
393                  } else {                  } else {
394                      # Make sure we don't trace something ungodly.                      # Make sure we don't trace something ungodly.

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3