[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.47, Sun Jun 18 05:14:56 2006 UTC revision 1.62, Mon Jul 10 09:42:34 2006 UTC
# Line 91  Line 91 
91    
92  32-bit signed integer  32-bit signed integer
93    
94    =item counter
95    
96    32-bit unsigned integer
97    
98  =item date  =item date
99    
100  64-bit unsigned integer, representing a PERL date/time value  64-bit unsigned integer, representing a PERL date/time value
# Line 318  Line 322 
322  # "maxLen" is the maximum permissible length of the incoming string data used to populate a field  # "maxLen" is the maximum permissible length of the incoming string data used to populate a field
323  # of the specified type. "dataGen" is PERL string that will be evaluated if no test data generation  # of the specified type. "dataGen" is PERL string that will be evaluated if no test data generation
324  # string is specified in the field definition. "avgLen" is the average byte length for estimating  # string is specified in the field definition. "avgLen" is the average byte length for estimating
325  # record sizes.  # record sizes. "sort" is the key modifier for the sort command.
326  my %TypeTable = ( char =>    { sqlType => 'CHAR(1)',            maxLen => 1,            avgLen =>   1, dataGen => "StringGen('A')" },  my %TypeTable = ( char =>    { sqlType => 'CHAR(1)',            maxLen => 1,            avgLen =>   1, sort => "",  dataGen => "StringGen('A')" },
327                    int =>     { sqlType => 'INTEGER',            maxLen => 20,           avgLen =>   4, dataGen => "IntGen(0, 99999999)" },                    int =>     { sqlType => 'INTEGER',            maxLen => 20,           avgLen =>   4, sort => "n", dataGen => "IntGen(0, 99999999)" },
328                    string =>  { sqlType => 'VARCHAR(255)',       maxLen => 255,          avgLen => 100, dataGen => "StringGen(IntGen(10,250))" },                    counter => { sqlType => 'INTEGER UNSIGNED',   maxLen => 20,           avgLen =>   4, sort => "n", dataGen => "IntGen(0, 99999999)" },
329                    text =>    { sqlType => 'TEXT',               maxLen => 1000000000,   avgLen => 500, dataGen => "StringGen(IntGen(80,1000))" },                    string =>  { sqlType => 'VARCHAR(255)',       maxLen => 255,          avgLen => 100, sort => "",  dataGen => "StringGen(IntGen(10,250))" },
330                    date =>    { sqlType => 'BIGINT',             maxLen => 80,           avgLen =>   8, dataGen => "DateGen(-7, 7, IntGen(0,1400))" },                    text =>    { sqlType => 'TEXT',               maxLen => 1000000000,   avgLen => 500, sort => "",  dataGen => "StringGen(IntGen(80,1000))" },
331                    float =>   { sqlType => 'DOUBLE PRECISION',   maxLen => 40,           avgLen =>   8, dataGen => "FloatGen(0.0, 100.0)" },                    date =>    { sqlType => 'BIGINT',             maxLen => 80,           avgLen =>   8, sort => "n", dataGen => "DateGen(-7, 7, IntGen(0,1400))" },
332                    boolean => { sqlType => 'SMALLINT',           maxLen => 1,            avgLen =>   1, dataGen => "IntGen(0, 1)" },                    float =>   { sqlType => 'DOUBLE PRECISION',   maxLen => 40,           avgLen =>   8, sort => "g", dataGen => "FloatGen(0.0, 100.0)" },
333                      boolean => { sqlType => 'SMALLINT',           maxLen => 1,            avgLen =>   1, sort => "n", dataGen => "IntGen(0, 1)" },
334                   'hash-string' =>                   'hash-string' =>
335                               { sqlType => 'VARCHAR(22)',        maxLen => 22,           avgLen =>  22, dataGen => "SringGen(22)" },                               { sqlType => 'VARCHAR(22)',        maxLen => 22,           avgLen =>  22, sort => "",  dataGen => "SringGen(22)" },
336                   'id-string' =>                   'id-string' =>
337                               { sqlType => 'VARCHAR(25)',        maxLen => 25,           avgLen =>  25, dataGen => "SringGen(22)" },                               { sqlType => 'VARCHAR(25)',        maxLen => 25,           avgLen =>  25, sort => "",  dataGen => "SringGen(22)" },
338                   'key-string' =>                   'key-string' =>
339                               { sqlType => 'VARCHAR(40)',        maxLen => 40,           avgLen =>  10, dataGen => "StringGen(IntGen(10,40))" },                               { sqlType => 'VARCHAR(40)',        maxLen => 40,           avgLen =>  10, sort => "",  dataGen => "StringGen(IntGen(10,40))" },
340                   'name-string' =>                   'name-string' =>
341                               { sqlType => 'VARCHAR(80)',        maxLen => 80,           avgLen =>  40, dataGen => "StringGen(IntGen(10,80))" },                               { sqlType => 'VARCHAR(80)',        maxLen => 80,           avgLen =>  40, sort => "",  dataGen => "StringGen(IntGen(10,80))" },
342                   'medium-string' =>                   'medium-string' =>
343                               { sqlType => 'VARCHAR(160)',       maxLen => 160,          avgLen =>  40, dataGen => "StringGen(IntGen(10,160))" },                               { sqlType => 'VARCHAR(160)',       maxLen => 160,          avgLen =>  40, sort => "",  dataGen => "StringGen(IntGen(10,160))" },
344                  );                  );
345    
346  # Table translating arities into natural language.  # Table translating arities into natural language.
# Line 1544  Line 1549 
1549      }      }
1550  }  }
1551    
1552    =head3 InsertValue
1553    
1554    C<< $erdb->InsertValue($entityID, $fieldName, $value); >>
1555    
1556    This method will insert a new value into the database. The value must be one
1557    associated with a secondary relation, since primary values cannot be inserted:
1558    they occur exactly once. Secondary values, on the other hand, can be missing
1559    or multiply-occurring.
1560    
1561    =over 4
1562    
1563    =item entityID
1564    
1565    ID of the object that is to receive the new value.
1566    
1567    =item fieldName
1568    
1569    Field name for the new value-- this includes the entity name, since
1570    field names are of the format I<objectName>C<(>I<fieldName>C<)>.
1571    
1572    =item value
1573    
1574    New value to be put in the field.
1575    
1576    =back
1577    
1578    =cut
1579    
1580    sub InsertValue {
1581        # Get the parameters.
1582        my ($self, $entityID, $fieldName, $value) = @_;
1583        # Parse the entity name and the real field name.
1584        if ($fieldName =~ /^([^(]+)\(([^)]+)\)/) {
1585            my $entityName = $1;
1586            my $fieldTitle = $2;
1587            # Get its descriptor.
1588            if (!$self->IsEntity($entityName)) {
1589                Confess("$entityName is not a valid entity.");
1590            } else {
1591                my $entityData = $self->{_metaData}->{Entities}->{$entityName};
1592                # Find the relation containing this field.
1593                my $fieldHash = $entityData->{Fields};
1594                if (! exists $fieldHash->{$fieldTitle}) {
1595                    Confess("$fieldTitle not found in $entityName.");
1596                } else {
1597                    my $relation = $fieldHash->{$fieldTitle}->{relation};
1598                    if ($relation eq $entityName) {
1599                        Confess("Cannot do InsertValue on primary field $fieldTitle of $entityName.");
1600                    } else {
1601                        # Now we can create an INSERT statement.
1602                        my $dbh = $self->{_dbh};
1603                        my $fixedName = _FixName($fieldTitle);
1604                        my $statement = "INSERT INTO $relation (id, $fixedName) VALUES(?, ?)";
1605                        # Execute the command.
1606                        $dbh->SQL($statement, 0, $entityID, $value);
1607                    }
1608                }
1609            }
1610        } else {
1611            Confess("$fieldName is not a valid field name.");
1612        }
1613    }
1614    
1615  =head3 InsertObject  =head3 InsertObject
1616    
1617  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>
# Line 1560  Line 1628 
1628  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1629  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1630    
1631  C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>  C<< $erdb->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
1632    
1633  =over 4  =over 4
1634    
# Line 1770  Line 1838 
1838          }          }
1839      }      }
1840      # Analyze the table to improve performance.      # Analyze the table to improve performance.
1841        Trace("Analyzing and compacting $relationName.") if T(3);
1842      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
1843        # Flush the database cache.
1844        $dbh->flush_tables();
1845        Trace("$relationName load completed.") if T(3);
1846      # Return the statistics.      # Return the statistics.
1847      return $retVal;      return $retVal;
1848  }  }
# Line 2001  Line 2073 
2073          push @retVal, \@rowData;          push @retVal, \@rowData;
2074          $fetched++;          $fetched++;
2075      }      }
2076        Trace("$fetched rows returned in GetAll.") if T(SQL => 4);
2077      # Return the resulting list.      # Return the resulting list.
2078      return @retVal;      return @retVal;
2079  }  }
2080    
2081    =head3 Exists
2082    
2083    C<< my $found = $sprout->Exists($entityName, $entityID); >>
2084    
2085    Return TRUE if an entity exists, else FALSE.
2086    
2087    =over 4
2088    
2089    =item entityName
2090    
2091    Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2092    
2093    =item entityID
2094    
2095    ID of the entity instance whose existence is to be checked.
2096    
2097    =item RETURN
2098    
2099    Returns TRUE if the entity instance exists, else FALSE.
2100    
2101    =back
2102    
2103    =cut
2104    #: Return Type $;
2105    sub Exists {
2106        # Get the parameters.
2107        my ($self, $entityName, $entityID) = @_;
2108        # Check for the entity instance.
2109        Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2110        my $testInstance = $self->GetEntity($entityName, $entityID);
2111        # Return an existence indicator.
2112        my $retVal = ($testInstance ? 1 : 0);
2113        return $retVal;
2114    }
2115    
2116  =head3 EstimateRowSize  =head3 EstimateRowSize
2117    
2118  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>
# Line 2072  Line 2180 
2180      return $objectData->{Fields};      return $objectData->{Fields};
2181  }  }
2182    
2183    =head2 Data Mining Methods
2184    
2185  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2186    
2187  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2133  Line 2243 
2243      return @retVal;      return @retVal;
2244  }  }
2245    
2246    =head3 FindColumn
2247    
2248    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2249    
2250    Return the location a desired column in a data mining header line. The data
2251    mining header line is a tab-separated list of column names. The column
2252    identifier is either the numerical index of a column or the actual column
2253    name.
2254    
2255    =over 4
2256    
2257    =item headerLine
2258    
2259    The header line from a data mining command, which consists of a tab-separated
2260    list of column names.
2261    
2262    =item columnIdentifier
2263    
2264    Either the ordinal number of the desired column (1-based), or the name of the
2265    desired column.
2266    
2267    =item RETURN
2268    
2269    Returns the array index (0-based) of the desired column.
2270    
2271    =back
2272    
2273    =cut
2274    
2275    sub FindColumn {
2276        # Get the parameters.
2277        my ($headerLine, $columnIdentifier) = @_;
2278        # Declare the return variable.
2279        my $retVal;
2280        # Split the header line into column names.
2281        my @headers = ParseColumns($headerLine);
2282        # Determine whether we have a number or a name.
2283        if ($columnIdentifier =~ /^\d+$/) {
2284            # Here we have a number. Subtract 1 and validate the result.
2285            $retVal = $columnIdentifier - 1;
2286            if ($retVal < 0 || $retVal > $#headers) {
2287                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2288            }
2289        } else {
2290            # Here we have a name. We need to find it in the list.
2291            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2292                if ($headers[$i] eq $columnIdentifier) {
2293                    $retVal = $i;
2294                }
2295            }
2296            if (! defined($retVal)) {
2297                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2298            }
2299        }
2300        # Return the result.
2301        return $retVal;
2302    }
2303    
2304    =head3 ParseColumns
2305    
2306    C<< my @columns = ERDB::ParseColumns($line); >>
2307    
2308    Convert the specified data line to a list of columns.
2309    
2310    =over 4
2311    
2312    =item line
2313    
2314    A data mining input, consisting of a tab-separated list of columns terminated by a
2315    new-line.
2316    
2317    =item RETURN
2318    
2319    Returns a list consisting of the column values.
2320    
2321    =back
2322    
2323    =cut
2324    
2325    sub ParseColumns {
2326        # Get the parameters.
2327        my ($line) = @_;
2328        # Chop off the line-end.
2329        chomp $line;
2330        # Split it into a list.
2331        my @retVal = split(/\t/, $line);
2332        # Return the result.
2333        return @retVal;
2334    }
2335    
2336  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2337    
2338  =head3 SetupSQL  =head3 SetupSQL
# Line 3042  Line 3242 
3242    
3243  =head3 SortNeeded  =head3 SortNeeded
3244    
3245  C<< my $flag = $erdb->SortNeeded($relationName); >>  C<< my $parms = $erdb->SortNeeded($relationName); >>
3246    
3247  Return TRUE if the specified relation should be sorted during loading to remove duplicate keys,  Return the pipe command for the sort that should be applied to the specified
3248  else FALSE.  relation when creating the load file.
3249    
3250    For example, if the load file should be sorted ascending by the first
3251    field with duplicates removed, this method would return
3252    
3253        sort -k 1 -u -t "\t"
3254    
3255    If the first field is numeric and duplicates are okay, the method would
3256    return
3257    
3258        sort -k 1n -t "\t"
3259    
3260  =over 4  =over 4
3261    
# Line 3053  Line 3263 
3263    
3264  Name of the relation to be examined.  Name of the relation to be examined.
3265    
3266  =item RETURN  =item
3267    
3268  Returns TRUE if the relation needs a sort, else FALSE.  Returns the sort command to use for sorting the relation, suitable for piping.
3269    
3270  =back  =back
3271    
# Line 3064  Line 3274 
3274  sub SortNeeded {  sub SortNeeded {
3275      # Get the parameters.      # Get the parameters.
3276      my ($self, $relationName) = @_;      my ($self, $relationName) = @_;
3277      # Declare the return variable.      # Declare a descriptor to hold the names of the key fields.
3278      my $retVal = 0;      my @keyNames = ();
3279      # Find out if the relation is a primary entity relation.      # Declare a flag for indicating uniqueness.
3280        my $unique;
3281        # Get the relation structure.
3282        my $relationData = $self->_FindRelation($relationName);
3283        # Find out if the relation is a primary entity relation,
3284        # a relationship relation, or a secondary entity relation.
3285      my $entityTable = $self->{_metaData}->{Entities};      my $entityTable = $self->{_metaData}->{Entities};
3286        my $relationshipTable = $self->{_metaData}->{Relationships};
3287      if (exists $entityTable->{$relationName}) {      if (exists $entityTable->{$relationName}) {
3288          my $keyType = $entityTable->{$relationName}->{keyType};          # Here we have a primary entity relation, so we have a unique sort on the
3289          Trace("Relation $relationName found in entity table with key type $keyType.") if T(3);          # ID field.
3290          # If the key is not a hash string, we must do the sort.          $unique = "-u ";
3291          if ($keyType ne 'hash-string') {          push @keyNames, "id";
3292              $retVal = 1;      } elsif (exists $relationshipTable->{$relationName}) {
3293            # Here we have a relationship. We sort using the FROM index.
3294            $unique = "";
3295            my $relationshipData = $relationshipTable->{$relationName};
3296            my $index = $relationData->{Indexes}->{"idx${relationName}From"};
3297            push @keyNames, @{$index->{IndexFields}};
3298        } else {
3299            # Here we have a secondary entity relation, so we have a non-unique sort on
3300            # the ID field.
3301            $unique = "";
3302            push @keyNames, "id";
3303        }
3304        # Now we parse the key names into sort parameters. First, we prime the return
3305        # string.
3306        my $retVal = "sort -t \"\t\" $unique";
3307        # Get the relation's field list.
3308        my @fields = @{$relationData->{Fields}};
3309        # Loop through the keys.
3310        for my $keyData (@keyNames) {
3311            # Get the key and the ordering.
3312            my ($keyName, $ordering);
3313            if ($keyData =~ /^([^ ]+) DESC/) {
3314                ($keyName, $ordering) = ($1, "descending");
3315            } else {
3316                ($keyName, $ordering) = ($keyData, "ascending");
3317            }
3318            # Find the key's position and type.
3319            my $fieldSpec;
3320            for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) {
3321                my $thisField = $fields[$i];
3322                if ($thisField->{name} eq $keyName) {
3323                    # Get the sort modifier for this field type. The modifier
3324                    # decides whether we're using a character, numeric, or
3325                    # floating-point sort.
3326                    my $modifier = $TypeTable{$thisField->{type}}->{sort};
3327                    # If the index is descending for this field, denote we want
3328                    # to reverse the sort order on this field.
3329                    if ($ordering eq 'descending') {
3330                        $modifier .= "r";
3331                    }
3332                    # Store the position and modifier into the field spec, which
3333                    # will stop the inner loop. Note that the field number is
3334                    # 1-based in the sort command, so we have to increment the
3335                    # index.
3336                    $fieldSpec = ($i + 1) . $modifier;
3337                }
3338          }          }
3339            # Add this field to the sort command.
3340            $retVal .= " -k $fieldSpec";
3341      }      }
3342      # Return the result.      # Return the result.
3343      return $retVal;      return $retVal;

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.62

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3