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

Annotation of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (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.12 C<< 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 :     my $fileString = "| " . $erdb->SortNeeded($relationName) . ">$fileName";
87 : parrello 1.8 # Open the output file and remember its handle.
88 :     $fileHandle = Open(undef, $fileString);
89 :     Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
90 :     }
91 : parrello 1.1 # Create the $erload object.
92 :     my $retVal = {
93 :     dbh => $erdb,
94 :     fh => $fileHandle,
95 :     fileName => $fileName,
96 :     relName => $relationName,
97 :     fileSize => 0,
98 :     lineCount => 0,
99 :     stats => Stats->new(),
100 : parrello 1.10 primary => $primary,
101 :     ignore => ($ignore ? 1 : 0)
102 : parrello 1.1 };
103 :     # Bless and return it.
104 :     bless $retVal, $class;
105 :     return $retVal;
106 :     }
107 :    
108 : parrello 1.10 =head3 Ignore
109 :    
110 : parrello 1.11 C<< my $flag = $erload->Ignore; >>
111 : parrello 1.10
112 :     Return TRUE if we are ignoring this table, else FALSE.
113 :    
114 :     =cut
115 :     #: Return Type $;
116 :     sub Ignore {
117 :     # Get the parameters.
118 :     my ($self) = @_;
119 :     # Return the result.
120 :     return $self->{ignore};
121 :     }
122 :    
123 : parrello 1.1 =head3 Put
124 :    
125 :     C<< my = $erload->Put($field1, $field2, ..., $fieldN); >>
126 :    
127 :     Write a line of data to the load file. This may also cause the load file to be closed
128 :     and data read into the table.
129 :    
130 :     =over 4
131 :    
132 :     =item field1, field2, ..., fieldN
133 :    
134 :     List of field values to be put into the data line. The field values must be in the
135 :     order determined shown in the documentation for the table. Internal tabs and
136 :     new-lines will automatically be escaped before the data line is formatted.
137 :    
138 :     =back
139 :    
140 :     =cut
141 :     #: Return Type ;
142 :     sub Put {
143 : parrello 1.9 # Get the ERDBLoad instance and the field list.
144 :     my ($self, @rawFields) = @_;
145 : parrello 1.10 # Only proceed if we're not ignoring.
146 :     if (! $self->{ignore}) {
147 : parrello 1.13 # Convert the hash-string fields to their digested value.
148 :     $self->{dbh}->DigestFields($self->{relName}, \@rawFields);
149 : parrello 1.10 # Insure the field values are okay.
150 :     my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
151 :     # Run through the list of field values, escaping them.
152 :     my @fields = map { Tracer::Escape($_) } @rawFields;
153 :     # If this is a primary relation, append the new-record field.
154 :     if ($self->{primary}) {
155 :     push @fields, '0';
156 :     }
157 :     # Form a data line from the fields.
158 :     my $line = join("\t", @fields) . "\n";
159 :     # Write the new record to the load file.
160 :     my $fh = $self->{fh};
161 :     print $fh $line;
162 :     # Determine how long this will make the load file.
163 :     my $lineLength = length $line;
164 :     # Update the statistics.
165 :     $self->{fileSize} += $lineLength;
166 :     $self->{lineCount} ++;
167 :     $self->Add("lineOut");
168 :     if ($truncates > 0) {
169 :     $self->Add("truncated", $truncates);
170 :     }
171 : parrello 1.9 }
172 : parrello 1.4 }
173 :    
174 :     =head3 Add
175 :    
176 : parrello 1.9 C<< my = $stats->Add($statName, $value); >>
177 : parrello 1.4
178 :     Increment the specified statistic.
179 :    
180 :     =over 4
181 :    
182 :     =item statName
183 :    
184 :     Name of the statistic to increment.
185 :    
186 : parrello 1.9 =item value (optional)
187 :    
188 :     Value by which to increment it. If omitted, C<1> is assumed.
189 :    
190 : parrello 1.4 =back
191 :    
192 :     =cut
193 :     #: Return Type ;
194 :     sub Add {
195 :     # Get the parameters.
196 : parrello 1.9 my ($self, $statName, $value) = @_;
197 :     # Fix the value.
198 :     if (! defined $value) {
199 :     $value = 1;
200 :     }
201 : parrello 1.4 # Increment the statistic.
202 : parrello 1.9 $self->{stats}->Add($statName, $value);
203 : parrello 1.1 }
204 :    
205 :     =head3 Finish
206 :    
207 :     C<< my $stats = $erload->Finish(); >>
208 :    
209 : parrello 1.14 Finish loading the table. This closes the load file.
210 : parrello 1.1
211 :     =over 4
212 :    
213 :     =item RETURN
214 :    
215 :     Returns a statistics object describing what happened during the load and containing any
216 :     error messages.
217 :    
218 :     =back
219 :    
220 :     =cut
221 :    
222 :     sub Finish {
223 :     # Get this object instance.
224 :     my ($self) = @_;
225 : parrello 1.10 if ($self->{fh}) {
226 :     # Close the load file.
227 :     close $self->{fh};
228 :     }
229 : parrello 1.1 # Return the statistics object.
230 :     return $self->{stats};
231 :     }
232 :    
233 :     =head3 RelName
234 :    
235 :     C<< my $name = $erload->RelName; >>
236 :    
237 :     Name of the relation being loaded by this object.
238 :    
239 :     =cut
240 :    
241 :     sub RelName {
242 :     # Get the object instance.
243 :     my ($self) = @_;
244 :     # Return the relation name.
245 :     return $self->{relName};
246 :     }
247 :    
248 :     1;
249 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3