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

Annotation of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (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 :     C<< my $erload = ERDBLoad->new($erdb, $relationName, $directory); >>
32 :    
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.1 =back
55 :    
56 :     =cut
57 :    
58 :     sub new {
59 :     # Get the parameters.
60 : parrello 1.8 my ($class, $erdb, $relationName, $directory, $loadOnly) = @_;
61 : parrello 1.1 # Validate the directory name.
62 :     if (! -d $directory) {
63 :     Confess("Load directory \"$directory\" not found.");
64 :     }
65 :     # Determine the name for this relation's load file.
66 :     my $fileName = "$directory/$relationName.dtx";
67 : parrello 1.8 # Declare the file handle variable.
68 :     my $fileHandle;
69 :     # Determine whether or not this is a primary relation.
70 :     my $primary = ($erdb->_IsPrimary($relationName) ? 1 : 0);
71 :     # Check to see if this is a load-only or a generate-and-load.
72 :     if ($loadOnly) {
73 :     Trace("Relation $relationName will be loaded from $fileName.") if T(2);
74 :     $fileHandle = "";
75 :     } else {
76 :     # If this is a primary entity relation, sort the output to remove
77 :     # duplicate keys.
78 :     my $fileString = ($erdb->IsEntity($relationName) ?
79 :     "| sort +0 -1 -u -t \"\t\" >$fileName" :
80 :     ">$fileName");
81 :     # Open the output file and remember its handle.
82 :     $fileHandle = Open(undef, $fileString);
83 :     Trace("Relation $relationName load file created with primary flag $primary.") if T(2);
84 :     }
85 : parrello 1.1 # Create the $erload object.
86 :     my $retVal = {
87 :     dbh => $erdb,
88 :     fh => $fileHandle,
89 :     fileName => $fileName,
90 :     relName => $relationName,
91 :     fileSize => 0,
92 :     lineCount => 0,
93 :     stats => Stats->new(),
94 : parrello 1.8 primary => $primary
95 : parrello 1.1 };
96 :     # Bless and return it.
97 :     bless $retVal, $class;
98 :     return $retVal;
99 :     }
100 :    
101 :     =head3 Put
102 :    
103 :     C<< my = $erload->Put($field1, $field2, ..., $fieldN); >>
104 :    
105 :     Write a line of data to the load file. This may also cause the load file to be closed
106 :     and data read into the table.
107 :    
108 :     =over 4
109 :    
110 :     =item field1, field2, ..., fieldN
111 :    
112 :     List of field values to be put into the data line. The field values must be in the
113 :     order determined shown in the documentation for the table. Internal tabs and
114 :     new-lines will automatically be escaped before the data line is formatted.
115 :    
116 :     =back
117 :    
118 :     =cut
119 :     #: Return Type ;
120 :     sub Put {
121 : parrello 1.9 # Get the ERDBLoad instance and the field list.
122 :     my ($self, @rawFields) = @_;
123 :     # Insure the field values are okay.
124 :     my $truncates = $self->{dbh}->VerifyFields($self->{relName}, \@rawFields);
125 : parrello 1.1 # Run through the list of field values, escaping them.
126 : parrello 1.9 my @fields = map { Tracer::Escape($_) } @rawFields;
127 : parrello 1.1 # If this is a primary relation, append the new-record field.
128 :     if ($self->{primary}) {
129 :     push @fields, '0';
130 :     }
131 :     # Form a data line from the fields.
132 :     my $line = join("\t", @fields) . "\n";
133 :     # Write the new record to the load file.
134 :     my $fh = $self->{fh};
135 :     print $fh $line;
136 : parrello 1.3 # Determine how long this will make the load file.
137 :     my $lineLength = length $line;
138 : parrello 1.1 # Update the statistics.
139 :     $self->{fileSize} += $lineLength;
140 :     $self->{lineCount} ++;
141 : parrello 1.4 $self->Add("lineOut");
142 : parrello 1.9 if ($truncates > 0) {
143 :     $self->Add("truncated", $truncates);
144 :     }
145 : parrello 1.4 }
146 :    
147 :     =head3 Add
148 :    
149 : parrello 1.9 C<< my = $stats->Add($statName, $value); >>
150 : parrello 1.4
151 :     Increment the specified statistic.
152 :    
153 :     =over 4
154 :    
155 :     =item statName
156 :    
157 :     Name of the statistic to increment.
158 :    
159 : parrello 1.9 =item value (optional)
160 :    
161 :     Value by which to increment it. If omitted, C<1> is assumed.
162 :    
163 : parrello 1.4 =back
164 :    
165 :     =cut
166 :     #: Return Type ;
167 :     sub Add {
168 :     # Get the parameters.
169 : parrello 1.9 my ($self, $statName, $value) = @_;
170 :     # Fix the value.
171 :     if (! defined $value) {
172 :     $value = 1;
173 :     }
174 : parrello 1.4 # Increment the statistic.
175 : parrello 1.9 $self->{stats}->Add($statName, $value);
176 : parrello 1.1 }
177 :    
178 :     =head3 Finish
179 :    
180 :     C<< my $stats = $erload->Finish(); >>
181 :    
182 :     Finish loading the table. This closes the load file and loads its contents into the database.
183 :     It also creates the indexes if the DBMS uses post-indexing.
184 :    
185 :     =over 4
186 :    
187 :     =item RETURN
188 :    
189 :     Returns a statistics object describing what happened during the load and containing any
190 :     error messages.
191 :    
192 :     =back
193 :    
194 :     =cut
195 :    
196 :     sub Finish {
197 :     # Get this object instance.
198 :     my ($self) = @_;
199 : parrello 1.3 # Close the load file.
200 :     close $self->{fh};
201 : parrello 1.1 # Return the statistics object.
202 :     return $self->{stats};
203 :     }
204 :    
205 :     =head3 RelName
206 :    
207 :     C<< my $name = $erload->RelName; >>
208 :    
209 :     Name of the relation being loaded by this object.
210 :    
211 :     =cut
212 :    
213 :     sub RelName {
214 :     # Get the object instance.
215 :     my ($self) = @_;
216 :     # Return the relation name.
217 :     return $self->{relName};
218 :     }
219 :    
220 :     1;
221 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3