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

Diff of /Sprout/ERDB.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.18, Sun Aug 14 23:32:08 2005 UTC revision 1.21, Tue Sep 13 18:32:02 2005 UTC
# Line 9  Line 9 
9      use DBObject;      use DBObject;
10      use Stats;      use Stats;
11      use Time::HiRes qw(gettimeofday);      use Time::HiRes qw(gettimeofday);
12        use FIG;
13    
14  =head1 Entity-Relationship Database Package  =head1 Entity-Relationship Database Package
15    
# Line 782  Line 783 
783      return sort keys %{$entityList};      return sort keys %{$entityList};
784  }  }
785    
786    =head3 IsEntity
787    
788    C<< my $flag = $erdb->IsEntity($entityName); >>
789    
790    Return TRUE if the parameter is an entity name, else FALSE.
791    
792    =over 4
793    
794    =item entityName
795    
796    Object name to be tested.
797    
798    =item RETURN
799    
800    Returns TRUE if the specified string is an entity name, else FALSE.
801    
802    =back
803    
804    =cut
805    
806    sub IsEntity {
807        # Get the parameters.
808        my ($self, $entityName) = @_;
809        # Test to see if it's an entity.
810        return exists $self->{_metaData}->{Entities}->{$entityName};
811    }
812    
813  =head3 Get  =head3 Get
814    
815  C<< my $query = $erdb->Get(\@objectNames, $filterClause, $param1, $param2, ..., $paramN); >>  C<< my $query = $erdb->Get(\@objectNames, $filterClause, $param1, $param2, ..., $paramN); >>
# Line 1304  Line 1332 
1332      # Check the truncation flag.      # Check the truncation flag.
1333      if ($truncateFlag) {      if ($truncateFlag) {
1334          Trace("Creating table $relationName") if T(2);          Trace("Creating table $relationName") if T(2);
1335            # Compute the row count estimate. We take the size of the load file,
1336            # divide it by the estimated row size, and then multiply by 1.5 to
1337            # leave extra room. We postulate a minimum row count of 1000 to
1338            # prevent problems with incoming empty load files.
1339            my $rowSize = $self->EstimateRowSize($relationName);
1340            my $fileSize = -s $fileName;
1341            my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);
1342          # Re-create the table without its index.          # Re-create the table without its index.
1343          $self->CreateTable($relationName, 0);          $self->CreateTable($relationName, 0, $estimate);
1344          # If this is a pre-index DBMS, create the index here.          # If this is a pre-index DBMS, create the index here.
1345          if ($dbh->{_preIndex}) {          if ($dbh->{_preIndex}) {
1346              eval {              eval {
# Line 1316  Line 1351 
1351              }              }
1352          }          }
1353      }      }
     # Determine whether or not this is a primary relation. Primary relations have an extra  
     # field indicating whether or not a given object is new or was loaded from the flat files.  
     my $primary = $self->_IsPrimary($relationName);  
     # Get the number of fields in this relation.  
     my @fieldList = @{$relation->{Fields}};  
     my $fieldCount = @fieldList;  
     # Start a database transaction.  
     $dbh->begin_tran;  
     # Open the relation file. We need to create a cleaned-up copy before loading.  
     open TABLEIN, '<', $fileName;  
     my $tempName = "$fileName.tbl";  
     open TABLEOUT, '>', $tempName;  
     my $inputCount = 0;  
     # Loop through the file.  
     while (<TABLEIN>) {  
         $inputCount++;  
         # Chop off the new-line character.  
         my $record = Tracer::Strip($_);  
         # Only proceed if the record is non-blank.  
         if ($record) {  
             # Escape all the backslashes found in the line.  
             $record =~ s/\\/\\\\/g;  
             # Insure the number of fields is correct.  
             my @fields = split /\t/, $record;  
             while (@fields > $fieldCount) {  
                 my $extraField = $fields[$#fields];  
                 delete $fields[$#fields];  
                 if ($extraField) {  
                     Trace("Nonblank extra field value \"$extraField\" deleted from record $inputCount of $fileName.") if T(1);  
                 }  
             }  
             while (@fields < $fieldCount) {  
                 push @fields, "";  
             }  
             # If this is a primary relation, add a 0 for the new-record flag (indicating that  
             # this record is not new, but part of the original load).  
             if ($primary) {  
                 push @fields, "0";  
             }  
             # Write the record.  
             $record = join "\t", @fields;  
             print TABLEOUT "$record\n";  
             # Count the record written.  
             my $count = $retVal->Add('records');  
             my $len = length $record;  
             Trace("Record $count written with $len characters.") if T(4);  
         } else {  
             # Here we have a blank record.  
             $retVal->Add('skipped');  
         }  
     }  
     # Close the files.  
     close TABLEIN;  
     close TABLEOUT;  
     Trace("Temporary file $tempName created.") if T(2);  
1354      # Load the table.      # Load the table.
1355      my $rv;      my $rv;
1356      eval {      eval {
1357          $rv = $dbh->load_table(file => $tempName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);
1358      };      };
1359      if (!defined $rv) {      if (!defined $rv) {
1360          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
1361          $retVal->AddMessage("Table load failed for $relationName using $tempName.");          $retVal->AddMessage("Table load failed for $relationName using $fileName.");
1362          Trace("Table load failed for $relationName.") if T(1);          Trace("Table load failed for $relationName.") if T(1);
1363      } else {      } else {
1364          # Here we successfully loaded the table. Trace the number of records loaded.          # Here we successfully loaded the table. Trace the number of records loaded.
# Line 1392  Line 1372 
1372                  $retVal->AddMessage($@);                  $retVal->AddMessage($@);
1373              }              }
1374          }          }
         # Analyze the table to help optimize tables.  
1375      }      }
1376      # Commit the database changes.      # Commit the database changes.
1377      $dbh->commit_tran;      $dbh->commit_tran;
1378        # Analyze the table to improve performance.
1379      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
     # Delete the temporary file.  
     unlink $tempName;  
1380      # Return the statistics.      # Return the statistics.
1381      return $retVal;      return $retVal;
1382  }  }
# Line 2247  Line 2225 
2225          my @fromList = ();          my @fromList = ();
2226          my @toList = ();          my @toList = ();
2227          my @bothList = ();          my @bothList = ();
2228          Trace("Join table build for $entityName.") if T(4);          Trace("Join table build for $entityName.") if T(metadata => 4);
2229          for my $relationshipName (keys %{$relationshipList}) {          for my $relationshipName (keys %{$relationshipList}) {
2230              my $relationship = $relationshipList->{$relationshipName};              my $relationship = $relationshipList->{$relationshipName};
2231              # Determine if this relationship has our entity in one of its link fields.              # Determine if this relationship has our entity in one of its link fields.
# Line 2258  Line 2236 
2236                  if ($toEntity eq $entityName) {                  if ($toEntity eq $entityName) {
2237                      # Here the relationship is recursive.                      # Here the relationship is recursive.
2238                      push @bothList, $relationshipName;                      push @bothList, $relationshipName;
2239                      Trace("Relationship $relationshipName put in both-list.") if T(4);                      Trace("Relationship $relationshipName put in both-list.") if T(metadata => 4);
2240                  } else {                  } else {
2241                      # Here the relationship comes from the entity.                      # Here the relationship comes from the entity.
2242                      push @fromList, $relationshipName;                      push @fromList, $relationshipName;
2243                      Trace("Relationship $relationshipName put in from-list.") if T(4);                      Trace("Relationship $relationshipName put in from-list.") if T(metadata => 4);
2244                  }                  }
2245              } elsif ($toEntity eq $entityName) {              } elsif ($toEntity eq $entityName) {
2246                  # Here the relationship goes to the entity.                  # Here the relationship goes to the entity.
2247                  push @toList, $relationshipName;                  push @toList, $relationshipName;
2248                  Trace("Relationship $relationshipName put in to-list.") if T(4);                  Trace("Relationship $relationshipName put in to-list.") if T(metadata => 4);
2249              }              }
2250          }          }
2251          # Create the nonrecursive joins. Note that we build two hashes for running          # Create the nonrecursive joins. Note that we build two hashes for running
# Line 2283  Line 2261 
2261                  # Create joins between the entity and this relationship.                  # Create joins between the entity and this relationship.
2262                  my $linkField = "$relationshipName.${linkType}_link";                  my $linkField = "$relationshipName.${linkType}_link";
2263                  my $joinClause = "$entityName.id = $linkField";                  my $joinClause = "$entityName.id = $linkField";
2264                  Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(4);                  Trace("Entity join clause is $joinClause for $entityName and $relationshipName.") if T(metadata => 4);
2265                  $joinTable{"$entityName/$relationshipName"} = $joinClause;                  $joinTable{"$entityName/$relationshipName"} = $joinClause;
2266                  $joinTable{"$relationshipName/$entityName"} = $joinClause;                  $joinTable{"$relationshipName/$entityName"} = $joinClause;
2267                  # Create joins between this relationship and the other relationships.                  # Create joins between this relationship and the other relationships.
# Line 2304  Line 2282 
2282                              # relationship and itself are prohibited.                              # relationship and itself are prohibited.
2283                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";
2284                              $joinTable{$joinKey} = $relJoinClause;                              $joinTable{$joinKey} = $relJoinClause;
2285                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(4);                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(metadata => 4);
2286                          }                          }
2287                      }                      }
2288                  }                  }
# Line 2313  Line 2291 
2291                  # relationship can only be ambiguous with another recursive relationship,                  # relationship can only be ambiguous with another recursive relationship,
2292                  # and the incoming relationship from the outer loop is never recursive.                  # and the incoming relationship from the outer loop is never recursive.
2293                  for my $otherName (@bothList) {                  for my $otherName (@bothList) {
2294                      Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(4);                      Trace("Setting up relationship joins to recursive relationship $otherName with $relationshipName.") if T(metadata => 4);
2295                      # Join from the left.                      # Join from the left.
2296                      $joinTable{"$relationshipName/$otherName"} =                      $joinTable{"$relationshipName/$otherName"} =
2297                          "$linkField = $otherName.from_link";                          "$linkField = $otherName.from_link";
# Line 2328  Line 2306 
2306          # rise to situations where we can't create the path we want; however, it is always          # rise to situations where we can't create the path we want; however, it is always
2307          # possible to get the same effect using multiple queries.          # possible to get the same effect using multiple queries.
2308          for my $relationshipName (@bothList) {          for my $relationshipName (@bothList) {
2309              Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(4);              Trace("Setting up entity joins to recursive relationship $relationshipName with $entityName.") if T(metadata => 4);
2310              # Join to the entity from each direction.              # Join to the entity from each direction.
2311              $joinTable{"$entityName/$relationshipName"} =              $joinTable{"$entityName/$relationshipName"} =
2312                  "$entityName.id = $relationshipName.from_link";                  "$entityName.id = $relationshipName.from_link";

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.21

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3