[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.19, Fri Sep 9 14:50:58 2005 UTC revision 1.22, Wed Sep 14 09:56:58 2005 UTC
# Line 783  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 1300  Line 1327 
1327      Trace("Loading table $relationName from $fileName") if T(2);      Trace("Loading table $relationName from $fileName") if T(2);
1328      # Get the database handle.      # Get the database handle.
1329      my $dbh = $self->{_dbh};      my $dbh = $self->{_dbh};
1330        # Get the input file size.
1331        my $fileSize = -s $fileName;
1332      # Get the relation data.      # Get the relation data.
1333      my $relation = $self->_FindRelation($relationName);      my $relation = $self->_FindRelation($relationName);
1334      # Check the truncation flag.      # Check the truncation flag.
# Line 1310  Line 1339 
1339          # leave extra room. We postulate a minimum row count of 1000 to          # leave extra room. We postulate a minimum row count of 1000 to
1340          # prevent problems with incoming empty load files.          # prevent problems with incoming empty load files.
1341          my $rowSize = $self->EstimateRowSize($relationName);          my $rowSize = $self->EstimateRowSize($relationName);
         my $fileSize = -s $fileName;  
1342          my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);          my $estimate = FIG::max($fileSize * 1.5 / $rowSize, 1000);
1343          # Re-create the table without its index.          # Re-create the table without its index.
1344          $self->CreateTable($relationName, 0, $estimate);          $self->CreateTable($relationName, 0, $estimate);
# Line 1324  Line 1352 
1352              }              }
1353          }          }
1354      }      }
     # 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);  
1355      # Load the table.      # Load the table.
1356      my $rv;      my $rv;
1357      eval {      eval {
1358          $rv = $dbh->load_table(file => $tempName, tbl => $relationName);          $rv = $dbh->load_table(file => $fileName, tbl => $relationName);
1359      };      };
1360      if (!defined $rv) {      if (!defined $rv) {
1361          $retVal->AddMessage($@) if ($@);          $retVal->AddMessage($@) if ($@);
1362          $retVal->AddMessage("Table load failed for $relationName using $tempName.");          $retVal->AddMessage("Table load failed for $relationName using $fileName.");
1363          Trace("Table load failed for $relationName.") if T(1);          Trace("Table load failed for $relationName.") if T(1);
1364      } else {      } else {
1365          # Here we successfully loaded the table. Trace the number of records loaded.          # Here we successfully loaded the table.
1366          Trace("$retVal->{records} records read for $relationName.") if T(2);          $retVal->Add("tables");
1367            my $size = -s $fileName;
1368            Trace("$size bytes loaded into $relationName.") if T(2);
1369          # If we're rebuilding, we need to create the table indexes.          # If we're rebuilding, we need to create the table indexes.
1370          if ($truncateFlag && ! $dbh->{_preIndex}) {          if ($truncateFlag && ! $dbh->{_preIndex}) {
1371              eval {              eval {
# Line 1400  Line 1375 
1375                  $retVal->AddMessage($@);                  $retVal->AddMessage($@);
1376              }              }
1377          }          }
         # Analyze the table to help optimize tables.  
1378      }      }
1379      # Commit the database changes.      # Commit the database changes.
1380      $dbh->commit_tran;      $dbh->commit_tran;
1381        # Analyze the table to improve performance.
1382      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
     # Delete the temporary file.  
     unlink $tempName;  
1383      # Return the statistics.      # Return the statistics.
1384      return $retVal;      return $retVal;
1385  }  }
# Line 2255  Line 2228 
2228          my @fromList = ();          my @fromList = ();
2229          my @toList = ();          my @toList = ();
2230          my @bothList = ();          my @bothList = ();
2231          Trace("Join table build for $entityName.") if T(4);          Trace("Join table build for $entityName.") if T(metadata => 4);
2232          for my $relationshipName (keys %{$relationshipList}) {          for my $relationshipName (keys %{$relationshipList}) {
2233              my $relationship = $relationshipList->{$relationshipName};              my $relationship = $relationshipList->{$relationshipName};
2234              # 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 2266  Line 2239 
2239                  if ($toEntity eq $entityName) {                  if ($toEntity eq $entityName) {
2240                      # Here the relationship is recursive.                      # Here the relationship is recursive.
2241                      push @bothList, $relationshipName;                      push @bothList, $relationshipName;
2242                      Trace("Relationship $relationshipName put in both-list.") if T(4);                      Trace("Relationship $relationshipName put in both-list.") if T(metadata => 4);
2243                  } else {                  } else {
2244                      # Here the relationship comes from the entity.                      # Here the relationship comes from the entity.
2245                      push @fromList, $relationshipName;                      push @fromList, $relationshipName;
2246                      Trace("Relationship $relationshipName put in from-list.") if T(4);                      Trace("Relationship $relationshipName put in from-list.") if T(metadata => 4);
2247                  }                  }
2248              } elsif ($toEntity eq $entityName) {              } elsif ($toEntity eq $entityName) {
2249                  # Here the relationship goes to the entity.                  # Here the relationship goes to the entity.
2250                  push @toList, $relationshipName;                  push @toList, $relationshipName;
2251                  Trace("Relationship $relationshipName put in to-list.") if T(4);                  Trace("Relationship $relationshipName put in to-list.") if T(metadata => 4);
2252              }              }
2253          }          }
2254          # 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 2291  Line 2264 
2264                  # Create joins between the entity and this relationship.                  # Create joins between the entity and this relationship.
2265                  my $linkField = "$relationshipName.${linkType}_link";                  my $linkField = "$relationshipName.${linkType}_link";
2266                  my $joinClause = "$entityName.id = $linkField";                  my $joinClause = "$entityName.id = $linkField";
2267                  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);
2268                  $joinTable{"$entityName/$relationshipName"} = $joinClause;                  $joinTable{"$entityName/$relationshipName"} = $joinClause;
2269                  $joinTable{"$relationshipName/$entityName"} = $joinClause;                  $joinTable{"$relationshipName/$entityName"} = $joinClause;
2270                  # Create joins between this relationship and the other relationships.                  # Create joins between this relationship and the other relationships.
# Line 2312  Line 2285 
2285                              # relationship and itself are prohibited.                              # relationship and itself are prohibited.
2286                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";                              my $relJoinClause = "$otherName.${otherType}_link = $linkField";
2287                              $joinTable{$joinKey} = $relJoinClause;                              $joinTable{$joinKey} = $relJoinClause;
2288                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(4);                              Trace("Relationship join clause is $relJoinClause for $joinKey.") if T(metadata => 4);
2289                          }                          }
2290                      }                      }
2291                  }                  }
# Line 2321  Line 2294 
2294                  # relationship can only be ambiguous with another recursive relationship,                  # relationship can only be ambiguous with another recursive relationship,
2295                  # and the incoming relationship from the outer loop is never recursive.                  # and the incoming relationship from the outer loop is never recursive.
2296                  for my $otherName (@bothList) {                  for my $otherName (@bothList) {
2297                      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);
2298                      # Join from the left.                      # Join from the left.
2299                      $joinTable{"$relationshipName/$otherName"} =                      $joinTable{"$relationshipName/$otherName"} =
2300                          "$linkField = $otherName.from_link";                          "$linkField = $otherName.from_link";
# Line 2336  Line 2309 
2309          # 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
2310          # possible to get the same effect using multiple queries.          # possible to get the same effect using multiple queries.
2311          for my $relationshipName (@bothList) {          for my $relationshipName (@bothList) {
2312              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);
2313              # Join to the entity from each direction.              # Join to the entity from each direction.
2314              $joinTable{"$entityName/$relationshipName"} =              $joinTable{"$entityName/$relationshipName"} =
2315                  "$entityName.id = $relationshipName.from_link";                  "$entityName.id = $relationshipName.from_link";

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.22

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3