3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
22 Please email comments or questions to the
public Ensembl
23 developers list at <http:
25 Questions may also be sent to the Ensembl help desk at
37 my $obj = $cache->
get(21);
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.
45 Provides extra functionality to compute additional lookup keys.
51 package Bio::EnsEMBL::DBSQL::Support::FullIdCache;
60 Description: Builds a cache keyed by dbID and populated from a call
61 using C<generic_fetch()>
71 my $adaptor = $self->adaptor();
73 my $objects = $adaptor->generic_fetch();
74 foreach my $object (@{$objects}) {
75 my $key = $object->dbID();
76 $cache{$key} = $object;
77 #Add to additional lookup
78 $self->add_to_additional_lookups($key, $object);
83 ########### Additional lookup code
86 my ($self, $key, $object) = @_;
87 my $old = $self->SUPER::put($key, $object);
88 #Add to additional lookup
89 $self->remove_from_additional_lookup($key, $old)
if $old;
90 $self->add_to_additional_lookups($key, $object);
96 my ($self, $key) = @_;
97 my $old = $self->SUPER::remove($key);
99 #Remove it from the additional lookup
100 $self->remove_from_additional_lookup($key, $old);
108 $self->delete_cache();
109 #Remove the additional lookup hash contents
110 delete $self->{_additional_lookup};
114 =head2 get_by_additional_lookup
116 Arg [1] : String key of the lookup to search
for the value in
117 Arg [2] : String value to search
for. We expect exact lookups in the hash
118 Description : Returns the
object linked to the value in the specified lookup.
119 Example : my $analysis = $cache->get_by_additional_lookup(
'logic_name',
'xrefchecksum');
120 Returntype : Object a single
object
121 Exceptions : Throws an exception
if there are more than one ID linked to the
122 value lookup. Also thrown
if additional lookups are not supported
123 Caller : BaseAdaptors
128 sub get_by_additional_lookup {
129 my ($self, $key, $value) = @_;
130 $self->cache(); # trigger cache building
131 my $additional_lookup = $self->_additional_lookup();
132 if(exists $additional_lookup->{$key}) {
133 if(exists $additional_lookup->{$key}->{$value}) {
134 my $ids = $additional_lookup->{$key}->{$value};
135 my $size = scalar(@{$ids});
137 throw "The lookup $key and search value $value has more than one value attached. Use get_all_by_additional_lookup() instead to fetch";
140 return $self->get($ids->[0]);
147 =head2 get_all_by_additional_lookup
149 Arg [1] : String key of the lookup to search
for the value in
150 Arg [2] : String value to search
for. We expect exact lookups in the hash
151 Description : Returns an array of all the objects linked to the value
152 in the specified lookup.
153 Example : my $array = $cache->get_all_by_additional_lookup(
'logic_name',
'xrefchecksum');
154 Returntype : ArrayRef of objects keyed agains the second argument
155 Exceptions : Throws an exception
if there are more than one ID linked to the
156 value lookup. Also thrown
if additional lookups are not supported
157 Caller : BaseAdaptors
162 sub get_all_by_additional_lookup {
163 my ($self, $key, $value) = @_;
164 $self->cache(); # trigger cache building
165 my $additional_lookup = $self->_additional_lookup();
166 if(exists $additional_lookup->{$key}) {
167 if(exists $additional_lookup->{$key}->{$value}) {
168 my $ids = $additional_lookup->{$key}->{$value};
169 return $self->get_by_list($ids);
175 =head2 remove_from_additional_lookup
177 Arg [1] : String The lookup key to remove from the additional lookup hash
178 Arg [2] : Object The
object to remove from the additional lookup hash
179 Description : Re-computes the additional keys
for this object
180 Example : $cache->remove_Object_from_additional_lookup($lookup_key, $object);
182 Exceptions : Thrown
if we
do not support additional lookups
183 Caller : BaseAdaptors
188 sub remove_from_additional_lookup {
189 my ($self, $lookup_key, $object) = @_;
192 my $keys = $self->compute_keys($object);
193 return if scalar(keys %{$keys}) == 0;
195 my $additional_lookup = $self->_additional_lookup();
197 foreach my $key (keys %{$keys}) {
198 my $value = $keys->{$key};
200 #Only remove if we had originally stored this as an
202 if(exists $additional_lookup->{$key}) {
203 if(exists $additional_lookup->{$key}->{$value}) {
205 #Get the object ID & lookup the array of DBIDs
206 my $lookup_keys = $additional_lookup->{$key}->{$value};
207 my $length = scalar(@{$lookup_keys});
208 for(my $i = 0; $i < $length; $i++) {
209 if($lookup_keys->[$i] == $lookup_key) {
210 #remove the 1 lookup key from the array and then terminate the
211 #loop as we found our value
212 splice(@{$lookup_keys}, $i, 1);
217 #If the size has hit 0 then delete the array
218 if(scalar(@{$lookup_keys}) == 0) {
219 delete $additional_lookup->{$key}->{$value};
230 Arg [1] : Object The
object to compute keys from
231 Description : Override to provide support
for additional key lookup. The
232 keys of the hash should represent the lookup name and the
233 value is the computed key.
234 Example : Example of returning hash not of its
usage. Proposed Analysis encoding
235 { logic_name =>
'xrefalignment', display_label =>
'Xref Alignment'}
236 Returntype : HashRef key is the lookup name and value is the computed key
238 Caller : BaseAdaptors
244 my ($self, $object) = @_;
248 =head2 add_to_additional_lookups
250 Arg [1] : String The key used in the primary lookup hash. Normally
252 Arg [2] : Object The
object to add to the additional lookups
253 Description : Internally calls the C<compute_keys()> method and adds
254 the
object to the C<_additional_lookup()> hash.
256 Exceptions : Thrown
if additional lookups are not supported
257 Caller : BaseAdaptors
262 sub add_to_additional_lookups {
263 my ($self, $lookup_key, $object) = @_;
264 my $keys = $self->compute_keys($object);
265 return if scalar(keys %{$keys}) == 0;
266 my $additional_lookup = $self->_additional_lookup();
267 foreach my $key (keys %{$keys}) {
268 my $value = $keys->{$key};
269 push(@{$additional_lookup->{$key}->{$value}}, $lookup_key);
274 =head2 _additional_lookup
276 Description : Returns the additional lookup hash
277 Example : Example of additional hash structure (key is
278 lookup name, second key is value to search
for
279 and value is an array of dbIDs)
285 'Xref Alignment' => [1]
290 Caller : BaseAdaptors
295 sub _additional_lookup {
297 $self->{_additional_lookup} ||= {};
298 return $self->{_additional_lookup};