EnsEMBL/DBSQL/Support/BaseCache.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 157 CC 15 SCAR 22.1
CC
15
Cov
0%
put
20.0
remove
20.0
get
12.0
CRAP 9.2
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
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  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::Support::BaseCache - Base cache code
34  
35 =head1 SYNOPSIS
36  
37 package Cache;
38
39 use base qw/Bio::EnsEMBL::DBSQL::Support::BaseCache/;
40
41 sub build_cache {
42 return {}; #sends back a very basic cache which is a hash
43 }
44
45 1;
46
47 #In use
48 $cache->put(1, 'a');
49 is($cache->get(1), 'a');
50 is($cache->put(1, 'b'), 'a'); #put returns the object it replaced
51 is($cache->delete(1), 'b'); #delete returns the object it removed
52
53 is_deeply([$cache->cache_keys()], [1]);
54
55 $cache->clear_cache();
56
57 is($cache->size(), 0);
58  
59 #Try using SQL - cache will be consulted accordingly
60 my $ids = $cache->get_by_sql('select dbid from table where val like ?', ['someval%']);
61  
62 =head1 DESCRIPTION
63  
64 A base class used for holding methods common to all cache implementations.
65 Never use this class to do direct caching instead use one of the following
66  
67 =over 8
68  
69 =item C<Bio::EnsEMBL::DBSQL::Support::LruIdCache>
70  
71 =item C<Bio::EnsEMBL::DBSQL::Support::FullIdCache>
72  
73 =back
74  
75 To provide exta functionality to the caches you should override one of the above
76 classes and extend. Caches work when you use inheritence by composition in their
77 target adaptor.
78  
79 =head1 METHODS
80  
81 =cut
82  
83 package Bio::EnsEMBL::DBSQL::Support::BaseCache;
84 $Bio::EnsEMBL::DBSQL::Support::BaseCache::VERSION = '114.0.0_50'; # TRIAL
85 $Bio::EnsEMBL::DBSQL::Support::BaseCache::VERSION = '114.0.050';
86 use strict;
87 use warnings;
88  
89 use Bio::EnsEMBL::Utils::Exception qw/throw/;
90 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref/;
91 use Scalar::Util qw/weaken/;
92  
93 =head2 new
94  
95 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $db_adaptor
96 Example : $cache = CacheInheritedFromBaseCache->new($db_adaptor);
97 Description: Creates a new cache class which handles all the basics of
98 working with a cache apart from what that cache implementation
99 is (apart from a hash)
100 Returntype : Bio::EnsEMBL::DBSQL::Support::BaseCache
101 Exceptions : none
102 Caller : BaseAdaptors
103 Status : Beta
104  
105 =cut
106  
107 sub new {
108 my ($class, $adaptor) = @_;
109
110 $class = ref($class) || $class;
111 my $self = bless({}, $class);
112
113 throw "Need an adaptor instance to delegate calls to" unless $adaptor;
114 $self->adaptor($adaptor);
115
116 return $self;
117 }
118  
119 =head2 adaptor
120  
121 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $db_adaptor
122 Description: Getter setter for the adaptor this serves as an ID cacher for
123 Returntype : Bio::EnsEMBL::DBSQL::BaseAdaptor
124 Exceptions : none
125 Caller : BaseAdaptors
126 Status : Beta
127  
128 =cut
129  
130  
131 sub adaptor {
132 my ($self, $adaptor) = @_;
133 if(defined $adaptor) {
134 assert_ref($adaptor, 'Bio::EnsEMBL::DBSQL::BaseAdaptor', 'adaptor');
135 $self->{'adaptor'} = $adaptor;
136 weaken($self->{'adaptor'});
137 }
138 return $self->{'adaptor'};
139 }
140  
141 =head2 cache
142  
143 Description: Returns back a Hash implementing object and also calls
144 C<build_cache()> for an initialise on demand system
145 Returntype : Hash
146 Exceptions : none
147 Caller : BaseAdaptors and internal
148 Status : Beta
149  
150 =cut
151  
152 sub cache {
153 my ($self) = @_;
154 if(! defined $self->{cache}) {
155 my $cache = $self->build_cache();
156 $self->{cache} = $cache;
157 }
158 return $self->{cache};
159 }
160  
161 =head2 delete_cache
162  
163 Example : $cache->delete_cache();
164 Description: Deletes the cache. Normally used to trigger a C<build_cache()>
165 call
166 Returntype : none
167 Exceptions : none
168 Caller : BaseAdaptors
169 Status : Beta
170  
171 =cut
172  
173 sub delete_cache {
174 my ($self) = @_;
175 delete $self->{cache};
176 return;
177 }
178  
179 =head2 get
180  
181 Arg [1] : String key to retrieve
182 Example : $cache->put(1,'a'); is($cache->get(1), 'a');
183 Description: Retrieves the value held in the cache. Can return undef if the
184 value could not be found
185 Returntype : Scalar value held in the cache or nothing
186 Exceptions : If key was undefined
187 Caller : BaseAdaptors
188 Status : Beta
189  
190 =cut
191  
192 sub get {
193 my ($self, $key) = @_;
194 throw "No key given" unless defined $key;
195 my $cache = $self->cache();
196 if(exists $cache->{$key}) {
197 return $cache->{$key};
198 }
199 return;
200 }
201  
202 =head2 get_by_list
203  
204 Arg [1] : ArrayRef keys to retrieve
205 Example : is($cache->get_by_list([1,2]), ['a','b']);
206 Description: Retrieves the values held in the cache. If a key cannot be
207 found you get no entry in the resulting array returned.
208 Returntype : ArrayRef of found values
209 Exceptions : If the given ArrayRef was undefined
210 Caller : BaseAdaptors
211 Status : Beta
212  
213 =cut
214  
215 sub get_by_list {
216 my ($self, $list) = @_;
217 assert_ref($list, 'ARRAY', 'list');
218 my @output;
219 my $cache = $self->cache();
220 foreach my $key (@{$list}) {
221 next if ! defined $key;
222 if(exists $cache->{$key}) {
223 push(@output, $cache->{$key});
224 }
225 }
226 return \@output;
227 }
228  
229 =head2 get_by_sql
230  
231 Arg [1] : String SQL to execute. Should return the key of this cache in column 1
232 Arg [2] : ArrayRef Parameters to bind to the specified query
233 Example : $cache->get_by_sql('select id from tables where p like ?', ['val%']);
234 Description: Executes the given SQL statement against the construnction adaptor's
235 backing database. The found IDs are then passed into C<get_by_list()>
236 where the elements are returned should the cache hold them.
237  
238 Remember if the cache is un-aware of the key or the specific
239 implementation used cannot perform database lookups based on cache misses
240 you will not be able to retrieve the object in question.
241 Returntype : ArrayRef of found values
242 Exceptions : Thrown if SQL and parameters are empty and not the expected types. All
243 other exceptions come from DBI/SQL operations.
244 Caller : BaseAdaptors
245 Status : Beta
246  
247 =cut
248  
249 sub get_by_sql {
250 my ($self, $sql, $params) = @_;
251 throw "No SQL given" unless $sql;
252 assert_ref($params, 'ARRAY', 'params');
253 my $helper = $self->adaptor()->dbc()->sql_helper();
254 my $ids = $helper->execute_simple(-SQL => $sql, -PARAMS => $params);
255 return $self->get_by_list($ids);
256 }
257  
258 =head2 put
259  
260 Arg [1] : String key to store
261 Arg [2] : Scalar value to store
262 Example : $cache->put(2, 'b');
263 Description: Stores a value in the cache. Returns the previous value held
264 under that key if one existed
265 Returntype : Scalar of the previously held value if one existed
266 Exceptions : If key was undefined
267 Caller : BaseAdaptors
268 Status : Beta
269  
270 =cut
271  
272 sub put {
273 my ($self, $key, $new) = @_;
274 throw "No key given" unless defined $key;
275 my $cache = $self->cache();
276 my $old;
277 if(exists $cache->{$key}) {
278 $old = $cache->{$key};
279 }
280 $cache->{$key} = $new;
281 return $old if $old;
282 return;
283 }
284  
285 =head2 remove
286  
287 Arg [1] : String key to remove
288 Example : is($cache->remove(1), 'a');
289 Description: Removes the supplied key from the cache
290 Returntype : Scalar value held in the cache or nothing
291 Exceptions : If key was undefined
292 Caller : BaseAdaptors
293 Status : Beta
294  
295 =cut
296  
297 sub remove {
298 my ($self, $key) = @_;
299 throw "No key given" unless defined $key;
300 my $cache = $self->cache();
301 my $old;
302 if(exists $cache->{$key}) {
303 $old = $cache->{$key};
304 delete $cache->{$key};
305 }
306 return $old if $old;
307 return;
308 }
309  
310 =head2 clear_cache
311  
312 Example : $cache->clear_cache();
313 Description: Removes all values from the cache but does not delete the cache
314 instance
315 Returntype : None
316 Exceptions : None
317 Caller : BaseAdaptors
318 Status : Beta
319  
320 =cut
321  
322 sub clear_cache {
323 my ($self) = @_;
324 #Clear the cache
325 my $cache = $self->cache();
326 %{$cache} = ();
327 return;
328 }
329  
330 =head2 cache_keys
331  
332 Example : my @keys = $cache->cache_keys();
333 Description: Returns all keys held by the cache
334 Returntype : List of all available keys
335 Exceptions : None
336 Caller : BaseAdaptors
337 Status : Beta
338  
339 =cut
340  
341 sub cache_keys {
342 my ($self) = @_;
343 return keys %{$self->cache()};
344 }
345  
346  
347 =head2 cached_values
348  
349 Example : my @values = $cache->cached_values();
350 Description: Returns all values held by the cache
351 Returntype : List of all available values
352 Exceptions : None
353 Caller : BaseAdaptors
354 Status : Beta
355  
356 =cut
357  
358 sub cached_values {
359 my ($self) = @_;
360 return values %{$self->cache()};
361 }
362  
363 =head2 size
364  
365 Example : $cache->size();
366 Description: Returns the number of keys currrently held by the cache
367 Returntype : Int $size
368 Exceptions : None
369 Caller : BaseAdaptors
370 Status : Beta
371  
372 =cut
373  
374 sub size {
375 my ($self) = @_;
376 return scalar($self->cache_keys());
377 }
378  
379 =head2 build_cache
380  
381 Description: Implement to return the required type of cache
382 Returntype : Hash
383 Exceptions : None
384 Caller : BaseAdaptors
385 Status : Beta
386  
387 =cut
388  
389 sub build_cache {
390 my ($self) = @_;
391 die 'Unimplemented';
392 }
393  
394 1;