Cacheable.pm 3.17 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
4
Copyright [2016-2021] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

Roy Storey's avatar
Roy Storey committed
20 21 22 23 24 25 26 27
=pod

=head1 NAME

Bio::EnsEMBL::Hive::Cacheable - base class to cache collections

=cut

28 29
package Bio::EnsEMBL::Hive::Cacheable;

30 31 32
use strict;
use warnings;

33
use Scalar::Util qw(weaken);
34
use Bio::EnsEMBL::Hive::Utils::URL ('hash_to_url');
35 36 37 38 39 40 41 42 43 44


sub hive_pipeline {
    my $self = shift @_;
    if (@_) {
        $self->{'_hive_pipeline'} = shift @_;
        weaken($self->{'_hive_pipeline'});
    }
    return $self->{'_hive_pipeline'};
}
45

46 47 48 49 50 51 52 53 54

sub is_local_to {
    my $self            = shift @_;
    my $rel_pipeline    = shift @_;

    return $self->hive_pipeline == $rel_pipeline;
}


55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
sub count_local_and_remote_objects {
    my $self            = shift @_;
    my $objects         = shift @_;

    my $this_pipeline   = $self->hive_pipeline;
    my $local_count     = 0;
    my $remote_count    = 0;

    foreach my $object (@$objects) {
        if($object->hive_pipeline == $this_pipeline) {
            $local_count++;
        } else {
            $remote_count++;
        }
    }

    return ($local_count, $remote_count);
}


75 76 77 78 79
sub relative_display_name {
    my ($self, $ref_pipeline) = @_;  # if 'reference' hive_pipeline is the same as 'my' hive_pipeline, a shorter display_name is generated

    my $my_pipeline = $self->hive_pipeline;
    my $my_dba      = $my_pipeline && $my_pipeline->hive_dba;
80 81 82 83 84 85 86 87 88 89

    if ($my_dba and !$self->is_local_to($ref_pipeline)) {
        if (($my_dba->dbc->driver eq 'sqlite') and ($my_dba->dbc->dbname =~ /([^\/]*)$/)) {
            return $1 . '/' . $self->display_name;
        } else {
            return $my_dba->dbc->dbname . '/' . $self->display_name;
        }
    } else {
        return $self->display_name;
    }
90 91
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

sub relative_url {
     my ($self, $ref_pipeline) = @_;  # if 'reference' hive_pipeline is the same as 'my' hive_pipeline, a shorter url is generated

    my $my_pipeline = $self->hive_pipeline;
    my $my_dba      = $my_pipeline && $my_pipeline->hive_dba;
    my $url_hash    = ($my_dba and !$self->is_local_to($ref_pipeline) ) ? $my_dba->dbc->to_url_hash : {};

    $url_hash->{'query_params'} = $self->url_query_params;      # calling a specific method for each class that supports URLs

    my $object_type = ref($self);
    $object_type=~s/^.+:://;
    $url_hash->{'query_params'}{'object_type'} = $object_type;

    return Bio::EnsEMBL::Hive::Utils::URL::hash_to_url( $url_hash );
}


110 111 112 113 114 115
sub display_name {
    my ($self) = @_;
    return "$self";     # Default implementation
}


116 117 118 119 120
sub unikey {    # to be redefined by individual Cacheable classes
    return undef;
}


121
1;