1 package Bio
::Root
::Storable
;
4 use Data
::Dumper
qw( Dumper );
6 use base
qw(Bio::Root::Root);
10 my $storable = Bio::Root::Storable->new();
12 # Store/retrieve using class retriever
13 my $token = $storable->store();
14 my $storable2 = Bio::Root::Storable->retrieve( $token );
16 # Store/retrieve using object retriever
17 my $storable2 = $storable->new_retrievable();
18 $storable2->retrieve();
22 Generic module that allows objects to be safely stored/retrieved from
23 disk. Can be inhereted by any BioPerl object. As it will not usually
24 be the first class in the inheretence list, _initialise_storable()
25 should be called during object instantiation.
27 Object storage is recursive; If the object being stored contains other
28 storable objects, these will be stored separately, and replaced by a
29 skeleton object in the parent hierarchy. When the parent is later
30 retrieved, its children remain in the skeleton state until explicitly
31 retrieved by the parent. This lazy-retrieve approach has obvious
32 memory efficiency benefits for certain applications.
35 By default, objects are stored in binary format (using the Perl
36 Storable module). Earlier versions of Perl5 do not include Storable as
37 a core module. If this is the case, ASCII object storage (using the
38 Perl Data::Dumper module) is used instead.
40 ASCII storage can be enabled by default by setting the value of
41 $Bio::Root::Storable::BINARY to false.
43 =head1 AUTHOR Will Spooner
47 use vars
qw( $BINARY );
50 if( eval "require Storable" ){
51 Storable->import( 'freeze', 'thaw' );
56 #----------------------------------------------------------------------
60 Arg [1] : -workdir => filesystem path,
61 -template => tmpfile template,
62 -suffix => tmpfile suffix,
63 Function : Builds a new Bio::Root::Storable inhereting object
64 Returntype: Bio::Root::Storable inhereting object
67 Example : $storable = Bio::Root::Storable->new()
72 my ($caller, @args) = @_;
73 my $self = $caller->SUPER::new(@args);
74 $self->_initialise_storable;
78 #----------------------------------------------------------------------
80 =head2 _initialise_storable
82 Arg [1] : See 'new' method
83 Function : Initialises storable-specific attributes
91 sub _initialise_storable {
93 my( $workdir, $template, $suffix ) =
94 $self->_rearrange([qw(WORKDIR TEMPLATE SUFFIX)], @_ );
95 $workdir && $self->workdir ( $workdir );
96 $template && $self->template( $template );
97 $suffix && $self->suffix ( $suffix );
103 #----------------------------------------------------------------------
107 Arg [1] : string (optional)
108 Function : Accessor for the file to write state into.
109 Should not normally use as a setter - let Root::IO
113 Caller : Bio::Root::Storable->store
114 Example : my $statefile = $obj->statefile();
119 my $key = '_statefile';
122 if( @_ ){ $self->{$key} = shift }
124 if( ! $self->{$key} ){ # Create a new statefile
125 my $workdir = $self->workdir;
126 my $template = $self->template;
127 my $suffix = $self->suffix;
129 # TODO: add cleanup and unlink methods. For now, we'll keep the
130 # statefile hanging around.
131 my @args = ( CLEANUP
=>0, UNLINK
=>0 );
132 if( $template ){ push( @args, 'TEMPLATE' => $template )};
133 if( $workdir ){ push( @args, 'DIR' => $workdir )};
134 if( $suffix ){ push( @args, 'SUFFIX' => $suffix )};
135 my( $fh, $file ) = Bio
::Root
::IO
->new->tempfile( @args );
136 # If filehandle is not stored, don't leave it open
139 $self->{$key} = $file;
142 return $self->{$key};
145 #----------------------------------------------------------------------
149 Arg [1] : string (optional) (TODO - convert to array for x-platform)
150 Function : Accessor for the statefile directory. Defaults to File::Spec->tmpdir
154 Example : $obj->workdir('/tmp/foo');
159 my $key = '_workdir';
162 my $caller = join( ', ', (caller(0))[1..2] );
163 $self->{$key} && $self->debug("Overwriting workdir: probably bad!");
164 $self->{$key} = shift
166 #$self->{$key} ||= $Bio::Root::IO::TEMPDIR;
167 $self->{$key} ||= File
::Spec
->tmpdir();
168 return $self->{$key};
171 #----------------------------------------------------------------------
175 Arg [1] : string (optional)
176 Function : Accessor for the statefile template. Defaults to XXXXXXXX
180 Example : $obj->workdir('RES_XXXXXXXX');
185 my $key = '_template';
187 if( @_ ){ $self->{$key} = shift }
188 $self->{$key} ||= 'XXXXXXXX';
189 return $self->{$key};
192 #----------------------------------------------------------------------
196 Arg [1] : string (optional)
197 Function : Accessor for the statefile template.
201 Example : $obj->suffix('.state');
208 if( @_ ){ $self->{$key} = shift }
209 return $self->{$key};
212 #----------------------------------------------------------------------
214 =head2 new_retrievable
216 Arg [1] : Same as for 'new'
217 Function : Similar to store, except returns a 'skeleton' of the calling
218 object, rather than the statefile.
219 The skeleton can be repopulated by calling 'retrieve'. This
220 will be a clone of the original object.
221 Returntype: Bio::Root::Storable inhereting object
224 Example : my $skel = $obj->new_retrievable(); # skeleton
225 $skel->retrieve(); # clone
233 $self->_initialise_storable( @args );
235 if( $self->retrievable ){ return $self->clone } # Clone retrievable
236 return bless( { _statefile
=> $self->store(@args),
237 _workdir
=> $self->workdir,
238 _suffix
=> $self->suffix,
239 _template
=> $self->template,
244 #----------------------------------------------------------------------
249 Function : Reports whether the object is in 'skeleton' state, and the
250 'retrieve' method can be called.
254 Example : if( $obj->retrievable ){ $obj->retrieve }
260 if( @_ ){ $self->{_retrievable
} = shift }
261 return $self->{_retrievable
};
264 #----------------------------------------------------------------------
269 Function : Accessor for token attribute
270 Returntype: string. Whatever retrieve needs to retrieve.
271 This base implementation returns the statefile
274 Example : my $token = $obj->token();
280 return $self->statefile;
284 #----------------------------------------------------------------------
289 Function : Saves a serialised representation of the object structure
290 to disk. Returns the name of the file that the object was
296 Example : my $token = $obj->store();
302 my $statefile = $self->statefile;
303 my $store_obj = $self->serialise;
304 my $io = Bio
::Root
::IO
->new( ">$statefile" );
305 $io->_print( $store_obj );
306 $self->debug( "STORING $self to $statefile\n" );
307 # If filehandle is not stored, don't leave it open
312 #----------------------------------------------------------------------
317 Function : Prepares the the serialised representation of the object.
318 Object attribute names starting with '__' are skipped.
319 This is useful for those that do not serialise too well
321 Attributes are examined for other storable objects. If these
322 are found they are serialised separately using 'new_retrievable'
326 Example : my $serialised = $obj->serialise();
333 # Create a new object of same class that is going to be serialised
334 my $store_obj = bless( {}, ref( $self ) );
336 my %retargs = ( -workdir
=>$self->workdir,
337 -suffix
=>$self->suffix,
338 -template
=>$self->template );
339 # Assume that other storable bio objects held by this object are
342 foreach my $key( keys( %$self ) ){
343 if( $key =~ /^__/ ){ next } # Ignore keys starting with '__'
344 my $value = $self->{$key};
347 if( ! ref( $value ) ){
348 $store_obj->{$key} = $value;
351 # Bio::Root::Storable obj: save placeholder
352 elsif( ref($value) =~ /^Bio::/ and $value->isa('Bio::Root::Storable') ){
353 # Bio::Root::Storable
354 $store_obj->{$key} = $value->new_retrievable( %retargs );
358 # Arrayref value. Look for Bio::Root::Storable objs
359 elsif( ref( $value ) eq 'ARRAY' ){
361 foreach my $val( @
$value ){
362 if( ref($val) =~ /^Bio::/ and $val->isa('Bio::Root::Storable') ){
363 push( @ary, $val->new_retrievable( %retargs ) );
365 else{ push( @ary, $val ) }
367 $store_obj->{$key} = \
@ary;
370 # Hashref value. Look for Bio::Root::Storable objs
371 elsif( ref( $value ) eq 'HASH' ){
373 foreach my $k2( keys %$value ){
374 my $val = $value->{$k2};
375 if( ref($val) =~ /^Bio::/ and $val->isa('Bio::Root::Storable') ){
376 $hash{$k2} = $val->new_retrievable( %retargs );
378 else{ $hash{$k2} = $val }
380 $store_obj->{$key} = \
%hash;
383 # Unknown, just add to the store object regardless
384 else{ $store_obj->{$key} = $value }
386 $store_obj->retrievable(0); # Once deserialised, obj not retrievable
387 return $self->_freeze( $store_obj );
391 #----------------------------------------------------------------------
395 Arg [1] : string; filesystem location of the state file to be retrieved
396 Function : Retrieves a stored object from disk.
397 Note that the retrieved object will be blessed into its original
399 Returntype: Bio::Root::Storable inhereting object
402 Example : my $obj = Bio::Root::Storable->retrieve( $token );
407 my( $caller, $statefile ) = @_;
410 my $class = ref( $caller ) || $caller;
412 # Is this a call on a retrievable object?
414 and $caller->retrievable
417 $statefile = $self->statefile;
419 bless( $self, $class );
421 # Recover serialised object
422 if( ! -f
$statefile ){
423 $self->throw( "Token $statefile is not found" );
425 my $io = Bio
::Root
::IO
->new( $statefile );
427 my $state_str = $io->_readline('-raw'=>1);
428 # If filehandle is not stored, don't leave it open
431 # Dynamic-load modules required by stored object
434 for( my $i=0; $i<10; $i++ ){
435 eval{ $stored_obj = $self->_thaw( $state_str ) };
441 if( $@
=~ /Cannot restore overloading(.*)/i ){
442 my $postmatch = $1; #'
443 if( $postmatch =~ /\(package +([\w\:]+)\)/ ) {
448 eval "require $package";
449 $self->throw($@
) if $@
;
451 else{ $self->throw($@
) }
453 if( ! $success ){ $self->throw("maximum number of requires exceeded" ) }
455 if( ! ref( $stored_obj ) ){
456 $self->throw( "Token $statefile returned no data" );
458 map{ $self->{$_} = $stored_obj->{$_} } keys %$stored_obj; # Copy hasheys
459 $self->retrievable(0);
461 # Maintain class of stored obj
465 #----------------------------------------------------------------------
471 Function : Returns a clone of the calling object
472 Returntype: Bio::Root::Storable inhereting object
475 Example : my $clone = $obj->clone();
481 my $frozen = $self->_freeze( $self );
482 return $self->_thaw( $frozen );
487 #----------------------------------------------------------------------
492 Function : Clears the stored object from disk
496 Example : $obj->remove();
502 if( -e
$self->statefile ){
503 unlink( $self->statefile );
508 #----------------------------------------------------------------------
513 Function : Converts whatever is in the the arg into a string.
514 Uses either Storable::freeze or Data::Dumper::Dump
515 depending on the value of $Bio::Root::BINARY
527 return freeze
( $data );
530 $Data::Dumper
::Purity
= 1;
531 return Data
::Dumper
->Dump( [\
$data],["*code"] );
535 #----------------------------------------------------------------------
540 Function : Converts the string into a perl 'whatever'.
541 Uses either Storable::thaw or eval depending on the
542 value of $Bio::Root::BINARY.
543 Note; the string arg should have been created with
544 the _freeze method, or strange things may occur!
560 $code = eval( $data ) ;
562 $self->throw( "eval: $@" );
564 ref( $code ) eq 'REF'
565 or $self->throw( "Serialised string was not a scalar ref" );