EnsEMBL/DBSQL/Support/FullIdCache.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 157 CC 16 SCAR 28.7
CC
16
Cov
0%
remove_from_additional_lookup
42.0
get_by_additional_lookup
30.0
put
12.0
CRAP 17.7
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 =head1 CONTACT
21  
22 Please email comments or questions to the public Ensembl
23 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24  
25 Questions may also be sent to the Ensembl help desk at
26 <http://www.ensembl.org/Help/Contact>.
27  
28 =cut
29  
30 =head1 NAME
31  
32 Bio::EnsEMBL::DBSQL::Support::FullIdCache - ID based caching using all available values
33  
34 =head1 SYNOPSIS
35  
36 my $cache = Bio::EnsEMBL::DBSQL::Support::FullIdCache->new($adaptor);
37 my $obj = $cache->get(21);
38  
39 =head1 DESCRIPTION
40  
41 An implementation of caching which uses a raw hash to hold all available
42 values from an adaptor. Useful for working with a controlled vocabulary
43 table where cardinality is low.
44  
45 Provides extra functionality to compute additional lookup keys.
46  
47 =head1 METHODS
48  
49 =cut
50  
51 package Bio::EnsEMBL::DBSQL::Support::FullIdCache;
52 $Bio::EnsEMBL::DBSQL::Support::FullIdCache::VERSION = '114.0.0_50'; # TRIAL
53 $Bio::EnsEMBL::DBSQL::Support::FullIdCache::VERSION = '114.0.050';
54 use strict;
55 use warnings;
56 use base qw/Bio::EnsEMBL::DBSQL::Support::BaseCache/;
57 use Bio::EnsEMBL::Utils::Exception qw/throw/;
58  
59 =head2 build_cache
60  
61 Description: Builds a cache keyed by dbID and populated from a call
62 using C<generic_fetch()>
63 Returntype : Hash
64 Exceptions : None
65 Caller : BaseAdaptors
66 Status : Beta
67  
68 =cut
69  
70 sub build_cache {
71 my ($self) = @_;
72 my $adaptor = $self->adaptor();
73 my %cache;
74 my $objects = $adaptor->generic_fetch();
75 foreach my $object (@{$objects}) {
76 my $key = $object->dbID();
77 $cache{$key} = $object;
78 #Add to additional lookup
79 $self->add_to_additional_lookups($key, $object);
80 }
81 return \%cache;
82 }
83  
84 ########### Additional lookup code
85  
86 sub put {
87 my ($self, $key, $object) = @_;
88 my $old = $self->SUPER::put($key, $object);
89 #Add to additional lookup
90 $self->remove_from_additional_lookup($key, $old) if $old;
91 $self->add_to_additional_lookups($key, $object);
92 return $old if $old;
93 return;
94 }
95  
96 sub remove {
97 my ($self, $key) = @_;
98 my $old = $self->SUPER::remove($key);
99 if($old) {
100 #Remove it from the additional lookup
101 $self->remove_from_additional_lookup($key, $old);
102 return $old;
103 }
104 return;
105 }
106  
107 sub clear_cache {
108 my ($self) = @_;
109 $self->delete_cache();
110 #Remove the additional lookup hash contents
111 delete $self->{_additional_lookup};
112 return;
113 }
114  
115 =head2 get_by_additional_lookup
116  
117 Arg [1] : String key of the lookup to search for the value in
118 Arg [2] : String value to search for. We expect exact lookups in the hash
119 Description : Returns the object linked to the value in the specified lookup.
120 Example : my $analysis = $cache->get_by_additional_lookup('logic_name', 'xrefchecksum');
121 Returntype : Object a single object
122 Exceptions : Throws an exception if there are more than one ID linked to the
123 value lookup. Also thrown if additional lookups are not supported
124 Caller : BaseAdaptors
125 Status : Beta
126  
127 =cut
128  
129 sub get_by_additional_lookup {
130 my ($self, $key, $value) = @_;
131 $self->cache(); # trigger cache building
132 my $additional_lookup = $self->_additional_lookup();
133 if(exists $additional_lookup->{$key}) {
134 if(exists $additional_lookup->{$key}->{$value}) {
135 my $ids = $additional_lookup->{$key}->{$value};
136 my $size = scalar(@{$ids});
137 if($size > 1) {
138 throw "The lookup $key and search value $value has more than one value attached. Use get_all_by_additional_lookup() instead to fetch";
139 }
140 elsif($size == 1) {
141 return $self->get($ids->[0]);
142 }
143 }
144 }
145 return;
146 }
147  
148 =head2 get_all_by_additional_lookup
149  
150 Arg [1] : String key of the lookup to search for the value in
151 Arg [2] : String value to search for. We expect exact lookups in the hash
152 Description : Returns an array of all the objects linked to the value
153 in the specified lookup.
154 Example : my $array = $cache->get_all_by_additional_lookup('logic_name', 'xrefchecksum');
155 Returntype : ArrayRef of objects keyed agains the second argument
156 Exceptions : Throws an exception if there are more than one ID linked to the
157 value lookup. Also thrown if additional lookups are not supported
158 Caller : BaseAdaptors
159 Status : Beta
160  
161 =cut
162  
163 sub get_all_by_additional_lookup {
164 my ($self, $key, $value) = @_;
165 $self->cache(); # trigger cache building
166 my $additional_lookup = $self->_additional_lookup();
167 if(exists $additional_lookup->{$key}) {
168 if(exists $additional_lookup->{$key}->{$value}) {
169 my $ids = $additional_lookup->{$key}->{$value};
170 return $self->get_by_list($ids);
171 }
172 }
173 return [];
174 }
175  
176 =head2 remove_from_additional_lookup
177  
178 Arg [1] : String The lookup key to remove from the additional lookup hash
179 Arg [2] : Object The object to remove from the additional lookup hash
180 Description : Re-computes the additional keys for this object
181 Example : $cache->remove_Object_from_additional_lookup($lookup_key, $object);
182 Returntype : None
183 Exceptions : Thrown if we do not support additional lookups
184 Caller : BaseAdaptors
185 Status : Beta
186  
187 =cut
188  
189 sub remove_from_additional_lookup {
190 my ($self, $lookup_key, $object) = @_;
191  
192 # Compute the keys
193 my $keys = $self->compute_keys($object);
194 return if scalar(keys %{$keys}) == 0;
195  
196 my $additional_lookup = $self->_additional_lookup();
197  
198 foreach my $key (keys %{$keys}) {
199 my $value = $keys->{$key};
200  
201 #Only remove if we had originally stored this as an
202 #additional lookup
203 if(exists $additional_lookup->{$key}) {
204 if(exists $additional_lookup->{$key}->{$value}) {
205  
206 #Get the object ID & lookup the array of DBIDs
207 my $lookup_keys = $additional_lookup->{$key}->{$value};
208 my $length = scalar(@{$lookup_keys});
209 for(my $i = 0; $i < $length; $i++) {
210 if($lookup_keys->[$i] == $lookup_key) {
211 #remove the 1 lookup key from the array and then terminate the
212 #loop as we found our value
213 splice(@{$lookup_keys}, $i, 1);
214 last;
215 }
216 }
217  
218 #If the size has hit 0 then delete the array
219 if(scalar(@{$lookup_keys}) == 0) {
220 delete $additional_lookup->{$key}->{$value};
221 }
222 }
223 }
224 }
225  
226 return;
227 }
228  
229 =head2 compute_keys
230  
231 Arg [1] : Object The object to compute keys from
232 Description : Override to provide support for additional key lookup. The
233 keys of the hash should represent the lookup name and the
234 value is the computed key.
235 Example : Example of returning hash not of its usage. Proposed Analysis encoding
236 { logic_name => 'xrefalignment', display_label => 'Xref Alignment'}
237 Returntype : HashRef key is the lookup name and value is the computed key
238 Exceptions : none
239 Caller : BaseAdaptors
240 Status : Beta
241  
242 =cut
243  
244 sub compute_keys {
245 my ($self, $object) = @_;
246 return {};
247 }
248  
249 =head2 add_to_additional_lookups
250  
251 Arg [1] : String The key used in the primary lookup hash. Normally
252 a DB identifier
253 Arg [2] : Object The object to add to the additional lookups
254 Description : Internally calls the C<compute_keys()> method and adds
255 the object to the C<_additional_lookup()> hash.
256 Returntype : None
257 Exceptions : Thrown if additional lookups are not supported
258 Caller : BaseAdaptors
259 Status : Beta
260  
261 =cut
262  
263 sub add_to_additional_lookups {
264 my ($self, $lookup_key, $object) = @_;
265 my $keys = $self->compute_keys($object);
266 return if scalar(keys %{$keys}) == 0;
267 my $additional_lookup = $self->_additional_lookup();
268 foreach my $key (keys %{$keys}) {
269 my $value = $keys->{$key};
270 push(@{$additional_lookup->{$key}->{$value}}, $lookup_key);
271 }
272 return;
273 }
274  
275 =head2 _additional_lookup
276  
277 Description : Returns the additional lookup hash
278 Example : Example of additional hash structure (key is
279 lookup name, second key is value to search for
280 and value is an array of dbIDs)
281 {
282 logic_name => {
283 xrefalignment => [1]
284 },
285 display_label => {
286 'Xref Alignment' => [1]
287 }
288 }
289 Returntype : HashRef
290 Exceptions : none
291 Caller : BaseAdaptors
292 Status : Beta
293  
294 =cut
295  
296 sub _additional_lookup {
297 my ($self) = @_;
298 $self->{_additional_lookup} ||= {};
299 return $self->{_additional_lookup};
300 }
301  
302 1;