EnsEMBL/DBSQL/Support/BaseCache.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 158 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';
85 use strict;
86 use warnings;
87  
88 use Bio::EnsEMBL::Utils::Exception qw/throw/;
89 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref/;
90 use Scalar::Util qw/weaken/;
91  
92 =head2 new
93  
94 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $db_adaptor
95 Example : $cache = CacheInheritedFromBaseCache->new($db_adaptor);
96 Description: Creates a new cache class which handles all the basics of
97 working with a cache apart from what that cache implementation
98 is (apart from a hash)
99 Returntype : Bio::EnsEMBL::DBSQL::Support::BaseCache
100 Exceptions : none
101 Caller : BaseAdaptors
102 Status : Beta
103  
104 =cut
105  
106 sub new {
107 my ($class, $adaptor) = @_;
108
109 $class = ref($class) || $class;
110 my $self = bless({}, $class);
111
112 throw "Need an adaptor instance to delegate calls to" unless $adaptor;
113 $self->adaptor($adaptor);
114
115 return $self;
116 }
117  
118 =head2 adaptor
119  
120 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $db_adaptor
121 Description: Getter setter for the adaptor this serves as an ID cacher for
122 Returntype : Bio::EnsEMBL::DBSQL::BaseAdaptor
123 Exceptions : none
124 Caller : BaseAdaptors
125 Status : Beta
126  
127 =cut
128  
129  
130 sub adaptor {
131 my ($self, $adaptor) = @_;
132 if(defined $adaptor) {
133 assert_ref($adaptor, 'Bio::EnsEMBL::DBSQL::BaseAdaptor', 'adaptor');
134 $self->{'adaptor'} = $adaptor;
135 weaken($self->{'adaptor'});
136 }
137 return $self->{'adaptor'};
138 }
139  
140 =head2 cache
141  
142 Description: Returns back a Hash implementing object and also calls
143 C<build_cache()> for an initialise on demand system
144 Returntype : Hash
145 Exceptions : none
146 Caller : BaseAdaptors and internal
147 Status : Beta
148  
149 =cut
150  
151 sub cache {
152 my ($self) = @_;
153 if(! defined $self->{cache}) {
154 my $cache = $self->build_cache();
155 $self->{cache} = $cache;
156 }
157 return $self->{cache};
158 }
159  
160 =head2 delete_cache
161  
162 Example : $cache->delete_cache();
163 Description: Deletes the cache. Normally used to trigger a C<build_cache()>
164 call
165 Returntype : none
166 Exceptions : none
167 Caller : BaseAdaptors
168 Status : Beta
169  
170 =cut
171  
172 sub delete_cache {
173 my ($self) = @_;
174 delete $self->{cache};
175 return;
176 }
177  
178 =head2 get
179  
180 Arg [1] : String key to retrieve
181 Example : $cache->put(1,'a'); is($cache->get(1), 'a');
182 Description: Retrieves the value held in the cache. Can return undef if the
183 value could not be found
184 Returntype : Scalar value held in the cache or nothing
185 Exceptions : If key was undefined
186 Caller : BaseAdaptors
187 Status : Beta
188  
189 =cut
190  
191 sub get {
192 my ($self, $key) = @_;
193 throw "No key given" unless defined $key;
194 my $cache = $self->cache();
195 if(exists $cache->{$key}) {
196 return $cache->{$key};
197 }
198 return;
199 }
200  
201 =head2 get_by_list
202  
203 Arg [1] : ArrayRef keys to retrieve
204 Example : is($cache->get_by_list([1,2]), ['a','b']);
205 Description: Retrieves the values held in the cache. If a key cannot be
206 found you get no entry in the resulting array returned.
207 Returntype : ArrayRef of found values
208 Exceptions : If the given ArrayRef was undefined
209 Caller : BaseAdaptors
210 Status : Beta
211  
212 =cut
213  
214 sub get_by_list {
215 my ($self, $list) = @_;
216 assert_ref($list, 'ARRAY', 'list');
217 my @output;
218 my $cache = $self->cache();
219 foreach my $key (@{$list}) {
220 next if ! defined $key;
221 if(exists $cache->{$key}) {
222 push(@output, $cache->{$key});
223 }
224 }
225 return \@output;
226 }
227  
228 =head2 get_by_sql
229  
230 Arg [1] : String SQL to execute. Should return the key of this cache in column 1
231 Arg [2] : ArrayRef Parameters to bind to the specified query
232 Example : $cache->get_by_sql('select id from tables where p like ?', ['val%']);
233 Description: Executes the given SQL statement against the construnction adaptor's
234 backing database. The found IDs are then passed into C<get_by_list()>
235 where the elements are returned should the cache hold them.
236  
237 Remember if the cache is un-aware of the key or the specific
238 implementation used cannot perform database lookups based on cache misses
239 you will not be able to retrieve the object in question.
240 Returntype : ArrayRef of found values
241 Exceptions : Thrown if SQL and parameters are empty and not the expected types. All
242 other exceptions come from DBI/SQL operations.
243 Caller : BaseAdaptors
244 Status : Beta
245  
246 =cut
247  
248 sub get_by_sql {
249 my ($self, $sql, $params) = @_;
250 throw "No SQL given" unless $sql;
251 assert_ref($params, 'ARRAY', 'params');
252 my $helper = $self->adaptor()->dbc()->sql_helper();
253 my $ids = $helper->execute_simple(-SQL => $sql, -PARAMS => $params);
254 return $self->get_by_list($ids);
255 }
256  
257 =head2 put
258  
259 Arg [1] : String key to store
260 Arg [2] : Scalar value to store
261 Example : $cache->put(2, 'b');
262 Description: Stores a value in the cache. Returns the previous value held
263 under that key if one existed
264 Returntype : Scalar of the previously held value if one existed
265 Exceptions : If key was undefined
266 Caller : BaseAdaptors
267 Status : Beta
268  
269 =cut
270  
271 sub put {
272 my ($self, $key, $new) = @_;
273 throw "No key given" unless defined $key;
274 my $cache = $self->cache();
275 my $old;
276 if(exists $cache->{$key}) {
277 $old = $cache->{$key};
278 }
279 $cache->{$key} = $new;
280 return $old if $old;
281 return;
282 }
283  
284 =head2 remove
285  
286 Arg [1] : String key to remove
287 Example : is($cache->remove(1), 'a');
288 Description: Removes the supplied key from the cache
289 Returntype : Scalar value held in the cache or nothing
290 Exceptions : If key was undefined
291 Caller : BaseAdaptors
292 Status : Beta
293  
294 =cut
295  
296 sub remove {
297 my ($self, $key) = @_;
298 throw "No key given" unless defined $key;
299 my $cache = $self->cache();
300 my $old;
301 if(exists $cache->{$key}) {
302 $old = $cache->{$key};
303 delete $cache->{$key};
304 }
305 return $old if $old;
306 return;
307 }
308  
309 =head2 clear_cache
310  
311 Example : $cache->clear_cache();
312 Description: Removes all values from the cache but does not delete the cache
313 instance
314 Returntype : None
315 Exceptions : None
316 Caller : BaseAdaptors
317 Status : Beta
318  
319 =cut
320  
321 sub clear_cache {
322 my ($self) = @_;
323 #Clear the cache
324 my $cache = $self->cache();
325 %{$cache} = ();
326 return;
327 }
328  
329 =head2 cache_keys
330  
331 Example : my @keys = $cache->cache_keys();
332 Description: Returns all keys held by the cache
333 Returntype : List of all available keys
334 Exceptions : None
335 Caller : BaseAdaptors
336 Status : Beta
337  
338 =cut
339  
340 sub cache_keys {
341 my ($self) = @_;
342 return keys %{$self->cache()};
343 }
344  
345  
346 =head2 cached_values
347  
348 Example : my @values = $cache->cached_values();
349 Description: Returns all values held by the cache
350 Returntype : List of all available values
351 Exceptions : None
352 Caller : BaseAdaptors
353 Status : Beta
354  
355 =cut
356  
357 sub cached_values {
358 my ($self) = @_;
359 return values %{$self->cache()};
360 }
361  
362 =head2 size
363  
364 Example : $cache->size();
365 Description: Returns the number of keys currrently held by the cache
366 Returntype : Int $size
367 Exceptions : None
368 Caller : BaseAdaptors
369 Status : Beta
370  
371 =cut
372  
373 sub size {
374 my ($self) = @_;
375 return scalar($self->cache_keys());
376 }
377  
378 =head2 build_cache
379  
380 Description: Implement to return the required type of cache
381 Returntype : Hash
382 Exceptions : None
383 Caller : BaseAdaptors
384 Status : Beta
385  
386 =cut
387  
388 sub build_cache {
389 my ($self) = @_;
390 die 'Unimplemented';
391 }
392  
393 1;