EnsEMBL/IdMapping/Serialisable.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 125 CC 12 SCAR 22.9
CC
12
Cov
0%
new
20.0
write_to_file
20.0
read_from_file
12.0
CRAP 9.9
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::IdMapping::Serialisable - base class for serialisable objects
34  
35 =head1 SYNOPSIS
36  
37 # instantiate an object which extends Serialisable
38 my $object = YourObject->new(
39 -DUMP_PATH => '/tmp',
40 -CACHE_FILE => 'object_cache.ser',
41 );
42  
43 # serialise object to file
44 my $filesize = $object->write_to_file;
45 print LOG "Serialised object to file of size $filesize.\n";
46  
47 # later, create another object defining the same serialisation
48 # location. specifying -LOAD_AUTO will automatically load it from the
49 # serialisation file.
50 my $object1 = YourObject->new(
51 -DUMP_PATH => '/tmp',
52 -CACHE_FILE => 'object_cache.ser',
53 -LOAD_AUTO => 1,
54 );
55  
56 # alternatively, manually load the object from file
57 $object1->load_from_file;
58  
59 =head1 DESCRIPTION
60  
61 This is the base class for serialisable objects used by the
62 stable Id mapping. It's essentially an OO wrapper for Storable,
63 providing a method to store (write_to_file(()) and one to retrieve
64 (read_from_file()) serialised objects.
65  
66 This class is not instantiated itself, but rather extended by
67 implementing classes.
68  
69 =head1 METHODS
70  
71 new
72 write_to_file
73 read_from_file
74 dump_path
75 cache_file_name
76 cache_file
77 loaded
78  
79 =cut
80  
81 package Bio::EnsEMBL::IdMapping::Serialisable;
82 $Bio::EnsEMBL::IdMapping::Serialisable::VERSION = '114.0.0';
83 use strict;
84 use warnings;
85 no warnings 'uninitialized';
86  
87 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
88 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
89 use Bio::EnsEMBL::Utils::ScriptUtils qw(parse_bytes);
90 use Storable qw(nstore retrieve);
91  
92  
93 =head2 new
94  
95 Arg [DUMP_PATH] : String - path for object serialisation
96 Arg [CACHE_FILE] : String - filename of serialised object
97 Arg [AUTO_LOAD] : Boolean - determines whether object should be automatically
98 loaded on instantiation
99 Description : Constructor.
100 Return type : Bio::EnsEMBL::IdMapping::Serialisable implementing object
101 Exceptions : thrown on missing argument
102 Caller : implementing subclass
103 Status : At Risk
104 : under development
105  
106 =cut
107  
108 sub new {
109 my $caller = shift;
110 my $class = ref($caller) || $caller;
111  
112 my ($dump_path, $cache_file, $auto_load) =
113 rearrange([qw(DUMP_PATH CACHE_FILE AUTO_LOAD)], @_);
114  
115 throw("You must provide a cache file name") unless ($cache_file);
116
117 my $self = {};
118 bless ($self, $class);
119  
120 # initialise internal datastructure
121 $self->{'dump_path'} = $dump_path || '.';
122 $self->{'cache_file_name'} = $cache_file;
123  
124 # automatically load serialised object from file if requested
125 if ($auto_load) {
126 if (-s $self->cache_file) {
127 $self->read_from_file;
128 $self->{'loaded'} = 1;
129 }
130 }
131  
132 return $self;
133 }
134  
135  
136 =head2 write_to_file
137  
138 Example : my $filesize = $object->write_to_file;
139 Description : Serialises an object to a file (determined by
140 $self->cache_file).
141 Return type : String - size of serialisation file
142 Exceptions : thrown on I/O errors
143 Caller : general
144 Status : At Risk
145 : under development
146  
147 =cut
148  
149 sub write_to_file {
150 my $self = shift;
151  
152 # create dump directory if it doesn't exist
153 if (my $dump_path = $self->dump_path) {
154 unless (-d $dump_path) {
155 system("mkdir -p $dump_path") == 0 or
156 throw("Unable to create directory $dump_path.\n");
157 }
158 }
159
160 my $cache_file = $self->cache_file;
161  
162 eval { nstore($self->{'cache'}, $cache_file) };
163 if ($@) {
164 throw("Unable to store $cache_file: $@\n");
165 }
166  
167 my $size = -s $cache_file;
168 return parse_bytes($size);
169 }
170  
171  
172 =head2 read_from_file
173  
174 Example : $object->read_from_file;
175 Description : Reads a serialised object from file (determined by
176 $self->cache_file).
177 Return type : Bio::EnsEMBL::IdMapping::Serialisable implementing object
178 Exceptions : thrown on I/O errors
179 Caller : general
180 Status : At Risk
181 : under development
182  
183 =cut
184  
185 sub read_from_file {
186 my $self = shift;
187  
188 my $cache_file = $self->cache_file;
189  
190 unless (-s $cache_file) {
191 throw("No valid cache file found at $cache_file.");
192 }
193  
194 eval { $self->{'cache'} = retrieve($cache_file); };
195 if ($@) {
196 throw("Unable to retrieve cache: $@");
197 }
198  
199 return $self;
200 }
201  
202  
203 =head2 dump_path
204  
205 Arg[1] : String - dump path for serialisation
206 Example : $object->dump_path('/tmp');
207 Description : Getter/setter for the dump path for serialisation.
208 Return type : String
209 Exceptions : none
210 Caller : general
211 Status : At Risk
212 : under development
213  
214 =cut
215  
216 sub dump_path {
217 my $self = shift;
218 $self->{'dump_path'} = shift if (@_);
219 return $self->{'dump_path'};
220 }
221  
222  
223 =head2 cache_file_name
224  
225 Arg[1] : String - file name for serialisation
226 Example : $object->cache_file_name('object_cache.ser');
227 Description : Getter/setter for the file name for serialisation.
228 Return type : String
229 Exceptions : none
230 Caller : general
231 Status : At Risk
232 : under development
233  
234 =cut
235  
236 sub cache_file_name {
237 my $self = shift;
238 $self->{'cache_file_name'} = shift if (@_);
239 return $self->{'cache_file_name'};
240 }
241  
242  
243 =head2 cache_file
244  
245 Example : my $cache_file = $object->cache_file;
246 Description : Returns the path and name of the serialised object file.
247 Return type : String
248 Exceptions : none
249 Caller : general
250 Status : At Risk
251 : under development
252  
253 =cut
254  
255 sub cache_file {
256 my $self = shift;
257 return $self->dump_path.'/'.$self->cache_file_name;
258 }
259  
260  
261 =head2 loaded
262  
263 Arg[1] : Boolean - "loaded" status
264 Example : if ($object->loaded) {
265 # do something with the object that was loaded from a file
266 } else {
267 # the object wasn't loaded but is new, so fill it
268 }
269 Description : Indicates whether a given object was loaded from its serialised
270 state on disk.
271 Return type : Boolean - TRUE if loaded from disk, FALSE otherwise
272 Exceptions : none
273 Caller : general
274 Status : At Risk
275 : under development
276  
277 =cut
278  
279 sub loaded {
280 my $self = shift;
281 $self->{'loaded'} = shift if (@_);
282 return $self->{'loaded'};
283 }
284  
285  
286 1;
287