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

Diff of /Sprout/AttrDBRefresh.pl

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

revision 1.4, Tue Nov 28 01:00:08 2006 UTC revision 1.5, Wed Nov 29 20:28:52 2006 UTC
# Line 2  Line 2 
2    
3  =head1 AttrDBRefresh  =head1 AttrDBRefresh
4    
5  This script sets up the custom attributes database.  This script performs useful function on the custom attributes database.
6    
7  The currently-supported command-line options are as follows.  The currently-supported command-line options are as follows.
8    
# Line 47  Line 47 
47    
48  If specified, then the tables in the attribute database are dropped and re-created.  If specified, then the tables in the attribute database are dropped and re-created.
49    
50    =item load
51    
52    If specified, the name of a file containing attribute data to be loaded into the
53    system. The file is presumed to be tab-delimited. The first column must be the
54    object ID, the second the attribute key name, and the remaining columns the
55    attribute values. All attribute keys mentioned in the file will be erased before
56    loading.
57    
58    =item backup
59    
60    If specified, the name of a file into which all the attribute data should be
61    dumped. The output file will be tab-delimited, with the first column containing
62    object IDs, the second attribute key names, and the remaining columns the
63    attribute values.
64    
65  =back  =back
66    
67  =cut  =cut
# Line 59  Line 74 
74  use File::Copy;  use File::Copy;
75  use File::Path;  use File::Path;
76  use CustomAttributes;  use CustomAttributes;
77    use ERDBLoad;
78  use FIG;  use FIG;
79    
80  # Get the command-line options and parameters.  # Get the command-line options and parameters.
81  my ($options, @parameters) = StandardSetup([qw(CustomAttributes ERDB ERDBLoad FIG) ],  my ($options, @parameters) = StandardSetup([qw(CustomAttributes FIG) ],
82                                             {                                             {
83                                                trace => [3, "trace level"],                                                trace => [3, "trace level"],
84                                                initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],                                                initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],
85                                                migrate => ["", "if specified, attribute data will be migrated along with the object IDs"],                                                migrate => ["", "if specified, attribute data will be migrated along with the object IDs"],
86                                                phone => ["", "phone number (international format) to call when load finishes"],                                                phone => ["", "phone number (international format) to call when load finishes"],
87                                                  load => ["", "file from which to load attribute data"],
88                                                  backup => ["", "file to which attribute data should be dumped"]
89                                             },                                             },
90                                             "",                                             "",
91                                             @ARGV);                                             @ARGV);
# Line 83  Line 101 
101      Trace("Connecting to attribute database.") if T(2);      Trace("Connecting to attribute database.") if T(2);
102      my $ca = CustomAttributes->new();      my $ca = CustomAttributes->new();
103      # Process according to the options selected.      # Process according to the options selected.
104        if ($options->{load} && $options->{backup}) {
105            # Prevent the user from screwing himself by doing a load and a backup
106            # at the same time.
107            Confess("Please do not specify LOAD and BACKUP at the same time.");
108        }
109      if ($options->{initializeAndClear}) {      if ($options->{initializeAndClear}) {
110          # Create the tables.          # Create the tables.
111          $ca->CreateTables();          $ca->CreateTables();
# Line 92  Line 115 
115          # Migrate the data.          # Migrate the data.
116          Trace("Migrating attribute data.") if T(2);          Trace("Migrating attribute data.") if T(2);
117          my $stats = MigrateAttributes($ca, $fig);          my $stats = MigrateAttributes($ca, $fig);
118            Trace("Migration statistics:\n" . $stats->Show()) if T(2);
119        }
120        if ($options->{load}) {
121            # We want to load the attribute data from the specified file, but
122            # first we need to verify that the file exists.
123            my $loadFileName = $options->{load};
124            if (! -f $loadFileName) {
125                Confess("Cannot load: file \"$loadFileName\" is not found or not a file.");
126            } else {
127                Trace("Loading attribute data from $loadFileName.") if T(2);
128                my $stats = $ca->LoadAttributesFrom($loadFileName);
129                Trace("Load statistics:\n" . $stats->Show()) if T(2);
130            }
131        }
132        if ($options->{backup}) {
133            # Back up the attributes to the specified file.
134            my $backupFileName = $options->{backup};
135            Trace("Backing up attribute database.") if T(2);
136            my $stats = $ca->BackupAllAttributes($backupFileName);
137            Trace("Backup statistics:\n" . $stats->Show()) if T(2);
138      }      }
139      Trace("Processing complete.") if T(2);      Trace("Processing complete.") if T(2);
140  };  };
# Line 143  Line 186 
186      # Get the parameters.      # Get the parameters.
187      my ($ca, $fig) = @_;      my ($ca, $fig) = @_;
188      # Create the return value.      # Create the return value.
189      my $retVal = Stats->new('keysIn', 'erased', 'loaded');      my $retVal = Stats->new('keysIn');
190        # Create a loader for the value table.
191        my $hasValueFor = ERDBLoad->new($ca, 'HasValueFor', $FIG_Config::temp);
192        # Create a hash for the target objects.
193        my %targetObjectHash = ();
194      # Get a list of all our attribute keys.      # Get a list of all our attribute keys.
195      my @keys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');      my @keys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
196      # Create a temporary file name.      # Loop through them, building the load files. Note that this process will
197      my $tempFileName = "$FIG_Config::temp/AttributeLoadFile$$.tbl";      # erase and rebuild the data tables involved.
     # Loop through them, erasing and loading.  
198      for my $key (@keys) {      for my $key (@keys) {
199          Trace("Migrating key $key.") if T(3);          Trace("Migrating key $key.") if T(3);
200          $retVal->Add(keysIn => 1);          $retVal->Add(keysIn => 1);
# Line 156  Line 202 
202          my @oldValues = $fig->get_attributes(undef, $key);          my @oldValues = $fig->get_attributes(undef, $key);
203          my $count = scalar(@oldValues);          my $count = scalar(@oldValues);
204          Trace("$count values found for $key in source system.") if T(3);          Trace("$count values found for $key in source system.") if T(3);
205          # If no values were found, erase the key without loading.          # Loop through the rows.
         if (! $count) {  
             $ca->EraseAttribute($key);  
             $retVal->Add(erased => 1);  
         } else {  
             # Write the values to a temporary file. The first column is the  
             # ID of the object possessing the attribute, the second is the key  
             # name (which we don't need), and the remaining columns are key  
             # values. The only tricky part is we need to concatenate the values  
             # from the tail of the list into a string.  
             my $fh = Open(undef, ">$tempFileName");  
206              for my $row (@oldValues) {              for my $row (@oldValues) {
207                # Get this row's data.
208                  my ($id, undef, @values) = @{$row};                  my ($id, undef, @values) = @{$row};
209                  my $value = join($ca->{splitter}, @values);              # Format the values.
210                  Tracer::PutLine($fh, [$id, $value]);              my $valueString = join($ca->{splitter}, @values);
211              }              # Add the value.
212              # Close the file and re-open it for input.              $hasValueFor->Put($key, $id, $valueString);
             close $fh;  
             $fh = Open(undef, "<$tempFileName");  
             # Replace the current attribute values (if any) with the values  
             # from the file.  
             my $newStats = $ca->LoadAttributeKey($key, $fh, 0, 1);  
             # Close the file and accumulate the statistics.  
             close $fh;  
             $retVal->Add(loaded => 1);  
             $retVal->Accumulate($newStats);  
             # Trace the statistics.  
             Trace("Statistics for load of $key:\n" . $newStats->Show()) if T(3);  
213          }          }
214      }      }
215        # Close and finish the loads to upload the data.
216        Trace("Closing value table.") if T(2);
217        my $hvfStats = $hasValueFor->FinishAndLoad();
218        Trace("Statistics from value table load:\n" . $hvfStats->Show()) if T(2);
219        # Merge the statistics.
220        $retVal->Accumulate($hvfStats);
221      # Return the statistics object.      # Return the statistics object.
222      return $retVal;      return $retVal;
223  }  }

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3