EdgeExpressDB

 view release on metacpan or  search on metacpan

lib/EEDB/Edge.pm  view on Meta::CPAN

  my $source = shift; #EdgeSource object uses database of source for fetching
  my @mdata_array = @_; #Metadata object(s)
  
  if(defined($source) && !($source->isa('EEDB::EdgeSource'))) {
    die('second parameter [source] must be a EEDB::EdgeSource');
  }
  
  if(!defined($source->database) or !defined($source->primary_id)) { return []; }
  
  my @mdata_ids;
  foreach my $mdata (@mdata_array) {
    unless(defined($mdata) && ($mdata->class eq 'Metadata')) {
      die("$mdata is not a EEDB::Metadata");
    }
    if(defined($mdata->primary_id)) {push @mdata_ids, $mdata->id; }
  }
  if(scalar(@mdata_ids) == 0) { return []; } #if mdata objects not stored then return []
    
  my $sql = sprintf("SELECT e.* FROM edge e JOIN edge_2_metadata using(edge_id) WHERE metadata_id in(%s) ",
                   join(',', @mdata_ids));
  $sql .= sprintf(" AND edge_source_id=%d", $source->id);
  $sql .= sprintf(" GROUP BY e.edge_id");

  #print($sql, "\n", );
  return $class->fetch_multiple($source->database, $sql);
}

sub fetch_all_with_symbol {
  my $class = shift;
  my $db = shift;
  my $source = shift; #EdgeSource object
  my $symbol = shift; #Symbol object
  my $response_limit = shift; #optional
  
  unless(defined($source) && $source->isa('EEDB::EdgeSource')) {
    die('second parameter [source] must be a EEDB::EdgeSource');
  }
  unless(defined($symbol) && ($symbol->class eq 'Symbol')) {
    die('third parameter [symbol] must be a EEDB::Symbol');
  }

  my $sql = sprintf("SELECT e.* FROM edge f ".
                    "JOIN edge_2_symbol using(edge_id)  ".
                    "WHERE symbol_id = %d ", 
                    $symbol->id);
  if(defined($source)) {
    $sql .= sprintf("AND edge_source_id=%d", $source->id);
  }
  $sql .= sprintf(" GROUP BY e.edge_id"); #to make sure the edge is not sent more than once
  if($response_limit) {
    $sql .= sprintf(" LIMIT %d", $response_limit);
  }

  #print($sql, "\n", );
  return $class->fetch_multiple($db, $sql);
}


###############################################################################################
#
# streaming API section
#
###############################################################################################


=head2 stream_all

  Description: stream all edges out of database with a given set of source filters
  Arg (1)    : $database (MQdb::Database)
  Arg (2...) : hash named filter parameters. 
                 sources=>[$fsrc1, $fsrc2,$fsrc3],  instances of EEDB::FeatureSource
  Returntype : a DBStream instance
  Exceptions : none 

=cut

sub stream_all {
  my $class = shift;
  my $db = shift;  #database
  my %options = @_;  #like sources=>[$esrc1, $esrc2,$esrc3]
    
  return [] unless($db);

  my $sql = "SELECT * FROM edge e ";
  if(%options and $options{'visible'} eq 'y') { 
    $sql .= "JOIN edge_source es on(e.edge_source_id = es.edge_source_id and es.is_visible='y') ";
  }
  $sql .= "WHERE 1=1 ";

  if(%options and $options{'sources'}) {
    my @esrc_ids;
    foreach my $source (@{$options{'sources'}}) { 
      if($source->class eq 'EdgeSource') { push @esrc_ids, $source->id; }
    }
    $sql .= sprintf("AND e.edge_source_id in(%s) ", join(',', @esrc_ids)) if(@esrc_ids);
  }  
  if(%options and $options{'feature1'} and ($options{'feature1'}->class eq "Feature")) {
    $sql .= sprintf("AND feature1_id=%s ", $options{'feature1'}->id);
  }
  if(%options and $options{'feature2'} and ($options{'feature2'}->class eq "Feature")) {
    $sql .= sprintf("AND feature2_id=%s ", $options{'feature2'}->id);
  }

  $sql .= "ORDER by e.edge_source_id, weight desc";
  if(%options and $options{'end'} eq 'f2') { $sql .= ",feature2_id, feature1_id "; }
  else { $sql .= ",feature1_id, feature2_id "; }

  #printf("<sql>%s</sql>\n", $sql);
  return $class->stream_multiple($db, $sql);
}


sub stream_all_with_feature { #used by EEDB::Tools::EdgeCompare (experimental approach)
  my $class = shift;
  my $db = shift;
  my $feature = shift;
  my %options = @_; #optional
  
  my $id = $feature->id;
  my $sql = "SELECT * FROM edge e ";
  if(%options and $options{'visible'} eq 'y') { 



( run in 0.566 second using v1.01-cache-2.11-cpan-140bd7fdf52 )