blib/lib/Bio/Graphics/Browser2/DataBase.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 139 CC 11 SCAR 30.1
CC
11
Cov
0%
open_database
56.0
set
20.0
get
6.0
CRAP 20.3
1 package Bio::Graphics::Browser2::DataBase;
2  
3 # This module maintains a cache of opened genome databases
4 # keyed by the database module name and the parameters
5 # passed to new(). It is intended to improve performance
6 # on in-memory databases and other databases that have
7 # a relatively slow startup time.
8  
9 =head1 NAME
10  
11 Bio::Graphics::Browser2::DataBase -- A simple cache for database handles
12  
13 =head1 SYNOPSIS
14  
15  
16 =head1 DESCRIPTION
17  
18 =head2 METHODS
19  
20 =cut
21  
22 use strict;
23 use warnings;
24 use Data::Dumper 'Dumper';
25 use constant DEBUG=>0;
26  
27 # Cache this many databases in a LRU cache.
28 # If you are getting too many open files errors, then set this
29 # lower.
30 use constant CACHE_SIZE => 100;
31  
32 my $CACHE = LRUCache->new(CACHE_SIZE);
33  
34 sub open_database {
35 my $self = shift;
36 my ($adaptor,@argv) = @_;
37  
38 my $key = Dumper($adaptor,@argv);
39 my $db = $CACHE->get($key);
40 return $db if defined $db;
41  
42 my @caller = caller(1);
43 warn "[$$] open database @argv from @caller" if DEBUG;
44 $db = eval {$adaptor->new(@argv)};
45  
46 if (!$db && $@ =~ /too many open files/) {
47 warn "Too many open databases. Clearing and trying again.\n";
48 warn "You may wish to adjust the CACHE_SIZE constant in Bio/Graphics/Browser2/DataBase.pm";
49 $CACHE->clear(); # last ditch attempt to free filehandles
50 $db = eval {$adaptor->new(@argv)};
51 }
52 unless ($db) {
53 warn "Could not open database: $@";
54 return;
55 }
56  
57 $db->strict_bounds_checking(1) if $db->can('strict_bounds_checking');
58 $db->absolute(1) if $db->can('absolute');
59 $CACHE->set($key,$db);
60 $db;
61 }
62  
63 sub delete_database {
64 my $self = shift;
65 my $key = Dumper(@_);
66 $CACHE->delete($key);
67 }
68  
69 =item Bio::Graphics::Browser2::DataBase->clone_databases()
70  
71 Call this after a fork in the child process to make sure that all open
72 databases have had a chance to clone themselves if they need
73 to. Otherwise you will get random database failures.
74  
75 =cut
76  
77 sub clone_databases {
78 my $self = shift;
79 eval {$_->clone()}
80 foreach $CACHE->values();
81 }
82  
83  
84 package LRUCache;
85  
86 sub new {
87 my $self = shift;
88 my $maxopen = shift || 20;
89 return bless {maxopen => $maxopen,
90 curopen => 0,
91 cacheseq => {},
92 cachedata => {},
93 },ref $self || $self;
94 }
95  
96 sub delete {
97 my $self = shift;
98 my $key = shift;
99 delete $self->{cachedata}{$key};
100 delete $self->{cacheseq}{$key};
101 $self->{curopen}--;
102 }
103  
104 sub get {
105 my $self = shift;
106 my $key = shift;
107 my $obj = $self->{cachedata}{$key};
108 return unless $obj;
109 $self->{cacheseq}{$key}++;
110 return $obj;
111 }
112  
113 sub set {
114 my $self = shift;
115 my ($key,$obj) = @_;
116  
117 if (exists $self->{cachedata}{$key}) {
118 $self->{cachedata}{$key} = $obj;
119 $self->{cacheseq}{$key} = 1;
120 return;
121 }
122  
123 if ($self->{curopen} >= $self->{maxopen}) {
124 my @lru = sort {$self->{cacheseq}{$a} <=> $self->{cacheseq}{$b}}
125 keys %{$self->{cachedata}};
126 splice(@lru, $self->{maxopen} / 3);
127 $self->{curopen} -= @lru;
128 foreach (@lru) {
129 delete $self->{cachedata}{$_};
130 delete $self->{cacheseq}{$_};
131 }
132  
133 warn "garbage collecting done, values = ",join ' ',$self->values
134 if Bio::Graphics::Browser2::DataBase::DEBUG;
135 }
136  
137 $self->{cacheseq}{$key}=1;
138 $self->{curopen}++;
139 $self->{cachedata}{$key} = $obj;
140 }
141  
142 sub keys {
143 my $self = shift;
144 return keys %{$self->{cachedata}};
145 }
146  
147 sub values {
148 my $self = shift;
149 return values %{$self->{cachedata}};
150 }
151  
152 sub clear {
153 my $self = shift;
154 $self->{cacheseq} = {};
155 $self->{cachedata} = {};
156 }
157  
158 1;