| 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_50'; # TRIAL | ||
| 83 | $Bio::EnsEMBL::IdMapping::Serialisable::VERSION = '114.0.050'; | ||
| 84 | use strict; | ||
| 85 | use warnings; | ||
| 86 | no warnings 'uninitialized'; | ||
| 87 | |||
| 88 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 89 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 90 | use Bio::EnsEMBL::Utils::ScriptUtils qw(parse_bytes); | ||
| 91 | use Storable qw(nstore retrieve); | ||
| 92 | |||
| 93 | |||
| 94 | =head2 new | ||
| 95 | |||
| 96 | Arg [DUMP_PATH] : String - path for object serialisation | ||
| 97 | Arg [CACHE_FILE] : String - filename of serialised object | ||
| 98 | Arg [AUTO_LOAD] : Boolean - determines whether object should be automatically | ||
| 99 | loaded on instantiation | ||
| 100 | Description : Constructor. | ||
| 101 | Return type : Bio::EnsEMBL::IdMapping::Serialisable implementing object | ||
| 102 | Exceptions : thrown on missing argument | ||
| 103 | Caller : implementing subclass | ||
| 104 | Status : At Risk | ||
| 105 | : under development | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub new { | ||
| 110 | my $caller = shift; | ||
| 111 | my $class = ref($caller) || $caller; | ||
| 112 | |||
| 113 | my ($dump_path, $cache_file, $auto_load) = | ||
| 114 | rearrange([qw(DUMP_PATH CACHE_FILE AUTO_LOAD)], @_); | ||
| 115 | |||
| 116 | throw("You must provide a cache file name") unless ($cache_file); | ||
| 117 | |||
| 118 | my $self = {}; | ||
| 119 | bless ($self, $class); | ||
| 120 | |||
| 121 | # initialise internal datastructure | ||
| 122 | $self->{'dump_path'} = $dump_path || '.'; | ||
| 123 | $self->{'cache_file_name'} = $cache_file; | ||
| 124 | |||
| 125 | # automatically load serialised object from file if requested | ||
| 126 | if ($auto_load) { | ||
| 127 | if (-s $self->cache_file) { | ||
| 128 | $self->read_from_file; | ||
| 129 | $self->{'loaded'} = 1; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head2 write_to_file | ||
| 138 | |||
| 139 | Example : my $filesize = $object->write_to_file; | ||
| 140 | Description : Serialises an object to a file (determined by | ||
| 141 | $self->cache_file). | ||
| 142 | Return type : String - size of serialisation file | ||
| 143 | Exceptions : thrown on I/O errors | ||
| 144 | Caller : general | ||
| 145 | Status : At Risk | ||
| 146 | : under development | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub write_to_file { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | # create dump directory if it doesn't exist | ||
| 154 | if (my $dump_path = $self->dump_path) { | ||
| 155 | unless (-d $dump_path) { | ||
| 156 | system("mkdir -p $dump_path") == 0 or | ||
| 157 | throw("Unable to create directory $dump_path.\n"); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | my $cache_file = $self->cache_file; | ||
| 162 | |||
| 163 | eval { nstore($self->{'cache'}, $cache_file) }; | ||
| 164 | if ($@) { | ||
| 165 | throw("Unable to store $cache_file: $@\n"); | ||
| 166 | } | ||
| 167 | |||
| 168 | my $size = -s $cache_file; | ||
| 169 | return parse_bytes($size); | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | =head2 read_from_file | ||
| 174 | |||
| 175 | Example : $object->read_from_file; | ||
| 176 | Description : Reads a serialised object from file (determined by | ||
| 177 | $self->cache_file). | ||
| 178 | Return type : Bio::EnsEMBL::IdMapping::Serialisable implementing object | ||
| 179 | Exceptions : thrown on I/O errors | ||
| 180 | Caller : general | ||
| 181 | Status : At Risk | ||
| 182 | : under development | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub read_from_file { | ||
| 187 | my $self = shift; | ||
| 188 | |||
| 189 | my $cache_file = $self->cache_file; | ||
| 190 | |||
| 191 | unless (-s $cache_file) { | ||
| 192 | throw("No valid cache file found at $cache_file."); | ||
| 193 | } | ||
| 194 | |||
| 195 | eval { $self->{'cache'} = retrieve($cache_file); }; | ||
| 196 | if ($@) { | ||
| 197 | throw("Unable to retrieve cache: $@"); | ||
| 198 | } | ||
| 199 | |||
| 200 | return $self; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | =head2 dump_path | ||
| 205 | |||
| 206 | Arg[1] : String - dump path for serialisation | ||
| 207 | Example : $object->dump_path('/tmp'); | ||
| 208 | Description : Getter/setter for the dump path for serialisation. | ||
| 209 | Return type : String | ||
| 210 | Exceptions : none | ||
| 211 | Caller : general | ||
| 212 | Status : At Risk | ||
| 213 | : under development | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub dump_path { | ||
| 218 | my $self = shift; | ||
| 219 | $self->{'dump_path'} = shift if (@_); | ||
| 220 | return $self->{'dump_path'}; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | =head2 cache_file_name | ||
| 225 | |||
| 226 | Arg[1] : String - file name for serialisation | ||
| 227 | Example : $object->cache_file_name('object_cache.ser'); | ||
| 228 | Description : Getter/setter for the file name for serialisation. | ||
| 229 | Return type : String | ||
| 230 | Exceptions : none | ||
| 231 | Caller : general | ||
| 232 | Status : At Risk | ||
| 233 | : under development | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub cache_file_name { | ||
| 238 | my $self = shift; | ||
| 239 | $self->{'cache_file_name'} = shift if (@_); | ||
| 240 | return $self->{'cache_file_name'}; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | =head2 cache_file | ||
| 245 | |||
| 246 | Example : my $cache_file = $object->cache_file; | ||
| 247 | Description : Returns the path and name of the serialised object file. | ||
| 248 | Return type : String | ||
| 249 | Exceptions : none | ||
| 250 | Caller : general | ||
| 251 | Status : At Risk | ||
| 252 | : under development | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub cache_file { | ||
| 257 | my $self = shift; | ||
| 258 | return $self->dump_path.'/'.$self->cache_file_name; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | =head2 loaded | ||
| 263 | |||
| 264 | Arg[1] : Boolean - "loaded" status | ||
| 265 | Example : if ($object->loaded) { | ||
| 266 | # do something with the object that was loaded from a file | ||
| 267 | } else { | ||
| 268 | # the object wasn't loaded but is new, so fill it | ||
| 269 | } | ||
| 270 | Description : Indicates whether a given object was loaded from its serialised | ||
| 271 | state on disk. | ||
| 272 | Return type : Boolean - TRUE if loaded from disk, FALSE otherwise | ||
| 273 | Exceptions : none | ||
| 274 | Caller : general | ||
| 275 | Status : At Risk | ||
| 276 | : under development | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub loaded { | ||
| 281 | my $self = shift; | ||
| 282 | $self->{'loaded'} = shift if (@_); | ||
| 283 | return $self->{'loaded'}; | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | 1; | ||
| 288 |