Commit a695d9b3 authored by Leo Gordon's avatar Leo Gordon
Browse files

Moved the caching from Hive.pm into the specific storable classes themselves...

Moved the caching from Hive.pm into the specific storable classes themselves (via Cacheable.pm) to better conform to Active Record pattern
parent 13f462ad
......@@ -37,10 +37,11 @@ package Bio::EnsEMBL::Hive::Analysis;
use strict;
use Bio::EnsEMBL::Hive;
use Bio::EnsEMBL::Hive::Utils ('stringify');
use Bio::EnsEMBL::Hive::AnalysisCtrlRule;
use Bio::EnsEMBL::Hive::DataflowRule;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
=head1 AUTOLOADED
......@@ -199,7 +200,7 @@ sub display_name {
sub stats {
my $self = shift @_;
return Bio::EnsEMBL::Hive->collection('AnalysisStats')->find_one_by('analysis', $self)
return Bio::EnsEMBL::Hive::AnalysisStats->collection()->find_one_by('analysis', $self)
|| $self->adaptor->db->get_AnalysisStatsAdaptor->fetch_by_analysis_id( $self->dbID );
}
......@@ -216,7 +217,7 @@ sub jobs_collection {
sub control_rules_collection {
my $self = shift @_;
return Bio::EnsEMBL::Hive->collection('AnalysisCtrlRule')->find_all_by('ctrled_analysis', $self)
return Bio::EnsEMBL::Hive::AnalysisCtrlRule->collection()->find_all_by('ctrled_analysis', $self)
|| $self->adaptor->db->get_AnalysisCtrlRuleAdaptor->fetch_all_by_ctrled_analysis_id( $self->dbID );
}
......@@ -226,7 +227,7 @@ sub dataflow_rules_collection {
$self->{'_dataflow_rules_collection'} = shift if(@_);
return $self->{'_dataflow_rules_collection'} || Bio::EnsEMBL::Hive->collection('DataflowRule')->find_all_by('from_analysis', $self);
return $self->{'_dataflow_rules_collection'} || Bio::EnsEMBL::Hive::DataflowRule->collection()->find_all_by('from_analysis', $self);
}
......
......@@ -46,7 +46,7 @@ use strict;
use Bio::EnsEMBL::Hive::Utils ('throw');
use Bio::EnsEMBL::Hive::URLFactory;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
=head1 AUTOLOADED
......@@ -118,7 +118,7 @@ sub condition_analysis {
# lazy load the analysis object if I can
if( !$self->{'_condition_analysis'} and my $condition_analysis_url = $self->condition_analysis_url ) {
if( $self->{'_condition_analysis'} = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $condition_analysis_url) ) {
if( $self->{'_condition_analysis'} = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $condition_analysis_url) ) {
# warn "Lazy-loading object from 'Analysis' collection\n";
} elsif(my $adaptor = $self->adaptor) {
# warn "Lazy-loading object from AnalysisAdaptor\n";
......
......@@ -42,7 +42,7 @@ use POSIX;
use Bio::EnsEMBL::Hive::Utils ('throw');
use Bio::EnsEMBL::Hive::Analysis;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
## Minimum amount of time in msec that a worker should run before reporting
......
package Bio::EnsEMBL::Hive::Cacheable;
use Data::Dumper;
use Bio::EnsEMBL::Hive::Utils::Collection;
our $cache_by_class; # global Hash-of-Hashes
sub collection {
my $class = shift @_;
if(@_) {
$cache_by_class{$class} = shift @_;
}
return $cache_by_class{$class} ||= Bio::EnsEMBL::Hive::Utils::Collection->new();
}
1;
......@@ -266,7 +266,8 @@ sub load_collections {
foreach my $AdaptorType ('ResourceClass', 'ResourceDescription', 'Analysis', 'AnalysisStats', 'AnalysisCtrlRule', 'DataflowRule') {
my $adaptor = $self->get_adaptor( $AdaptorType );
Bio::EnsEMBL::Hive->collection( $AdaptorType, Bio::EnsEMBL::Hive::Utils::Collection->new( $adaptor->fetch_all ) );
my $class = 'Bio::EnsEMBL::Hive::'.$AdaptorType;
$class->collection( Bio::EnsEMBL::Hive::Utils::Collection->new( $adaptor->fetch_all ) );
}
}
......@@ -284,14 +285,15 @@ sub save_collections {
foreach my $AdaptorType ('ResourceClass', 'ResourceDescription', 'Analysis', 'AnalysisStats', 'AnalysisCtrlRule', 'DataflowRule') {
my $adaptor = $self->get_adaptor( $AdaptorType );
foreach my $storable_object ( Bio::EnsEMBL::Hive->collection( $AdaptorType )->list ) {
my $class = 'Bio::EnsEMBL::Hive::'.$AdaptorType;
foreach my $storable_object ( $class->collection()->list ) {
$adaptor->store_or_update_one( $storable_object );
# warn "Stored/updated ".$storable_object->toString()."\n";
}
}
my $job_adaptor = $self->get_AnalysisJobAdaptor;
foreach my $analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
if(my $our_jobs = $analysis->jobs_collection ) {
$job_adaptor->store( $our_jobs );
foreach my $job (@$our_jobs) {
......
......@@ -56,7 +56,7 @@ use strict;
use Bio::EnsEMBL::Hive::Utils ('stringify', 'throw');
use Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
=head1 AUTOLOADED
......@@ -161,7 +161,7 @@ sub to_analysis {
# lazy load the analysis object if I can
if( !$self->{'_to_analysis'} and my $to_analysis_url = $self->to_analysis_url ) {
if( $self->{'_to_analysis'} = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $to_analysis_url) ) {
if( $self->{'_to_analysis'} = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $to_analysis_url) ) {
# warn "Lazy-loading object from 'Analysis' collection\n";
} elsif(my $adaptor = $self->adaptor) {
# warn "Lazy-loading object from AnalysisAdaptor\n";
......
......@@ -463,14 +463,14 @@ sub add_objects_from_config {
my $resource_class;
if( $resource_class = Bio::EnsEMBL::Hive->collection('ResourceClass')->find_one_by('name', $rc_name) ) {
if( $resource_class = Bio::EnsEMBL::Hive::ResourceClass->collection()->find_one_by('name', $rc_name) ) {
warn "Found an already existing resource_class '$rc_name'.\n";
} else {
warn "Creating a new resource_class '$rc_name'.\n";
$resource_class = Bio::EnsEMBL::Hive::ResourceClass->new(
'name' => $rc_name,
);
Bio::EnsEMBL::Hive->collection('ResourceClass')->add( $resource_class );
Bio::EnsEMBL::Hive::ResourceClass->collection()->add( $resource_class );
}
while( my($meadow_type, $resource_param_list) = each %{ $resource_classes_hash->{$rc_name} } ) {
......@@ -478,7 +478,7 @@ sub add_objects_from_config {
my $resource_description;
if( $resource_description = Bio::EnsEMBL::Hive->collection('ResourceDescription')->find_one_by('resource_class', $resource_class, 'meadow_type', $meadow_type) ) {
if( $resource_description = Bio::EnsEMBL::Hive::ResourceDescription->collection()->find_one_by('resource_class', $resource_class, 'meadow_type', $meadow_type) ) {
warn "Attempting to redefine an existing description for '$rc_name/$meadow_type' resource class\n";
$resource_description->submission_cmd_args( $resource_param_list->[0] );
$resource_description->worker_cmd_args( $resource_param_list->[1] );
......@@ -490,16 +490,16 @@ sub add_objects_from_config {
'submission_cmd_args' => $resource_param_list->[0],
'worker_cmd_args' => $resource_param_list->[1],
);
Bio::EnsEMBL::Hive->collection('ResourceDescription')->add( $resource_description );
Bio::EnsEMBL::Hive::ResourceDescription->collection()->add( $resource_description );
}
}
}
unless(my $default_rc = Bio::EnsEMBL::Hive->collection('ResourceClass')->find_one_by('name', 'default') ) {
unless(my $default_rc = Bio::EnsEMBL::Hive::ResourceClass->collection()->find_one_by('name', 'default') ) {
warn "\tNB:'default' resource class is not in the database (did you forget to inherit from SUPER::resource_classes ?) - creating it for you\n";
$default_rc = Bio::EnsEMBL::Hive::ResourceClass->new(
'name' => 'default',
);
Bio::EnsEMBL::Hive->collection('ResourceClass')->add( $default_rc );
Bio::EnsEMBL::Hive::ResourceClass->collection()->add( $default_rc );
}
warn "Done.\n\n";
......@@ -527,7 +527,7 @@ sub add_objects_from_config {
die "(-rc_id => $rc_id) syntax is deprecated, please use (-rc_name => 'your_resource_class_name')";
}
my $analysis = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $logic_name); # the analysis with this logic_name may have already been stored in the db
my $analysis = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $logic_name); # the analysis with this logic_name may have already been stored in the db
my $stats;
if( $analysis ) {
......@@ -539,7 +539,7 @@ sub add_objects_from_config {
warn "Creating analysis '$logic_name'.\n";
$rc_name ||= 'default';
my $resource_class = Bio::EnsEMBL::Hive->collection('ResourceClass')->find_one_by('name', $rc_name)
my $resource_class = Bio::EnsEMBL::Hive::ResourceClass->collection()->find_one_by('name', $rc_name)
or die "Could not find local resource with name '$rc_name', please check that resource_classes() method of your PipeConfig either contains or inherits it from the parent class";
if ($meadow_type and not exists $valley->available_meadow_hash()->{$meadow_type}) {
......@@ -581,8 +581,8 @@ sub add_objects_from_config {
'sync_lock' => 0,
);
Bio::EnsEMBL::Hive->collection('Analysis')->add( $analysis );
Bio::EnsEMBL::Hive->collection('AnalysisStats')->add( $stats );
Bio::EnsEMBL::Hive::Analysis->collection()->add( $analysis );
Bio::EnsEMBL::Hive::AnalysisStats->collection()->add( $stats );
}
# now create the corresponding jobs (if there are any):
......@@ -603,7 +603,7 @@ sub add_objects_from_config {
my ($logic_name, $wait_for, $flow_into)
= @{$aha}{qw(-logic_name -wait_for -flow_into)}; # slicing a hash reference
my $analysis = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $logic_name);
my $analysis = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $logic_name);
$wait_for ||= [];
$wait_for = [ $wait_for ] unless(ref($wait_for) eq 'ARRAY'); # force scalar into an arrayref
......@@ -611,14 +611,14 @@ sub add_objects_from_config {
# create control rules:
foreach my $condition_url (@$wait_for) {
unless ($condition_url =~ m{^\w*://}) {
my $condition_analysis = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $condition_url)
my $condition_analysis = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $condition_url)
or die "Could not find a local analysis '$condition_url' to create a control rule (in '".($analysis->logic_name)."')\n";
}
my $c_rule = Bio::EnsEMBL::Hive::AnalysisCtrlRule->new(
'condition_analysis_url' => $condition_url,
'ctrled_analysis' => $analysis,
);
Bio::EnsEMBL::Hive->collection('AnalysisCtrlRule')->add( $c_rule );
Bio::EnsEMBL::Hive::AnalysisCtrlRule->collection()->add( $c_rule );
}
$flow_into ||= {};
......@@ -660,7 +660,7 @@ sub add_objects_from_config {
while(my ($heir_url, $input_id_template_list) = each %$heirs) {
unless ($heir_url =~ m{^\w*://}) {
my $heir_analysis = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $heir_url)
my $heir_analysis = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $heir_url)
or die "Could not find a local analysis named '$heir_url' (dataflow from analysis '".($analysis->logic_name)."')\n";
}
......@@ -675,7 +675,7 @@ sub add_objects_from_config {
'funnel_dataflow_rule' => $funnel_dataflow_rule,
'input_id_template' => $input_id_template,
);
Bio::EnsEMBL::Hive->collection('DataflowRule')->add( $df_rule );
Bio::EnsEMBL::Hive::DataflowRule->collection()->add( $df_rule );
if($group_role eq 'funnel') {
if($group_tag_to_funnel_dataflow_rule{$group_tag}) {
......
......@@ -39,7 +39,7 @@ package Bio::EnsEMBL::Hive::ResourceClass;
use strict;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
sub name {
......
......@@ -41,7 +41,7 @@ package Bio::EnsEMBL::Hive::ResourceDescription;
use strict;
use base ( 'Bio::EnsEMBL::Hive::Storable' );
use base ( 'Bio::EnsEMBL::Hive::Cacheable', 'Bio::EnsEMBL::Hive::Storable' );
=head1 AUTOLOADED
......
......@@ -45,7 +45,6 @@ use strict;
use warnings;
use Scalar::Util qw(weaken);
use Bio::EnsEMBL::Hive;
use Bio::EnsEMBL::Hive::Utils ('throw');
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
......@@ -156,7 +155,8 @@ sub AUTOLOAD {
# attempt to lazy-load:
} elsif( !$self->{$foo_obj_method_name} and my $foo_object_id = $self->{$foo_id_method_name}) {
if( $self->{$foo_obj_method_name} = Bio::EnsEMBL::Hive->collection($AdaptorType)->find_one_by('dbID', $foo_object_id) ) { # careful: $AdaptorType may not be unique (aliases)
my $foo_class = 'Bio::EnsEMBL::Hive::'.$AdaptorType;
if( $self->{$foo_obj_method_name} = $foo_class->collection()->find_one_by('dbID', $foo_object_id) ) { # careful: $AdaptorType may not be unique (aliases)
# warn "Lazy-loading object from $AdaptorType collection\n";
} elsif(my $adaptor = $self->adaptor) {
# warn "Lazy-loading object from $AdaptorType adaptor\n";
......
......@@ -48,10 +48,13 @@ package Bio::EnsEMBL::Hive::Utils::Graph;
use strict;
use warnings;
use Bio::EnsEMBL::Hive;
use Bio::EnsEMBL::Hive::Utils::GraphViz;
use Bio::EnsEMBL::Hive::Utils::Collection;
use Bio::EnsEMBL::Hive::Utils::Config;
use Bio::EnsEMBL::Hive::Analysis;
use Bio::EnsEMBL::Hive::AnalysisStats;
use Bio::EnsEMBL::Hive::AnalysisCtrlRule;
use Bio::EnsEMBL::Hive::DataflowRule;
use base ('Bio::EnsEMBL::Hive::Configurable');
......@@ -169,15 +172,15 @@ sub build {
my $hive_dba = $self->hive_dba;
if( my $job_limit = $self->config_get('DisplayJobs') and my $job_adaptor = $hive_dba && $hive_dba->get_AnalysisJobAdaptor ) {
foreach my $analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
my @jobs = sort {$a->dbID <=> $b->dbID} @{ $job_adaptor->fetch_some_by_analysis_id_limit( $analysis->dbID, $job_limit+1 )};
$analysis->jobs_collection( \@jobs );
}
}
foreach my $df_rule ( Bio::EnsEMBL::Hive->collection('DataflowRule')->list ) {
foreach my $df_rule ( Bio::EnsEMBL::Hive::DataflowRule->collection()->list ) {
if(my $target_object = Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $df_rule->to_analysis_url )) {
if(my $target_object = Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $df_rule->to_analysis_url )) {
$df_rule->to_analysis( $target_object );
if(UNIVERSAL::isa($target_object, 'Bio::EnsEMBL::Hive::Analysis')) {
$target_object->{'_inflow_count'}++;
......@@ -185,7 +188,7 @@ sub build {
} elsif( $df_rule->to_analysis->isa('Bio::EnsEMBL::Hive::Analysis') ) { # dataflow target is a foreign Analysis
$target_object = $df_rule->to_analysis();
$target_object->{'_foreign'}=1;
Bio::EnsEMBL::Hive->collection('Analysis')->add( $target_object ); # add it to the collection
Bio::EnsEMBL::Hive::Analysis->collection()->add( $target_object ); # add it to the collection
}
if( my $funnel_dataflow_rule = $df_rule->funnel_dataflow_rule ) {
......@@ -193,17 +196,17 @@ sub build {
}
}
foreach my $c_rule ( Bio::EnsEMBL::Hive->collection('AnalysisCtrlRule')->list ) { # control rule's condition is a foreign Analysis
unless( Bio::EnsEMBL::Hive->collection('Analysis')->find_one_by('logic_name', $c_rule->condition_analysis_url )) {
foreach my $c_rule ( Bio::EnsEMBL::Hive::AnalysisCtrlRule->collection()->list ) { # control rule's condition is a foreign Analysis
unless( Bio::EnsEMBL::Hive::Analysis->collection()->find_one_by('logic_name', $c_rule->condition_analysis_url )) {
my $condition_analysis = $c_rule->condition_analysis();
$condition_analysis->{'_foreign'}=1;
Bio::EnsEMBL::Hive->collection('Analysis')->add( $condition_analysis ); # add it to the collection
Bio::EnsEMBL::Hive::Analysis->collection()->add( $condition_analysis ); # add it to the collection
}
}
# NB: this is a very approximate algorithm with rough edges!
# It will not find all start nodes in cyclic components!
foreach my $source_analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $source_analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
unless( $source_analysis->{'_inflow_count'} or $source_analysis->{'_foreign'} ) { # if there is no dataflow into this analysis
# run the recursion in each component that has a non-cyclic start:
$self->_propagate_allocation( $source_analysis );
......@@ -214,16 +217,16 @@ sub build {
my $pipeline_label = sprintf('%s@%s', $dbc->dbname, $dbc->host || '-');
$self->_add_pipeline_label( $pipeline_label );
}
foreach my $analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
$self->_add_analysis_node($analysis);
}
foreach my $analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
$self->_add_control_rules( $analysis->control_rules_collection );
$self->_add_dataflow_rules( $analysis->dataflow_rules_collection );
}
if($self->config_get('DisplayStretched') ) { # put each analysis before its' funnel midpoint
foreach my $analysis ( Bio::EnsEMBL::Hive->collection('Analysis')->list ) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
if($analysis->{'_funnel_dfr'}) { # this should only affect analyses that have a funnel
my $from = _analysis_node_name( $analysis );
my $to = _midpoint_name( $analysis->{'_funnel_dfr'} );
......@@ -238,7 +241,7 @@ sub build {
if($self->config_get('DisplaySemaphoreBoxes') ) {
my %cluster_2_nodes = ();
foreach my $analysis (Bio::EnsEMBL::Hive->collection('Analysis')->list) {
foreach my $analysis ( Bio::EnsEMBL::Hive::Analysis->collection()->list ) {
if(my $funnel = $analysis->{'_funnel_dfr'}) {
push @{$cluster_2_nodes{ _midpoint_name( $funnel ) } }, _analysis_node_name( $analysis );
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment