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

Annotation of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package ERDBLoad;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use PageBuilder;
8 :     use ERDB;
9 :     use Stats;
10 :    
11 :     =head1 ERDB Table Load Utility Object
12 :    
13 :     =head2 Introduction
14 :    
15 : parrello 1.3 This object is designed to assist with creating the load file for an ERDB
16 :     data relation. The user constructs the object by specifying an ERDB object
17 :     and a relation name. This create the load file for the relevant relation. The client
18 :     then passes in data lines which are written to a file, and calls
19 :     L</Finish> to close the file and get the statistics.
20 : parrello 1.1
21 : parrello 1.3 This module makes use of the internal ERDB method C<_IsPrimary>.
22 : parrello 1.1
23 :     =cut
24 :    
25 :     #
26 :    
27 :     =head2 Public Methods
28 :    
29 :     =head3 new
30 :    
31 : parrello 1.20 my $erload = ERDBLoad->new($erdb, $relationName, $directory, $loadOnly, $ignore);
32 : parrello 1.1
33 :     Begin loading an ERDB relation.
34 :    
35 :     =over 4
36 :    
37 :     =item erdb
38 :    
39 :     ERDB object representing the target database.
40 :    
41 :     =item relationName
42 :    
43 :     Name of the relation being loaded.
44 :    
45 :     =item directory
46 :    
47 :     Name of the directory to use for the load files, WITHOUT a trailing slash.
48 :    
49 : parrello 1.8 =item loadOnly
50 : parrello 1.2
51 : parrello 1.8 TRUE if the data is to be loaded from an existing file, FALSE if a file is
52 :     to be created.
53 : parrello 1.2
54 : parrello 1.10 =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 : parrello 1.1 =back
61 :    
62 :     =cut
63 :    
64 :     sub new {
65 :     # Get the parameters.
66 : parrello 1.10 my ($class, $erdb, $relationName, $directory, $loadOnly, $ignore) = @_;
67 : parrello 1.1 # Validate the directory name.
68 :     if (! -d $directory) {
69 :     Confess("Load directory \"$directory\" not found.");
70 :     }
71 :     # Determine the name for this relation's load file.
72 :     my $fileName = "$directory/$relationName.dtx";
73 : parrello 1.8 # Declare the file handle variable.
74 :     my $fileHandle;
75 :     # Determine whether or not this is a primary relation.
76 :     my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
77 : parrello 1.10 # Check to see if this is a load-only, ignore, or a generate-and-load.
78 :     if ($ignore) {
79 :     Trace("Relation $relationName will be ignored.") if T(2);
80 :     $fileHandle = "";
81 :     } elsif ($loadOnly) {
82 : parrello 1.8 Trace("Relation $relationName will be loaded from $fileName.") if T(2);
83 :     $fileHandle = "";
84 :     } else {
85 : parrello 1.16 # Determine the sort for this relation.
86 : parrello 1.17 my $fileString = "| " . $erdb->SortNeeded($relationName) . " >$fileName";
87 : parrello 1.18 Trace("Load file creation string is \"$fileString\".") if T(3);
88 : parrello 1.8 # Open the output file and remember its handle.
89 :     $fileHandle = Open(undef, $fileString);
90 :     Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
91 :     }
92 : parrello 1.1 # Create the $erload object.
93 :     my $retVal = {
94 :     dbh => $erdb,
95 :     fh => $fileHandle,
96 :     fileName => $fileName,
97 :     relName => $relationName,
98 :     fileSize => 0,
99 :     lineCount => 0,
100 :     stats => Stats->new(),
101 : parrello 1.10 primary => $primary,
102 :     ignore => ($ignore ? 1 : 0)
103 : parrello 1.1 };
104 :     # Bless and return it.
105 :     bless $retVal, $class;
106 :     return $retVal;
107 :     }
108 :    
109 : parrello 1.10 =head3 Ignore
110 :    
111 : parrello 1.20 my $flag = $erload->Ignore;
112 : parrello 1.10
113 :     Return TRUE if we are ignoring this table, else FALSE.
114 :    
115 :     =cut
116 :     #: Return Type $;
117 :     sub Ignore {
118 :     # Get the parameters.
119 :     my ($self) = @_;
120 :     # Return the result.
121 :     return $self->{ignore};
122 :     }
123 :    
124 : parrello 1.1 =head3 Put
125 :    
126 : parrello 1.20 my = $erload->Put($field1, $field2, ..., $fieldN);
127 : parrello 1.1
128 :     Write a line of data to the load file. This may also cause the load file to be closed
129 :     and data read into the table.
130 :    
131 :     =over 4
132 :    
133 :     =item field1, field2, ..., fieldN
134 :    
135 :     List of field values to be put into the data line. The field values must be in the
136 :     order determined shown in the documentation for the table. Internal tabs and
137 :     new-lines will automatically be escaped before the data line is formatted.
138 :    
139 :     =back
140 :    
141 :     =cut
142 :     #: Return Type ;
143 :     sub Put {
144 : parrello 1.9 # Get the ERDBLoad instance and the field list.
145 :     my ($self, @rawFields) = @_;
146 : parrello 1.10 # Only proceed if we're not ignoring.
147 :     if (! $self->{ignore}) {
148 : parrello 1.13 # Convert the hash-string fields to their digested value.
149 :     $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
150 : parrello 1.10 # 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.
153 :     my @fields = map { Tracer::Escape($_) } @rawFields;
154 :     # Form a data line from the fields.
155 :     my $line = join("\t", @fields) . "\n";
156 :     # Write the new record to the load file.
157 :     my $fh = $self->{fh};
158 :     print $fh $line;
159 :     # Determine how long this will make the load file.
160 :     my $lineLength = length $line;
161 :     # Update the statistics.
162 :     $self->{fileSize} += $lineLength;
163 :     $self->{lineCount} ++;
164 :     $self->Add("lineOut");
165 :     if ($truncates > 0) {
166 :     $self->Add("truncated", $truncates);
167 :     }
168 : parrello 1.9 }
169 : parrello 1.4 }
170 :    
171 :     =head3 Add
172 :    
173 : parrello 1.20 my = $stats->Add($statName, $value);
174 : parrello 1.4
175 :     Increment the specified statistic.
176 :    
177 :     =over 4
178 :    
179 :     =item statName
180 :    
181 :     Name of the statistic to increment.
182 :    
183 : parrello 1.9 =item value (optional)
184 :    
185 :     Value by which to increment it. If omitted, C<1> is assumed.
186 :    
187 : parrello 1.4 =back
188 :    
189 :     =cut
190 :     #: Return Type ;
191 :     sub Add {
192 :     # Get the parameters.
193 : parrello 1.9 my ($self, $statName, $value) = @_;
194 :     # Fix the value.
195 :     if (! defined $value) {
196 :     $value = 1;
197 :     }
198 : parrello 1.4 # Increment the statistic.
199 : parrello 1.9 $self->{stats}->Add($statName, $value);
200 : parrello 1.1 }
201 :    
202 :     =head3 Finish
203 :    
204 : parrello 1.20 my $stats = $erload->Finish();
205 : parrello 1.1
206 : parrello 1.14 Finish loading the table. This closes the load file.
207 : parrello 1.1
208 :     =over 4
209 :    
210 :     =item RETURN
211 :    
212 :     Returns a statistics object describing what happened during the load and containing any
213 :     error messages.
214 :    
215 :     =back
216 :    
217 :     =cut
218 :    
219 :     sub Finish {
220 :     # Get this object instance.
221 :     my ($self) = @_;
222 : parrello 1.10 if ($self->{fh}) {
223 :     # Close the load file.
224 :     close $self->{fh};
225 :     }
226 : parrello 1.1 # Return the statistics object.
227 :     return $self->{stats};
228 :     }
229 :    
230 : parrello 1.19 =head3 FinishAndLoad
231 :    
232 : parrello 1.20 my $stats = $erload->FinishAndLoad();
233 : parrello 1.19
234 :     Finish the load and load the table, returning the statistics.
235 :    
236 :     =cut
237 :    
238 :     sub FinishAndLoad {
239 :     # Get the parameters.
240 :     my ($self) = @_;
241 :     # Finish the load file.
242 :     my $retVal = $self->Finish();
243 :     # Load the table.
244 :     my $newStats = $self->LoadTable();
245 :     # Accumulate the stats.
246 :     $retVal->Accumulate($newStats);
247 :     # Return the result.
248 :     return $retVal;
249 :     }
250 :    
251 : parrello 1.1 =head3 RelName
252 :    
253 : parrello 1.20 my $name = $erload->RelName;
254 : parrello 1.1
255 :     Name of the relation being loaded by this object.
256 :    
257 :     =cut
258 :    
259 :     sub RelName {
260 :     # Get the object instance.
261 :     my ($self) = @_;
262 :     # Return the relation name.
263 :     return $self->{relName};
264 :     }
265 :    
266 : parrello 1.19 =head3 LoadTable
267 :    
268 : parrello 1.20 my $stats = $erload->LoadTable();
269 : parrello 1.19
270 :     Load the database table from the load file and return a statistics object.
271 :    
272 :     =cut
273 :    
274 :     sub LoadTable {
275 :     # Get the parameters.
276 :     my ($self) = @_;
277 :     # Get the database object, the file name, and the relation name.
278 :     my $erdb = $self->{dbh};
279 :     my $fileName = $self->{fileName};
280 :     my $relName = $self->{relName};
281 :     # Load the table. The third parameter indicates this is a drop and reload.
282 : parrello 1.21 my $retVal = $erdb->LoadTable($fileName, $relName, truncate => 1);
283 : parrello 1.19 # Return the result.
284 :     return $retVal;
285 :     }
286 :    
287 : parrello 1.1 1;
288 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3