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

Diff of /Sprout/CustomAttributes.pm

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

revision 1.27, Sun Sep 30 03:46:30 2007 UTC revision 1.28, Sun Sep 30 20:52:51 2007 UTC
# Line 9  Line 9 
9      use Tracer;      use Tracer;
10      use ERDBLoad;      use ERDBLoad;
11      use Stats;      use Stats;
12      use Time::HiRes;      use Time::HiRes qw(time);
13    
14  =head1 Custom SEED Attribute Manager  =head1 Custom SEED Attribute Manager
15    
# Line 429  Line 429 
429    
430  If specified, the specified object type will be prefixed to each object ID.  If specified, the specified object type will be prefixed to each object ID.
431    
432    =item resume
433    
434    If specified, key-value pairs already in the database will not be reinserted.
435    
436  =back  =back
437    
438  =cut  =cut
# Line 439  Line 443 
443      # Declare the return variable.      # Declare the return variable.
444      my $retVal = Stats->new('keys', 'values');      my $retVal = Stats->new('keys', 'values');
445      # Initialize the timers.      # Initialize the timers.
446      my ($insertTime, $eraseTime, $archiveTime) = (0, 0, 0);      my ($insertTime, $eraseTime, $archiveTime, $checkTime) = (0, 0, 0, 0);
447      # Check for append mode.      # Check for append mode.
448      my $append = ($options{append} ? 1 : 0);      my $append = ($options{append} ? 1 : 0);
449        # Check for resume mode.
450        my $resume = ($options{resume} ? 1 : 0);
451      # Create a hash of key names found.      # Create a hash of key names found.
452      my %keyHash = ();      my %keyHash = ();
453      # Open the file for input. Note we must anticipate the possibility of an      # Open the file for input. Note we must anticipate the possibility of an
# Line 460  Line 466 
466          $ah = Open(undef, ">$options{archive}");          $ah = Open(undef, ">$options{archive}");
467          Trace("Load file will be archived to $options{archive}.") if T(3);          Trace("Load file will be archived to $options{archive}.") if T(3);
468      }      }
469      # Finally, open a database transaction.      # Insure we recover from errors.
     $self->BeginTran();  
     # Insure we recover from errors. If an error occurs, we will delete the archive file and  
     # roll back the updates.  
470      eval {      eval {
471          # Loop through the file.          # Loop through the file.
472          while (! eof $fh) {          while (! eof $fh) {
# Line 518  Line 521 
521                      }                      }
522                      Trace("Key $realKey found.") if T(3);                      Trace("Key $realKey found.") if T(3);
523                  }                  }
524                    # If we're in resume mode, check to see if this insert is redundant.
525                    my $ok = 1;
526                    if ($resume) {
527                        my $startTime = time();
528                        my $count = $self->GetAttributes($id, $key, @values);
529                        $ok = ! $count;
530                        $checkTime += time() - $startTime;
531                    }
532                    if ($ok) {
533                  # Everything is all set up, so add the value.                  # Everything is all set up, so add the value.
534                  my $startTime = time();                  my $startTime = time();
535                  $self->AddAttribute($id, $key, @values);                  $self->AddAttribute($id, $key, @values);
536                  $insertTime += time() - $startTime;                  $insertTime += time() - $startTime;
537                    } else {
538                        # Here we skipped because of resume mode.
539                        $retVal->Add(resumeSkip => 1);
540                    }
541    
542                  my $progress = $retVal->Add(values => 1);                  my $progress = $retVal->Add(values => 1);
543                  Trace("$progress values loaded.") if T(3) && ($progress % 1000 == 0);                  Trace("$progress values loaded.") if T(3) && ($progress % 1000 == 0);
544              }              }
# Line 529  Line 546 
546          $retVal->Add(eraseTime  =>  $eraseTime);          $retVal->Add(eraseTime  =>  $eraseTime);
547          $retVal->Add(insertTime =>  $insertTime);          $retVal->Add(insertTime =>  $insertTime);
548          $retVal->Add(archiveTime => $archiveTime);          $retVal->Add(archiveTime => $archiveTime);
549            $retVal->Add(checkTime   => $checkTime);
550      };      };
551      # Check for an error.      # Check for an error.
552      if ($@) {      if ($@) {
553          # Here we have an error. Roll back the transaction and delete the archive file.          # Here we have an error. Display the error message.
554          my $message = $@;          my $message = $@;
555          Trace("Rolling back attribute updates due to error.") if T(1);          Trace("Error during attribute load: $message") if T(0);
556          $self->RollbackTran();          $retVal->AddMessage($message);
         if (defined $ah) {  
             Trace("Deleting archive file $options{archive}.") if T(1);  
             close $ah;  
             unlink $options{archive};  
557          }          }
558          Confess("Error during attribute load: $message");      # Close the archive file, if any.
     } else {  
         # Here the load worked. Commit the transaction and close the archive file.  
         Trace("Committing attribute upload.") if T(2);  
         $self->CommitTran();  
559          if (defined $ah) {          if (defined $ah) {
560              Trace("Closing archive file $options{archive}.") if T(2);              Trace("Closing archive file $options{archive}.") if T(2);
561              close $ah;              close $ah;
562          }          }
     }  
563      # Return the result.      # Return the result.
564      return $retVal;      return $retVal;
565  }  }

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3