[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.3, Thu Nov 16 19:56:49 2006 UTC revision 1.4, Tue Nov 28 01:00:08 2006 UTC
# Line 2  Line 2 
2    
3  =head1 AttrDBRefresh  =head1 AttrDBRefresh
4    
5  This script refreshes the main entity tables of the Custom Attribute database.  This script sets up 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 43  Line 43 
43  attribute system, it will have no effect, but it will take a very long time  attribute system, it will have no effect, but it will take a very long time
44  doing it.  doing it.
45    
46  =item noLoad  =item initializeAndClear
47    
48  If specified, then the attributes will be migrated but no refresh will take place.  If specified, then the tables in the attribute database are dropped and re-created.
49    
50  =back  =back
51    
# Line 65  Line 65 
65  my ($options, @parameters) = StandardSetup([qw(CustomAttributes ERDB ERDBLoad FIG) ],  my ($options, @parameters) = StandardSetup([qw(CustomAttributes ERDB ERDBLoad FIG) ],
66                                             {                                             {
67                                                trace => [3, "trace level"],                                                trace => [3, "trace level"],
68                                                noLoad => ["", "if specified, the refresh will be skipped"],                                                initializeAndClear => ["", "if specified, the tables of the attribute database will be re-created"],
69                                                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"],
70                                                phone => ["", "phone number (international format) to call when load finishes"],                                                phone => ["", "phone number (international format) to call when load finishes"],
71                                             },                                             },
# Line 79  Line 79 
79      $FIG_Config::attrOld = 1;      $FIG_Config::attrOld = 1;
80      # Get the FIG object.      # Get the FIG object.
81      my $fig = FIG->new();      my $fig = FIG->new();
82      # First we refresh the IDs. Since this is done inside a block, the custom      # Get the attribute database.
     # attributes object we create will be released before we start migrating,  
     # which is important.  
     if (! $options->{noLoad}) {  
83          Trace("Connecting to attribute database.") if T(2);          Trace("Connecting to attribute database.") if T(2);
84          my $ca = CustomAttributes->new();          my $ca = CustomAttributes->new();
85          Trace("Refreshing object IDs.") if T(2);      # Process according to the options selected.
86          $ca->Refresh($fig);      if ($options->{initializeAndClear}) {
87            # Create the tables.
88            $ca->CreateTables();
89            Trace("Tables recreated.") if T(2);
90      }      }
91      if ($options->{migrate}) {      if ($options->{migrate}) {
92            # Migrate the data.
93          Trace("Migrating attribute data.") if T(2);          Trace("Migrating attribute data.") if T(2);
94          CustomAttributes::MigrateAttributes($fig);          my $stats = MigrateAttributes($ca, $fig);
95      }      }
96      Trace("Processing complete.") if T(2);      Trace("Processing complete.") if T(2);
97  };  };
# Line 110  Line 111 
111      }      }
112  }  }
113    
114    =head3 MigrateAttributes
115    
116    C<< my $stats = MigrateAttributes($ca, $fig); >>
117    
118    Migrate all the attributes data from the specified FIG instance. This is a long, slow
119    method used to convert the old attribute data to the new system. Only attribute
120    keys that are already in the database will be loaded, and they will completely
121    replace the existing values for those keys. Therefore, it is very important that the
122    FIG instance not be connected to the attribute database.
123    
124    =over 4
125    
126    =item ca
127    
128    B<CustomAttributes> object used to access the attribute database.
129    
130    =item fig
131    
132    A FIG object that can be used to retrieve attributes for migration purposes.
133    
134    =item RETURN
135    
136    Returns a statistical object for the load process.
137    
138    =back
139    
140    =cut
141    
142    sub MigrateAttributes {
143        # Get the parameters.
144        my ($ca, $fig) = @_;
145        # Create the return value.
146        my $retVal = Stats->new('keysIn', 'erased', 'loaded');
147        # Get a list of all our attribute keys.
148        my @keys = $ca->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
149        # Create a temporary file name.
150        my $tempFileName = "$FIG_Config::temp/AttributeLoadFile$$.tbl";
151        # Loop through them, erasing and loading.
152        for my $key (@keys) {
153            Trace("Migrating key $key.") if T(3);
154            $retVal->Add(keysIn => 1);
155            # Get all the values of the specified key.
156            my @oldValues = $fig->get_attributes(undef, $key);
157            my $count = scalar(@oldValues);
158            Trace("$count values found for $key in source system.") if T(3);
159            # If no values were found, erase the key without loading.
160            if (! $count) {
161                $ca->EraseAttribute($key);
162                $retVal->Add(erased => 1);
163            } else {
164                # Write the values to a temporary file. The first column is the
165                # ID of the object possessing the attribute, the second is the key
166                # name (which we don't need), and the remaining columns are key
167                # values. The only tricky part is we need to concatenate the values
168                # from the tail of the list into a string.
169                my $fh = Open(undef, ">$tempFileName");
170                for my $row (@oldValues) {
171                    my ($id, undef, @values) = @{$row};
172                    my $value = join($ca->{splitter}, @values);
173                    Tracer::PutLine($fh, [$id, $value]);
174                }
175                # Close the file and re-open it for input.
176                close $fh;
177                $fh = Open(undef, "<$tempFileName");
178                # Replace the current attribute values (if any) with the values
179                # from the file.
180                my $newStats = $ca->LoadAttributeKey($key, $fh, 0, 1);
181                # Close the file and accumulate the statistics.
182                close $fh;
183                $retVal->Add(loaded => 1);
184                $retVal->Accumulate($newStats);
185                # Trace the statistics.
186                Trace("Statistics for load of $key:\n" . $newStats->Show()) if T(3);
187            }
188        }
189        # Return the statistics object.
190        return $retVal;
191    }
192    
193  1;  1;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3