ensembl-hive  2.5
Cacheable.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4 Copyright [2016-2022] EMBL-European Bioinformatics Institute
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 =pod
21 
22 =head1 NAME
23 
24 Bio::EnsEMBL::Hive::Cacheable - base class to cache collections
25 
26 =cut
27 
28 package Bio::EnsEMBL::Hive::Cacheable;
29 
30 use strict;
31 use warnings;
32 
33 use Scalar::Util qw(weaken);
34 use Bio::EnsEMBL::Hive::Utils::URL ('hash_to_url');
35 
36 
37 sub hive_pipeline {
38  my $self = shift @_;
39  if (@_) {
40  $self->{'_hive_pipeline'} = shift @_;
41  weaken($self->{'_hive_pipeline'});
42  }
43  return $self->{'_hive_pipeline'};
44 }
45 
46 
47 sub is_local_to {
48  my $self = shift @_;
49  my $rel_pipeline = shift @_;
50 
51  return $self->hive_pipeline == $rel_pipeline;
52 }
53 
54 
55 sub count_local_and_remote_objects {
56  my $self = shift @_;
57  my $objects = shift @_;
58 
59  my $this_pipeline = $self->hive_pipeline;
60  my $local_count = 0;
61  my $remote_count = 0;
62 
63  foreach my $object (@$objects) {
64  if($object->hive_pipeline == $this_pipeline) {
65  $local_count++;
66  } else {
67  $remote_count++;
68  }
69  }
70 
71  return ($local_count, $remote_count);
72 }
73 
74 
75 sub relative_display_name {
76  my ($self, $ref_pipeline) = @_; # if 'reference' hive_pipeline is the same as 'my' hive_pipeline, a shorter display_name is generated
77 
78  my $my_pipeline = $self->hive_pipeline;
79  my $my_dba = $my_pipeline && $my_pipeline->hive_dba;
80 
81  if ($my_dba and !$self->is_local_to($ref_pipeline)) {
82  if (($my_dba->dbc->driver eq 'sqlite') and ($my_dba->dbc->dbname =~ /([^\/]*)$/)) {
83  return $1 . '/' . $self->display_name;
84  } else {
85  return $my_dba->dbc->dbname . '/' . $self->display_name;
86  }
87  } else {
88  return $self->display_name;
89  }
90 }
91 
92 
93 sub relative_url {
94  my ($self, $ref_pipeline) = @_; # if 'reference' hive_pipeline is the same as 'my' hive_pipeline, a shorter url is generated
95 
96  my $my_pipeline = $self->hive_pipeline;
97  my $my_dba = $my_pipeline && $my_pipeline->hive_dba;
98  my $url_hash = ($my_dba and !$self->is_local_to($ref_pipeline) ) ? $my_dba->dbc->to_url_hash : {};
99 
100  $url_hash->{'query_params'} = $self->url_query_params; # calling a specific method for each class that supports URLs
101 
102  my $object_type = ref($self);
103  $object_type=~s/^.+:://;
104  $url_hash->{'query_params'}{'object_type'} = $object_type;
105 
107 }
108 
109 
110 sub display_name {
111  my ($self) = @_;
112  return "$self"; # Default implementation
113 }
114 
115 
116 sub unikey { # to be redefined by individual Cacheable classes
117  return undef;
118 }
119 
120 
121 1;