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

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 45 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 351 CC 27 SCAR 26.8
CC
27
Cov
0%
status
72.0
needs_refresh
20.0
new
12.0
CRAP 14.6
1 package Bio::Graphics::Browser2::CachedTrack;
2  
3 # $Id$
4 # This package defines a Bio::Graphics::Browser2::Track option that manages
5 # the caching of track images and imagemaps.
6  
7 use strict;
8 use warnings;
9 use Carp;
10 use Fcntl ':flock';
11 use File::Spec;
12 use File::Path;
13 use IO::File;
14 use Digest::MD5 'md5_hex';
15 use Storable qw(:DEFAULT freeze thaw);
16  
17 # pending requests get 1 minute before they are considered likely to be defunct
18 use constant DEFAULT_REQUEST_TIME => 60;
19 use constant DEFAULT_CACHE_TIME => 60*60; # 1 hour
20  
21 # constructor:
22 # Bio::Graphics::Browser2::CachedTrack->new($cache_base_directory,$key_data)
23 # If $key_data is a scalar, then it is taken to be the literal key.
24 # Otherwise if it is an arrayref, it is an array of arguments that will be
25 # converted into the key.
26 sub new {
27 my $self = shift;
28 my %args = @_;
29 my $cache_base = $args{-cache_base};
30 my $panel_args = $args{-panel_args};
31 my $track_args = $args{-track_args};
32 my $extra_args = $args{-extra_args};
33 my $cache_time = $args{-cache_time};
34 my $key = $args{-key};
35  
36 -d $cache_base && -w _ or croak "$cache_base is not writable";
37  
38 # If next argument is a scalar, then it is our key to use.
39 # Otherwise, it is the data to use to generate a key.
40 unless ($key) {
41 $key = $self->generate_cache_key(@$panel_args,@$track_args,@$extra_args);
42 }
43  
44 my $obj = bless {
45 cache_base => $cache_base ,
46 key => $key,
47 panel_args => $panel_args,
48 track_args => $track_args,
49 extra_args => $extra_args,
50 cache_time => defined $cache_time ? $cache_time : DEFAULT_CACHE_TIME,
51 },ref $self || $self;
52 return $obj;
53 }
54  
55 sub cache_base { shift->{cache_base} }
56 sub lock_base { shift->{lock_base} }
57 sub key { shift->{key} }
58 sub panel_args { shift->{panel_args} }
59 sub track_args { shift->{track_args} }
60 sub extra_args { shift->{extra_args} }
61 sub max_time {
62 my $self = shift;
63 $self->{max_time} = shift if @_;
64 return $self->{max_time} || DEFAULT_REQUEST_TIME;
65 }
66 sub cache_time {
67 my $self = shift;
68 my $d = $self->{cache_time};
69 $self->{cache_time} = shift if @_;
70 return $d;
71 }
72 sub cachedir {
73 my $self = shift;
74 my $key = $self->key;
75 my @comp = $key =~ /(..)/g;
76 my $path = File::Spec->catfile($self->cache_base,@comp[0..2],$key);
77 mkpath ($path) unless -e $path;
78 die "Can't mkpath($path): $!" unless -d $path;
79 return $path;
80 }
81 sub dotfile {
82 my $self = shift;
83 return File::Spec->catfile($self->cachedir,'.lock');
84 }
85 sub tsfile {
86 my $self = shift;
87 return File::Spec->catfile($self->cachedir,'.ts');
88 }
89 sub datafile {
90 my $self = shift;
91 return File::Spec->catfile($self->cachedir,'data');
92 }
93  
94 sub errorfile {
95 my $self = shift;
96 return File::Spec->catfile($self->cachedir,'error');
97 }
98  
99 # given an arbitrary set of arguments, make a unique cache key
100 sub generate_cache_key {
101 my $self = shift;
102 my @args = map {$_ || ''} grep {!ref($_)} @_; # the map gets rid of uninit variable warnings
103 return md5_hex(sort @args);
104 }
105  
106 # lock the cache -- indicates that an update is in process
107 # we use simple dotfile locking
108 sub lock {
109 my $self = shift;
110 my $dotfile = $self->dotfile;
111 my $tsfile = $self->tsfile;
112 if (-e $dotfile) { # if it exists, then either we are in process or something died
113 return if $self->status eq 'PENDING';
114 }
115 my $f = IO::File->new(">$dotfile") or die "Can't open $dotfile for writing: $!";
116 flock $f,LOCK_EX;
117 $f->print($$,' ',time()); # PID<sp>timestamp
118 $f->close;
119 return 1;
120 }
121  
122 sub unlock {
123 my $self = shift;
124 my $dotfile = $self->dotfile;
125 unlink $dotfile;
126 }
127  
128 sub flag_error {
129 my $self = shift;
130 my $msg = shift;
131 my $errorfile = $self->errorfile;
132 open my $fh,'>',$errorfile or die;
133 print $fh $msg;
134 close $fh;
135 $self->unlock;
136 }
137  
138 sub errstr {
139 my $self = shift;
140 my $errorfile = $self->errorfile;
141 open my $fh,'<',$errorfile or return;
142 while (my $msg = <$fh>) {
143 chomp $msg;
144 next if $msg =~ /EXCEPTION/; # bioperl error header
145 $msg =~ s/MSG://; # more bioperl cruft
146 return $msg if $msg;
147 }
148 return 'unknown';
149 }
150  
151 sub put_data {
152 my $self = shift;
153 my ($gd,$map,$titles) = @_;
154 $self->{data}{gd} = $gd->can('gd2') ? $gd->gd2 : $gd;
155 $self->{data}{map} = $map;
156 $self->{data}{titles} = $titles;
157 my $datafile = $self->datafile;
158 store $self->{data},$datafile;
159 $self->unlock;
160 unlink $self->errorfile if -e $self->errorfile;
161 return;
162 }
163  
164 sub get_data {
165 my $self = shift;
166 my $ignore_expires = shift;
167 return $self->{data} if $self->{data};
168  
169 my $status = $self->status;
170 if ( ($status eq 'AVAILABLE') or
171 ($status eq 'EXPIRED' && $ignore_expires)) {
172 return $self->_get_data();
173 } else {
174 return;
175 }
176 }
177  
178 sub _get_data {
179 my $self = shift;
180 my $datafile = $self->datafile;
181 $self->{data} = retrieve($datafile);
182 return $self->{data};
183 }
184  
185 sub gd {
186 my $self = shift;
187 my $data = $self->get_data or return;
188  
189 # The ? statement here accomodates the storage of GD::SVG objects,
190 # which do not support the call to newFromPngData.
191 my $gd = (ref($data->{gd})
192 && ref($data->{gd})=~/^GD/)
193 ? $data->{gd}
194 : GD::Image->newFromGd2Data($data->{gd});
195 return $gd;
196 }
197  
198 sub map {
199 my $self = shift;
200 my $data = $self->get_data or return;
201 return $data->{map};
202 }
203  
204 sub titles {
205 my $self = shift;
206 my $data = $self->get_data or return;
207 return $data->{titles};
208 }
209  
210 sub width {
211 my $self = shift;
212 my $gd = $self->gd or return;
213 return ($gd->getBounds)[0];
214 }
215  
216 sub height {
217 my $self = shift;
218 my $gd = $self->gd or return;
219 return ($gd->getBounds)[1];
220 }
221  
222 # status returns one of four states
223 # 'EMPTY' no data available and no requests are pending
224 # 'PENDING' a request for the data is pending - current contents invalid
225 # 'AVAILABLE' data is available and no requests are pending
226 # 'DEFUNCT' a request for the data has timed out - current contents invalid
227 # 'EXPIRED' there is data, but it has expired
228 # 'ERROR' an error occurred, and data will never be available
229 sub status {
230 my $self = shift;
231 my $dir = $self->cachedir;
232 my $dotfile = $self->dotfile;
233 my $tsfile = $self->tsfile;
234 my $datafile = $self->datafile;
235 my $errorfile = $self->errorfile;
236  
237 # if a dotfile exists then either we are in the midst of updating the
238 # contents of the directory, or something has gone wrong and we are
239 # waiting forever.
240 if (-e $dotfile) {
241 -s _ or return 'PENDING'; # size zero means that dotfile has been created but not locked
242 my $f = IO::File->new($dotfile)
243 or return 'AVAILABLE'; # dotfile disappeared, so data has just become available
244 flock $f,LOCK_SH;
245 my ($pid,$timestamp) = split /\s+/,$f->getline();
246 $f->close;
247 return 'DEFUNCT' unless $timestamp;
248 unless (kill 0=>$pid) {
249 $self->flag_error('the rendering process crashed');
250 return 'ERROR';
251 }
252 return 'PENDING' if time()-$timestamp < $self->max_time;
253 $self->flag_error('timeout; try viewing a smaller region');
254 return 'ERROR';
255 } elsif (-e $datafile) {
256 return $self->expired($datafile) ? 'EXPIRED' : 'AVAILABLE';
257 } elsif (-e $errorfile) {
258 return 'ERROR';
259 } else {
260 return 'EMPTY';
261 }
262 }
263  
264 sub needs_refresh {
265 my $self = shift;
266 my $status = $self->status;
267 return 1 if $status eq 'EMPTY';
268 return 1 if $status eq 'EXPIRED';
269 return 1 if $status eq 'DEFUNCT';
270 return;
271 }
272  
273 sub expired {
274 my $self = shift;
275 my $datafile = shift;
276 my $cache_time= $self->cache_time;
277 my $time = time();
278  
279 my $mtime = (stat($datafile))[9];
280 my $elapsed = $time-$mtime;
281 return 0 if ( $mtime and not $cache_time);
282 return $elapsed > $cache_time;
283 }
284  
285 1;