[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.81, Wed Dec 20 20:03:25 2006 UTC revision 1.87, Sun Feb 18 21:28:19 2007 UTC
# Line 228  Line 228 
228    
229  =head3 Indexes  =head3 Indexes
230    
231  An entity can have multiple alternate indexes associated with it. The fields must  An entity can have multiple alternate indexes associated with it. The fields in an
232  all be from the same relation. The alternate indexes assist in ordering results  index must all be from the same relation. The alternate indexes assist in searching
233  from a query. A relationship can have up to two indexes-- a I<to-index> and a  on fields other than the entity ID. A relationship has at least two indexes-- a I<to-index> and a
234  I<from-index>. These order the results when crossing the relationship. For  I<from-index> that order the results when crossing the relationship. For
235  example, in the relationship C<HasContig> from C<Genome> to C<Contig>, the  example, in the relationship C<HasContig> from C<Genome> to C<Contig>, the
236  from-index would order the contigs of a ganome, and the to-index would order  from-index would order the contigs of a ganome, and the to-index would order
237  the genomes of a contig. A relationship's index must specify only fields in  the genomes of a contig. In addition, it can have zero or more alternate
238    indexes. A relationship's index must specify only fields in
239  the relationship.  the relationship.
240    
241  The indexes for an entity must be listed inside the B<Indexes> tag. The from-index  The alternate indexes for an entity or relationship must be listed inside the B<Indexes> tag.
242  of a relationship is specified using the B<FromIndex> tag; the to-index is specified  The from-index of a relationship is specified using the B<FromIndex> tag; the to-index is
243  using the B<ToIndex> tag.  specified using the B<ToIndex> tag.
244    
245  Each index can contain a B<Notes> tag. In addition, it will have an B<IndexFields>  Each index can contain a B<Notes> tag. In addition, it will have an B<IndexFields>
246  tag containing the B<IndexField> tags. These specify, in order, the fields used in  tag containing the B<IndexField> tags. These specify, in order, the fields used in
# Line 303  Line 304 
304    
305  A relationship is described by the C<Relationship> tag. Within a relationship,  A relationship is described by the C<Relationship> tag. Within a relationship,
306  there can be a C<Notes> tag, a C<Fields> tag containing the intersection data  there can be a C<Notes> tag, a C<Fields> tag containing the intersection data
307  fields, a C<FromIndex> tag containing the from-index, and a C<ToIndex> tag containing  fields, a C<FromIndex> tag containing the from-index, a C<ToIndex> tag containing
308  the to-index.  the to-index, and an C<Indexes> tag containing the alternate indexes.
309    
310  The C<Relationship> tag has the following attributes.  The C<Relationship> tag has the following attributes.
311    
# Line 548  Line 549 
549          if (my $notes = $entityData->{Notes}) {          if (my $notes = $entityData->{Notes}) {
550              $retVal .= "<p>" . HTMLNote($notes->{content}) . "</p>\n";              $retVal .= "<p>" . HTMLNote($notes->{content}) . "</p>\n";
551          }          }
552          # Now we want a list of the entity's relationships. First, we set up the relationship subsection.          # See if we need a list of the entity's relationships.
553            my $relCount = keys %{$relationshipList};
554            if ($relCount > 0) {
555                # First, we set up the relationship subsection.
556          $retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n";          $retVal .= "<h4>Relationships for <b>$key</b></h4>\n<ul>\n";
557          # Loop through the relationships.          # Loop through the relationships.
558          for my $relationship (sort keys %{$relationshipList}) {          for my $relationship (sort keys %{$relationshipList}) {
# Line 564  Line 568 
568          }          }
569          # Close off the relationship list.          # Close off the relationship list.
570          $retVal .= "</ul>\n";          $retVal .= "</ul>\n";
571            }
572          # Get the entity's relations.          # Get the entity's relations.
573          my $relationList = $entityData->{Relations};          my $relationList = $entityData->{Relations};
574          # Create a header for the relation subsection.          # Create a header for the relation subsection.
# Line 739  Line 744 
744      # Loop through the relations.      # Loop through the relations.
745      for my $relationName (@relNames) {      for my $relationName (@relNames) {
746          # Create a table for this relation.          # Create a table for this relation.
747          $self->CreateTable($relationName);          $self->CreateTable($relationName, 1);
748          Trace("Relation $relationName created.") if T(2);          Trace("Relation $relationName created.") if T(2);
749      }      }
750  }  }
# Line 1841  Line 1846 
1846          for my $dir ('from', 'to') {          for my $dir ('from', 'to') {
1847              if ($structure->{$dir} eq $originEntityName) {              if ($structure->{$dir} eq $originEntityName) {
1848                  # Delete all relationship instances on this side of the entity instance.                  # Delete all relationship instances on this side of the entity instance.
1849                    Trace("Disconnecting in $dir direction with ID \"$originEntityID\".");
1850                  $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ?", 0, $originEntityID);                  $dbh->SQL("DELETE FROM $relationshipName WHERE ${dir}_link = ?", 0, $originEntityID);
1851                  $found = 1;                  $found = 1;
1852              }              }
# Line 2428  Line 2434 
2434                  if (!$retVal) {                  if (!$retVal) {
2435                      my $errorString = $sth->errstr();                      my $errorString = $sth->errstr();
2436                      Confess("Error inserting into $relationName: $errorString");                      Confess("Error inserting into $relationName: $errorString");
2437                    } else {
2438                        Trace("Insert successful using $parameterList[0].") if T(3);
2439                  }                  }
2440              }              }
2441          }          }
# Line 2587  Line 2595 
2595              # primary relations are allowed that privilege.              # primary relations are allowed that privilege.
2596              Trace("Checking for full-text index on $relationName.") if T(2);              Trace("Checking for full-text index on $relationName.") if T(2);
2597              if ($self->_IsPrimary($relationName)) {              if ($self->_IsPrimary($relationName)) {
2598                  # Get the relation's entity/relationship structure.                  $self->CreateSearchIndex($relationName);
                 my $structure = $self->_GetStructure($relationName);  
                 Trace("Checking for search fields in $relationName.") if T(3);  
                 # Check for a searchable fields list.  
                 if (exists $structure->{searchFields}) {  
                     # Here we know that we need to create a full-text search index.  
                     # Get an SQL-formatted field name list.  
                     my $fields = join(", ", _FixNames(@{$structure->{searchFields}}));  
                     # Create the index.  
                     $dbh->create_index(tbl => $relationName, idx => "search_idx",  
                                        flds => $fields, kind => 'fulltext');  
                     Trace("Index created for $fields in $relationName.") if T(2);  
                 }  
2599              }              }
2600          }          }
2601      }      }
# Line 2611  Line 2607 
2607      return $retVal;      return $retVal;
2608  }  }
2609    
2610    =head3 CreateSearchIndex
2611    
2612    C<< $erdb->CreateSearchIndex($objectName); >>
2613    
2614    Check for a full-text search index on the specified entity or relationship object, and
2615    if one is required, rebuild it.
2616    
2617    =over 4
2618    
2619    =item objectName
2620    
2621    Name of the entity or relationship to be indexed.
2622    
2623    =back
2624    
2625    =cut
2626    
2627    sub CreateSearchIndex {
2628        # Get the parameters.
2629        my ($self, $objectName) = @_;
2630        # Get the relation's entity/relationship structure.
2631        my $structure = $self->_GetStructure($objectName);
2632        # Get the database handle.
2633        my $dbh = $self->{_dbh};
2634        Trace("Checking for search fields in $objectName.") if T(3);
2635        # Check for a searchable fields list.
2636        if (exists $structure->{searchFields}) {
2637            # Here we know that we need to create a full-text search index.
2638            # Get an SQL-formatted field name list.
2639            my $fields = join(", ", _FixNames(@{$structure->{searchFields}}));
2640            # Create the index. If it already exists, it will be dropped.
2641            $dbh->create_index(tbl => $objectName, idx => "search_idx",
2642                               flds => $fields, kind => 'fulltext');
2643            Trace("Index created for $fields in $objectName.") if T(2);
2644        }
2645    }
2646    
2647  =head3 DropRelation  =head3 DropRelation
2648    
2649  C<< $erdb->DropRelation($relationName); >>  C<< $erdb->DropRelation($relationName); >>
# Line 3297  Line 3330 
3330      # Substitute the bulletin board codes.      # Substitute the bulletin board codes.
3331      $retVal =~ s!\[(/?[bi])\]!<$1>!g;      $retVal =~ s!\[(/?[bi])\]!<$1>!g;
3332      $retVal =~ s!\[p\]!</p><p>!g;      $retVal =~ s!\[p\]!</p><p>!g;
3333        $retVal =~ s!\[link\s+([^\]]+)\]!<a href="$1">!g;
3334        $retVal =~ s!\[/link\]!</a>!g;
3335      # Return the result.      # Return the result.
3336      return $retVal;      return $retVal;
3337  }  }
3338    
3339    =head3 BeginTran
3340    
3341    C<< $erdb->BeginTran(); >>
3342    
3343    Start a database transaction.
3344    
3345    =cut
3346    
3347    sub BeginTran {
3348        my ($self) = @_;
3349        $self->{_dbh}->begin_tran();
3350    
3351    }
3352    
3353    =head3 CommitTran
3354    
3355    C<< $erdb->CommitTran(); >>
3356    
3357    Commit an active database transaction.
3358    
3359    =cut
3360    
3361    sub CommitTran {
3362        my ($self) = @_;
3363        $self->{_dbh}->commit_tran();
3364    }
3365    
3366    =head3 RollbackTran
3367    
3368    C<< $erdb->RollbackTran(); >>
3369    
3370    Roll back an active database transaction.
3371    
3372    =cut
3373    
3374    sub RollbackTran {
3375        my ($self) = @_;
3376        $self->{_dbh}->roll_tran();
3377    }
3378    
3379    
3380  =head2 Data Mining Methods  =head2 Data Mining Methods
3381    
# Line 4068  Line 4143 
4143    
4144  =head3 _LoadMetaData  =head3 _LoadMetaData
4145    
4146    C<< my $metadata = ERDB::_LoadMetaData($filename); >>
4147    
4148  This method loads the data describing this database from an XML file into a metadata structure.  This method loads the data describing this database from an XML file into a metadata structure.
4149  The resulting structure is a set of nested hash tables containing all the information needed to  The resulting structure is a set of nested hash tables containing all the information needed to
4150  load or use the database. The schema for the XML file is F<ERDatabase.xml>.  load or use the database. The schema for the XML file is F<ERDatabase.xml>.
# Line 4217  Line 4294 
4294              if ($found == 0) {              if ($found == 0) {
4295                  push @{$indexList}, { IndexFields => [ {name => 'id', order => 'ascending'} ] };                  push @{$indexList}, { IndexFields => [ {name => 'id', order => 'ascending'} ] };
4296              }              }
4297              # Now we need to convert the relation's index list to an index table. We begin by creating              # Attach all the indexes to the relation.
4298              # an empty table in the relation structure.              _ProcessIndexes($indexList, $relation);
             $relation->{Indexes} = { };  
             # Loop through the indexes.  
             my $count = 0;  
             for my $index (@{$indexList}) {  
                 # Add this index to the index table.  
                 _AddIndex("idx$count", $relation, $index);  
                 # Increment the counter so that the next index has a different name.  
                 $count++;  
             }  
4299          }          }
4300          # Finally, we add the relation structure to the entity.          # Finally, we add the relation structure to the entity.
4301          $entityStructure->{Relations} = $relationTable;          $entityStructure->{Relations} = $relationTable;
# Line 4241  Line 4309 
4309          _FixupFields($relationshipStructure, $relationshipName, 2, 3);          _FixupFields($relationshipStructure, $relationshipName, 2, 3);
4310          # Format a description for the FROM field.          # Format a description for the FROM field.
4311          my $fromEntity = $relationshipStructure->{from};          my $fromEntity = $relationshipStructure->{from};
4312          my $fromComment = "<b>id</b> of the source <b><a href=\"#$fromEntity\">$fromEntity</a></b>.";          my $fromComment = "[b]id[/b] of the source [b][link #$fromEntity]$fromEntity\[/link][/b].";
4313          # Get the FROM entity's key type.          # Get the FROM entity's key type.
4314          my $fromType = $entityList->{$fromEntity}->{keyType};          my $fromType = $entityList->{$fromEntity}->{keyType};
4315          # Add the FROM field.          # Add the FROM field.
# Line 4251  Line 4319 
4319                                                      PrettySort => 1});                                                      PrettySort => 1});
4320          # Format a description for the TO field.          # Format a description for the TO field.
4321          my $toEntity = $relationshipStructure->{to};          my $toEntity = $relationshipStructure->{to};
4322          my $toComment = "<b>id</b> of the target <b><a href=\"#$toEntity\">$toEntity</a></b>.";          my $toComment = "[b]id[/b] of the target [b][link #$toEntity]$toEntity\[/link][/b].";
4323          # Get the TO entity's key type.          # Get the TO entity's key type.
4324          my $toType = $entityList->{$toEntity}->{keyType};          my $toType = $entityList->{$toEntity}->{keyType};
4325          # Add the TO field.          # Add the TO field.
# Line 4263  Line 4331 
4331          my $thisRelation = { Fields => _ReOrderRelationTable($relationshipStructure->{Fields}),          my $thisRelation = { Fields => _ReOrderRelationTable($relationshipStructure->{Fields}),
4332                               Indexes => { } };                               Indexes => { } };
4333          $relationshipStructure->{Relations} = { $relationshipName => $thisRelation };          $relationshipStructure->{Relations} = { $relationshipName => $thisRelation };
4334    
4335            # Add the alternate indexes (if any). This MUST be done before the FROM and
4336            # TO indexes, because it erases the relation's index list.
4337            if (exists $relationshipStructure->{Indexes}) {
4338                _ProcessIndexes($relationshipStructure->{Indexes}, $thisRelation);
4339            }
4340            # Add the relation to the master table.
4341          # Create the FROM and TO indexes.          # Create the FROM and TO indexes.
4342          _CreateRelationshipIndex("From", $relationshipName, $relationshipStructure);          _CreateRelationshipIndex("From", $relationshipName, $relationshipStructure);
4343          _CreateRelationshipIndex("To", $relationshipName, $relationshipStructure);          _CreateRelationshipIndex("To", $relationshipName, $relationshipStructure);
         # Add the relation to the master table.  
4344          $masterRelationTable{$relationshipName} = $thisRelation;          $masterRelationTable{$relationshipName} = $thisRelation;
4345      }      }
4346      # Now store the master relation table in the metadata structure.      # Now store the master relation table in the metadata structure.
# Line 4425  Line 4499 
4499      _AddIndex("idx$indexKey", $relationStructure, $newIndex);      _AddIndex("idx$indexKey", $relationStructure, $newIndex);
4500  }  }
4501    
4502    =head3 _ProcessIndexes
4503    
4504    C<< ERDB::_ProcessIndexes($indexList, $relation); >>
4505    
4506    Build the data structures for the specified indexes in the specified relation.
4507    
4508    =over 4
4509    
4510    =item indexList
4511    
4512    Reference to a list of indexes. Each index is a hash reference containing an optional
4513    C<Notes> value that describes the index and an C<IndexFields> value that is a reference
4514    to a list of index field structures. An index field structure, in turn, is a reference
4515    to a hash that contains a C<name> attribute for the field name and an C<order>
4516    attribute that specifies either C<ascending> or C<descending>. In this sense the
4517    index list encapsulates the XML C<Indexes> structure in the database definition.
4518    
4519    =item relation
4520    
4521    The structure that describes the current relation. The new index descriptors will
4522    be stored in the structure's C<Indexes> member. Any previous data in the structure
4523    will be lost.
4524    
4525    =back
4526    
4527    =cut
4528    
4529    sub _ProcessIndexes {
4530        # Get the parameters.
4531        my ($indexList, $relation) = @_;
4532        # Now we need to convert the relation's index list to an index table. We begin by creating
4533        # an empty table in the relation structure.
4534        $relation->{Indexes} = { };
4535        # Loop through the indexes.
4536        my $count = 0;
4537        for my $index (@{$indexList}) {
4538            # Add this index to the index table.
4539            _AddIndex("idx$count", $relation, $index);
4540            # Increment the counter so that the next index has a different name.
4541            $count++;
4542        }
4543    }
4544    
4545  =head3 _AddIndex  =head3 _AddIndex
4546    
4547  Add an index to a relation structure.  Add an index to a relation structure.
# Line 4954  Line 5071 
5071      # Compute the number of columns.      # Compute the number of columns.
5072      my $colCount = @colNames;      my $colCount = @colNames;
5073      # Generate the title row.      # Generate the title row.
5074      my $htmlString = "<p><table border=\"2\"><tr><td colspan=\"$colCount\" align=\"center\">$tablename</td></tr>\n";      my $htmlString = "<table border=\"2\"><tr><td colspan=\"$colCount\" align=\"center\">$tablename</td></tr>\n";
5075      # Loop through the columns, adding the column header rows.      # Loop through the columns, adding the column header rows.
5076      $htmlString .= "<tr>";      $htmlString .= "<tr>";
5077      for my $colName (@colNames) {      for my $colName (@colNames) {
# Line 4973  Line 5090 
5090  =cut  =cut
5091    
5092  sub _CloseTable {  sub _CloseTable {
5093      return "</table></p>\n";      return "</table>\n";
5094  }  }
5095    
5096  =head3 _ShowField  =head3 _ShowField

Legend:
Removed from v.1.81  
changed lines
  Added in v.1.87

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3