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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
37 # instantiate an object which extends Serialisable
38 my $object = YourObject->
new(
40 -CACHE_FILE =>
'object_cache.ser',
43 # serialise object to file
45 print LOG
"Serialised object to file of size $filesize.\n";
47 # later, create another object defining the same serialisation
48 # location. specifying -LOAD_AUTO will automatically load it from the
50 my $object1 = YourObject->new(
52 -CACHE_FILE =>
'object_cache.ser',
56 # alternatively, manually load the object from file
57 $object1->load_from_file;
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.
66 This class is not instantiated itself, but rather extended by
81 package Bio::EnsEMBL::IdMapping::Serialisable;
85 no warnings
'uninitialized';
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.
101 Exceptions : thrown on missing argument
102 Caller : implementing subclass
110 my $class = ref($caller) || $caller;
112 my ($dump_path, $cache_file, $auto_load) =
113 rearrange([qw(DUMP_PATH CACHE_FILE AUTO_LOAD)], @_);
115 throw(
"You must provide a cache file name") unless ($cache_file);
118 bless ($self, $class);
120 # initialise internal datastructure
121 $self->{
'dump_path'} = $dump_path ||
'.';
122 $self->{
'cache_file_name'} = $cache_file;
124 # automatically load serialised object from file if requested
126 if (-s $self->cache_file) {
127 $self->read_from_file;
128 $self->{
'loaded'} = 1;
138 Example : my $filesize = $object->write_to_file;
139 Description : Serialises an
object to a file (determined by
141 Return type : String - size of serialisation file
142 Exceptions : thrown on I/O errors
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");
160 my $cache_file = $self->cache_file;
162 eval { nstore($self->{
'cache'}, $cache_file) };
164 throw(
"Unable to store $cache_file: $@\n");
167 my $size = -s $cache_file;
168 return parse_bytes($size);
172 =head2 read_from_file
174 Example : $object->read_from_file;
175 Description : Reads a serialised
object from file (determined by
178 Exceptions : thrown on I/O errors
190 unless (-s $cache_file) {
191 throw(
"No valid cache file found at $cache_file.");
194 eval { $self->{
'cache'} = retrieve($cache_file); };
196 throw(
"Unable to retrieve cache: $@");
205 Arg[1] : String - dump path
for serialisation
206 Example : $object->dump_path(
'/tmp');
207 Description : Getter/setter
for the dump path
for serialisation.
218 $self->{
'dump_path'} = shift
if (@_);
219 return $self->{
'dump_path'};
223 =head2 cache_file_name
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.
236 sub cache_file_name {
238 $self->{
'cache_file_name'} = shift
if (@_);
239 return $self->{
'cache_file_name'};
245 Example : my $cache_file = $object->cache_file;
246 Description : Returns the path and name of the serialised
object file.
257 return $self->dump_path.
'/'.$self->cache_file_name;
263 Arg[1] : Boolean -
"loaded" status
264 Example :
if ($object->loaded) {
265 # do something with the object that was loaded from a file
267 # the object wasn't loaded but is new, so fill it
269 Description : Indicates whether a given
object was loaded from its serialised
271 Return type : Boolean - TRUE
if loaded from disk, FALSE otherwise
281 $self->{
'loaded'} = shift
if (@_);
282 return $self->{
'loaded'};