[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.66, Thu Jul 13 04:53:54 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 1431  Line 1436 
1436  sub GetCount {  sub GetCount {
1437      # Get the parameters.      # Get the parameters.
1438      my ($self, $objectNames, $filter, $params) = @_;      my ($self, $objectNames, $filter, $params) = @_;
1439        # Insure the params argument is an array reference if the caller left it off.
1440        if (! defined($params)) {
1441            $params = [];
1442        }
1443      # Declare the return variable.      # Declare the return variable.
1444      my $retVal;      my $retVal;
1445      # Find out if we're counting an entity or a relationship.      # Find out if we're counting an entity or a relationship.
# Line 1544  Line 1553 
1553      }      }
1554  }  }
1555    
1556    =head3 InsertValue
1557    
1558    C<< $erdb->InsertValue($entityID, $fieldName, $value); >>
1559    
1560    This method will insert a new value into the database. The value must be one
1561    associated with a secondary relation, since primary values cannot be inserted:
1562    they occur exactly once. Secondary values, on the other hand, can be missing
1563    or multiply-occurring.
1564    
1565    =over 4
1566    
1567    =item entityID
1568    
1569    ID of the object that is to receive the new value.
1570    
1571    =item fieldName
1572    
1573    Field name for the new value-- this includes the entity name, since
1574    field names are of the format I<objectName>C<(>I<fieldName>C<)>.
1575    
1576    =item value
1577    
1578    New value to be put in the field.
1579    
1580    =back
1581    
1582    =cut
1583    
1584    sub InsertValue {
1585        # Get the parameters.
1586        my ($self, $entityID, $fieldName, $value) = @_;
1587        # Parse the entity name and the real field name.
1588        if ($fieldName =~ /^([^(]+)\(([^)]+)\)/) {
1589            my $entityName = $1;
1590            my $fieldTitle = $2;
1591            # Get its descriptor.
1592            if (!$self->IsEntity($entityName)) {
1593                Confess("$entityName is not a valid entity.");
1594            } else {
1595                my $entityData = $self->{_metaData}->{Entities}->{$entityName};
1596                # Find the relation containing this field.
1597                my $fieldHash = $entityData->{Fields};
1598                if (! exists $fieldHash->{$fieldTitle}) {
1599                    Confess("$fieldTitle not found in $entityName.");
1600                } else {
1601                    my $relation = $fieldHash->{$fieldTitle}->{relation};
1602                    if ($relation eq $entityName) {
1603                        Confess("Cannot do InsertValue on primary field $fieldTitle of $entityName.");
1604                    } else {
1605                        # Now we can create an INSERT statement.
1606                        my $dbh = $self->{_dbh};
1607                        my $fixedName = _FixName($fieldTitle);
1608                        my $statement = "INSERT INTO $relation (id, $fixedName) VALUES(?, ?)";
1609                        # Execute the command.
1610                        $dbh->SQL($statement, 0, $entityID, $value);
1611                    }
1612                }
1613            }
1614        } else {
1615            Confess("$fieldName is not a valid field name.");
1616        }
1617    }
1618    
1619  =head3 InsertObject  =head3 InsertObject
1620    
1621  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>  C<< my $ok = $erdb->InsertObject($objectType, \%fieldHash); >>
# Line 1560  Line 1632 
1632  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
1633  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>.
1634    
1635  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'}); >>
1636    
1637  =over 4  =over 4
1638    
# Line 1770  Line 1842 
1842          }          }
1843      }      }
1844      # Analyze the table to improve performance.      # Analyze the table to improve performance.
1845        Trace("Analyzing and compacting $relationName.") if T(3);
1846      $dbh->vacuum_it($relationName);      $dbh->vacuum_it($relationName);
1847        Trace("$relationName load completed.") if T(3);
1848      # Return the statistics.      # Return the statistics.
1849      return $retVal;      return $retVal;
1850  }  }
# Line 2001  Line 2075 
2075          push @retVal, \@rowData;          push @retVal, \@rowData;
2076          $fetched++;          $fetched++;
2077      }      }
2078        Trace("$fetched rows returned in GetAll.") if T(SQL => 4);
2079      # Return the resulting list.      # Return the resulting list.
2080      return @retVal;      return @retVal;
2081  }  }
2082    
2083    =head3 Exists
2084    
2085    C<< my $found = $sprout->Exists($entityName, $entityID); >>
2086    
2087    Return TRUE if an entity exists, else FALSE.
2088    
2089    =over 4
2090    
2091    =item entityName
2092    
2093    Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2094    
2095    =item entityID
2096    
2097    ID of the entity instance whose existence is to be checked.
2098    
2099    =item RETURN
2100    
2101    Returns TRUE if the entity instance exists, else FALSE.
2102    
2103    =back
2104    
2105    =cut
2106    #: Return Type $;
2107    sub Exists {
2108        # Get the parameters.
2109        my ($self, $entityName, $entityID) = @_;
2110        # Check for the entity instance.
2111        Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2112        my $testInstance = $self->GetEntity($entityName, $entityID);
2113        # Return an existence indicator.
2114        my $retVal = ($testInstance ? 1 : 0);
2115        return $retVal;
2116    }
2117    
2118  =head3 EstimateRowSize  =head3 EstimateRowSize
2119    
2120  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>  C<< my $rowSize = $erdb->EstimateRowSize($relName); >>
# Line 2072  Line 2182 
2182      return $objectData->{Fields};      return $objectData->{Fields};
2183  }  }
2184    
2185    =head2 Data Mining Methods
2186    
2187  =head3 GetUsefulCrossValues  =head3 GetUsefulCrossValues
2188    
2189  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>  C<< my @attrNames = $sprout->GetUsefulCrossValues($sourceEntity, $relationship); >>
# Line 2133  Line 2245 
2245      return @retVal;      return @retVal;
2246  }  }
2247    
2248    =head3 FindColumn
2249    
2250    C<< my $colIndex = ERDB::FindColumn($headerLine, $columnIdentifier); >>
2251    
2252    Return the location a desired column in a data mining header line. The data
2253    mining header line is a tab-separated list of column names. The column
2254    identifier is either the numerical index of a column or the actual column
2255    name.
2256    
2257    =over 4
2258    
2259    =item headerLine
2260    
2261    The header line from a data mining command, which consists of a tab-separated
2262    list of column names.
2263    
2264    =item columnIdentifier
2265    
2266    Either the ordinal number of the desired column (1-based), or the name of the
2267    desired column.
2268    
2269    =item RETURN
2270    
2271    Returns the array index (0-based) of the desired column.
2272    
2273    =back
2274    
2275    =cut
2276    
2277    sub FindColumn {
2278        # Get the parameters.
2279        my ($headerLine, $columnIdentifier) = @_;
2280        # Declare the return variable.
2281        my $retVal;
2282        # Split the header line into column names.
2283        my @headers = ParseColumns($headerLine);
2284        # Determine whether we have a number or a name.
2285        if ($columnIdentifier =~ /^\d+$/) {
2286            # Here we have a number. Subtract 1 and validate the result.
2287            $retVal = $columnIdentifier - 1;
2288            if ($retVal < 0 || $retVal > $#headers) {
2289                Confess("Invalid column identifer \"$columnIdentifier\": value out of range.");
2290            }
2291        } else {
2292            # Here we have a name. We need to find it in the list.
2293            for (my $i = 0; $i <= $#headers && ! defined($retVal); $i++) {
2294                if ($headers[$i] eq $columnIdentifier) {
2295                    $retVal = $i;
2296                }
2297            }
2298            if (! defined($retVal)) {
2299                Confess("Invalid column identifier \"$columnIdentifier\": value not found.");
2300            }
2301        }
2302        # Return the result.
2303        return $retVal;
2304    }
2305    
2306    =head3 ParseColumns
2307    
2308    C<< my @columns = ERDB::ParseColumns($line); >>
2309    
2310    Convert the specified data line to a list of columns.
2311    
2312    =over 4
2313    
2314    =item line
2315    
2316    A data mining input, consisting of a tab-separated list of columns terminated by a
2317    new-line.
2318    
2319    =item RETURN
2320    
2321    Returns a list consisting of the column values.
2322    
2323    =back
2324    
2325    =cut
2326    
2327    sub ParseColumns {
2328        # Get the parameters.
2329        my ($line) = @_;
2330        # Chop off the line-end.
2331        chomp $line;
2332        # Split it into a list.
2333        my @retVal = split(/\t/, $line);
2334        # Return the result.
2335        return @retVal;
2336    }
2337    
2338  =head2 Internal Utility Methods  =head2 Internal Utility Methods
2339    
2340  =head3 SetupSQL  =head3 SetupSQL
# Line 3042  Line 3244 
3244    
3245  =head3 SortNeeded  =head3 SortNeeded
3246    
3247  C<< my $flag = $erdb->SortNeeded($relationName); >>  C<< my $parms = $erdb->SortNeeded($relationName); >>
3248    
3249  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
3250  else FALSE.  relation when creating the load file.
3251    
3252    For example, if the load file should be sorted ascending by the first
3253    field with duplicates removed, this method would return
3254    
3255        sort -k 1 -u -t "\t"
3256    
3257    If the first field is numeric and duplicates are okay, the method would
3258    return
3259    
3260        sort -k 1n -t "\t"
3261    
3262  =over 4  =over 4
3263    
# Line 3053  Line 3265 
3265    
3266  Name of the relation to be examined.  Name of the relation to be examined.
3267    
3268  =item RETURN  =item
3269    
3270  Returns TRUE if the relation needs a sort, else FALSE.  Returns the sort command to use for sorting the relation, suitable for piping.
3271    
3272  =back  =back
3273    
# Line 3064  Line 3276 
3276  sub SortNeeded {  sub SortNeeded {
3277      # Get the parameters.      # Get the parameters.
3278      my ($self, $relationName) = @_;      my ($self, $relationName) = @_;
3279      # Declare the return variable.      # Declare a descriptor to hold the names of the key fields.
3280      my $retVal = 0;      my @keyNames = ();
3281      # Find out if the relation is a primary entity relation.      # Declare a flag for indicating uniqueness.
3282        my $unique;
3283        # Get the relation structure.
3284        my $relationData = $self->_FindRelation($relationName);
3285        # Find out if the relation is a primary entity relation,
3286        # a relationship relation, or a secondary entity relation.
3287      my $entityTable = $self->{_metaData}->{Entities};      my $entityTable = $self->{_metaData}->{Entities};
3288        my $relationshipTable = $self->{_metaData}->{Relationships};
3289      if (exists $entityTable->{$relationName}) {      if (exists $entityTable->{$relationName}) {
3290          my $keyType = $entityTable->{$relationName}->{keyType};          # Here we have a primary entity relation, so we have a unique sort on the
3291          Trace("Relation $relationName found in entity table with key type $keyType.") if T(3);          # ID field.
3292          # If the key is not a hash string, we must do the sort.          $unique = "-u ";
3293          if ($keyType ne 'hash-string') {          push @keyNames, "id";
3294              $retVal = 1;      } elsif (exists $relationshipTable->{$relationName}) {
3295            # Here we have a relationship. We sort using the FROM index.
3296            $unique = "";
3297            my $relationshipData = $relationshipTable->{$relationName};
3298            my $index = $relationData->{Indexes}->{"idx${relationName}From"};
3299            push @keyNames, @{$index->{IndexFields}};
3300        } else {
3301            # Here we have a secondary entity relation, so we have a non-unique sort on
3302            # the ID field.
3303            $unique = "";
3304            push @keyNames, "id";
3305        }
3306        # Now we parse the key names into sort parameters. First, we prime the return
3307        # string.
3308        my $retVal = "sort -t \"\t\" $unique";
3309        # Get the relation's field list.
3310        my @fields = @{$relationData->{Fields}};
3311        # Loop through the keys.
3312        for my $keyData (@keyNames) {
3313            # Get the key and the ordering.
3314            my ($keyName, $ordering);
3315            if ($keyData =~ /^([^ ]+) DESC/) {
3316                ($keyName, $ordering) = ($1, "descending");
3317            } else {
3318                ($keyName, $ordering) = ($keyData, "ascending");
3319            }
3320            # Find the key's position and type.
3321            my $fieldSpec;
3322            for (my $i = 0; $i <= $#fields && ! $fieldSpec; $i++) {
3323                my $thisField = $fields[$i];
3324                if ($thisField->{name} eq $keyName) {
3325                    # Get the sort modifier for this field type. The modifier
3326                    # decides whether we're using a character, numeric, or
3327                    # floating-point sort.
3328                    my $modifier = $TypeTable{$thisField->{type}}->{sort};
3329                    # If the index is descending for this field, denote we want
3330                    # to reverse the sort order on this field.
3331                    if ($ordering eq 'descending') {
3332                        $modifier .= "r";
3333                    }
3334                    # Store the position and modifier into the field spec, which
3335                    # will stop the inner loop. Note that the field number is
3336                    # 1-based in the sort command, so we have to increment the
3337                    # index.
3338                    $fieldSpec = ($i + 1) . $modifier;
3339                }
3340          }          }
3341            # Add this field to the sort command.
3342            $retVal .= " -k $fieldSpec";
3343      }      }
3344      # Return the result.      # Return the result.
3345      return $retVal;      return $retVal;

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3