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

Annotation of /Sprout/ERDBLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (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.2 =item estimatedRows (optional)
50 :    
51 :     Estimated maximum number of table rows. If omitted, the table will be created in
52 :     a format that permits an essentially unlimited number of rows.
53 :    
54 : parrello 1.1 =back
55 :    
56 :     =cut
57 :    
58 :     sub new {
59 :     # Get the parameters.
60 : parrello 1.2 my ($class, $erdb, $relationName, $directory, $estimatedRows) = @_;
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.4 # If this is a primary entity relation, sort the output to remove
68 :     # duplicate keys.
69 :     my $fileString = ($erdb->IsEntity($relationName) ?
70 : parrello 1.5 "| sort +0 -1 -u -t \\t >$fileName" :
71 : parrello 1.4 ">$fileName");
72 : parrello 1.1 # Open the output file and remember its handle.
73 : parrello 1.4 my $fileHandle = Open(undef, $fileString);
74 : parrello 1.1 # Create the $erload object.
75 :     my $retVal = {
76 :     dbh => $erdb,
77 :     fh => $fileHandle,
78 :     fileName => $fileName,
79 :     relName => $relationName,
80 :     fileSize => 0,
81 :     lineCount => 0,
82 :     stats => Stats->new(),
83 :     primary => $erdb->_IsPrimary($relationName)
84 :     };
85 :     # Bless and return it.
86 :     bless $retVal, $class;
87 :     return $retVal;
88 :     }
89 :    
90 :     =head3 Put
91 :    
92 :     C<< my = $erload->Put($field1, $field2, ..., $fieldN); >>
93 :    
94 :     Write a line of data to the load file. This may also cause the load file to be closed
95 :     and data read into the table.
96 :    
97 :     =over 4
98 :    
99 :     =item field1, field2, ..., fieldN
100 :    
101 :     List of field values to be put into the data line. The field values must be in the
102 :     order determined shown in the documentation for the table. Internal tabs and
103 :     new-lines will automatically be escaped before the data line is formatted.
104 :    
105 :     =back
106 :    
107 :     =cut
108 :     #: Return Type ;
109 :     sub Put {
110 :     # Get the ERDBLoad instance.
111 :     my $self = shift @_;
112 :     # Run through the list of field values, escaping them.
113 :     my @fields = map { Tracer::Escape($_) } @_;
114 :     # If this is a primary relation, append the new-record field.
115 :     if ($self->{primary}) {
116 :     push @fields, '0';
117 :     }
118 :     # Form a data line from the fields.
119 :     my $line = join("\t", @fields) . "\n";
120 :     # Write the new record to the load file.
121 :     my $fh = $self->{fh};
122 :     print $fh $line;
123 : parrello 1.3 # Determine how long this will make the load file.
124 :     my $lineLength = length $line;
125 : parrello 1.1 # Update the statistics.
126 :     $self->{fileSize} += $lineLength;
127 :     $self->{lineCount} ++;
128 : parrello 1.4 $self->Add("lineOut");
129 :     }
130 :    
131 :     =head3 Add
132 :    
133 :     C<< my = $stats->Add($statName); >>
134 :    
135 :     Increment the specified statistic.
136 :    
137 :     =over 4
138 :    
139 :     =item statName
140 :    
141 :     Name of the statistic to increment.
142 :    
143 :     =back
144 :    
145 :     =cut
146 :     #: Return Type ;
147 :     sub Add {
148 :     # Get the parameters.
149 :     my ($self, $statName) = @_;
150 :     # Increment the statistic.
151 :     $self->{stats}->Add($statName);
152 : parrello 1.1 }
153 :    
154 :     =head3 Finish
155 :    
156 :     C<< my $stats = $erload->Finish(); >>
157 :    
158 :     Finish loading the table. This closes the load file and loads its contents into the database.
159 :     It also creates the indexes if the DBMS uses post-indexing.
160 :    
161 :     =over 4
162 :    
163 :     =item RETURN
164 :    
165 :     Returns a statistics object describing what happened during the load and containing any
166 :     error messages.
167 :    
168 :     =back
169 :    
170 :     =cut
171 :    
172 :     sub Finish {
173 :     # Get this object instance.
174 :     my ($self) = @_;
175 : parrello 1.3 # Close the load file.
176 :     close $self->{fh};
177 : parrello 1.1 # Return the statistics object.
178 :     return $self->{stats};
179 :     }
180 :    
181 :     =head3 RelName
182 :    
183 :     C<< my $name = $erload->RelName; >>
184 :    
185 :     Name of the relation being loaded by this object.
186 :    
187 :     =cut
188 :    
189 :     sub RelName {
190 :     # Get the object instance.
191 :     my ($self) = @_;
192 :     # Return the relation name.
193 :     return $self->{relName};
194 :     }
195 :    
196 :     1;
197 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3