File Coverage

Bio/Prospect/Client.pm
Criterion Covered Total %
statement 27 55 49.0
branch 6 26 23.0
condition 0 6 0.0
subroutine 5 8 62.5
pod 1 1 100.0
total 39 96 40.6


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Bio::Prospect::Client -- base class for Bio::Prospect::LocalClient and
4             Bio::Prospect::SoapClient.
5             S<$Id: Client.pm,v 1.16 2003/11/18 19:45:45 rkh Exp $>
6              
7             =head1 SYNOPSIS
8              
9             This is an abstract class and is intended only for subclassing.
10              
11             =head1 DESCRIPTION
12              
13             B<Bio::Prospect::Client> is the abstract base class for Bio::Prospect::LocalClient and
14             Bio::Prospect::SoapClient. Not intended to be instantiated directly.
15              
16             =head1 ROUTINES & METHODS
17              
18             =cut
19              
20             package Bio::Prospect::Client;
21 1     1   5 use strict;
  1         3  
  1         28  
22 1     1   5 use warnings;
  1         2  
  1         24  
23 1     1   1226 use File::Temp;
  1         27948  
  1         89  
24 1     1   9 use vars qw( $VERSION );
  1         12  
  1         715  
25             $VERSION = sprintf( "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/ );
26              
27              
28             #-------------------------------------------------------------------------------
29             # new()
30             #-------------------------------------------------------------------------------
31              
32             =head2 new()
33              
34             Name: new()
35             Purpose: constructor
36             Arguments: 'tempdir' => directory to create temporary files (optional)
37             Returns: Bio::Prospect::Client
38              
39             =cut
40              
41             sub new {
42 1     1 1 3 my $type = shift;
43 1         2 my $self = {};
44 1 50       4 if (ref $_[0])
45 1         2 { %{$self} = %{$_[0]}; }
  1         2  
  1         4  
46             else
47 0         0 { %{$self} = @_; }
  0         0  
48 1         4 bless($self,$type);
49              
50 1 50       9 if ( ! defined $self->{'tempdir'} ) {
51 1         8 $self->{tempdir} = File::Temp::tempdir(
52             '/tmp/'.__PACKAGE__.'-XXXX',
53             CLEANUP=>!$ENV{DEBUG} );
54 1 50       528 defined $self->{tempdir}
55             or throw Bio::Prospect::RuntimeError( "couldn't create temporary directory" );
56             }
57 1 50       17 if ( ! -w $self->{tempdir} ) {
58 0         0 throw Bio::Prospect::RuntimeError( "tempdir (" . $self->{tempdir} . ") is not writeable" );
59             }
60              
61 1 50       3 print(STDERR "tempdir: " . $self->{tempdir} . "\n") if $ENV{'DEBUG'};
62              
63 1 50       4 if (not defined $self->{cacheLimit})
64 1         2 { $self->{cacheLimit} = 25; }
65              
66 1         3 return $self;
67             }
68              
69              
70              
71             #-------------------------------------------------------------------------------
72             # _tempfile()
73             #-------------------------------------------------------------------------------
74              
75             =head2 _tempfile()
76              
77             Name: _tempfile()
78             Purpose: return the filename of a temporary file
79             Arguments: suffix for filename (optional)
80             Returns: filename
81              
82             =cut
83              
84             sub _tempfile {
85 0     0     my $self = shift;
86 0 0         my $sfx = @_ ? ".$_[0]" : undef;
87 0           return File::Temp::tempfile( DIR=>$self->{tempdir}, SUFFIX=>$sfx, UNLINK=>0 );
88             }
89              
90              
91             #-------------------------------------------------------------------------------
92             # _get_cache_file()
93             #-------------------------------------------------------------------------------
94              
95             =head2 _get_cache_file()
96              
97             Name: _get_cache_file()
98             Purpose: return the value for a given key in a given cache
99             Arguments: key, cache name
100             Returns: value
101              
102             =cut
103              
104             sub _get_cache_file {
105 0     0     my ($self,$key,$cacheName) = @_;
106              
107 0 0         if ( defined $self->{'cache'}{$cacheName}{$key}{'fn'}) {
108 0           return $self->{'cache'}{$cacheName}{$key}{'fn'};
109             } else {
110 0           return;
111             }
112             }
113              
114              
115             #-------------------------------------------------------------------------------
116             # _put_cache_file()
117             #-------------------------------------------------------------------------------
118              
119             =head2 _put_cache_file()
120              
121             Name: _put_cache_file()
122             Purpose: put a filename into a given cache using a given key
123             Arguments: key, cache name, value
124             Returns: value
125              
126             =cut
127              
128             sub _put_cache_file {
129 0     0     my ($self,$key,$cacheName,$fn) = @_;
130              
131 0 0         if ( !defined $self->{'cache'}{$cacheName} ) {
132 0           $self->{'cache'}{$cacheName} = {};
133             }
134 0           my $cache = $self->{'cache'}{$cacheName};
135              
136             # cache this result
137 0 0         print(STDERR "## caching $fn in '$cacheName' file cache using a key of $key ...\n") if $ENV{DEBUG};
138 0           $cache->{$key}{'fn'} = $fn;
139 0           $cache->{$key}{'timestamp'} = time;
140              
141             # expire oldest
142 0 0 0       if ( defined $cache and ( exists $self->{cacheLimit} ) and ( scalar keys %{$cache} >= $self->{cacheLimit} ) ) {
  0   0        
143 0           foreach my $key ( sort { $cache->{$a}{'timestamp'} <=> $cache->{$b}{'timestamp'} } keys %{$cache} ) {
  0            
  0            
144 0 0         print STDERR "deleting $key because it is the oldest key: " . $cache->{$key}{'timestamp'} . "\n" if $ENV{DEBUG};
145 0 0         print STDERR "unlinking " . $cache->{$key}{'fn'} . "\n" if $ENV{DEBUG};
146 0           unlink $cache->{$key}{'fn'};
147 0           delete $cache->{$key};
148 0           last;
149             }
150             }
151 0           return;
152             }
153              
154              
155             =pod
156              
157             =head1 BUGS
158              
159             =head1 SEE ALSO
160              
161             Bio::Prospect::LocalClient
162             Bio::Prospect::SoapClient
163              
164             =cut
165              
166             1;