File Coverage

blib/lib/Class/Prevayler/SystemRecoverer.pm
Criterion Covered Total %
statement 26 66 39.3
branch 0 12 0.0
condition 0 3 0.0
subroutine 8 15 53.3
pod 0 2 0.0
total 34 98 34.6


line stmt bran cond sub pod time code
1              
2             package Class::Prevayler::SystemRecoverer;
3 1     1   605 use Class::Prevayler::CommandRecoverer;
  1         3  
  1         53  
4 1     1   9 use strict;
  1         2  
  1         31  
5 1     1   5 use warnings;
  1         3  
  1         30  
6 1     1   4 use Carp;
  1         1  
  1         60  
7              
8             BEGIN {
9 1     1   4 use Exporter ();
  1         2  
  1         24  
10 1     1   3 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         2  
  1         123  
11 1     1   2598 $VERSION = 0.02;
12 1         17 @ISA = qw (Exporter);
13              
14             #Give a hoot don't pollute, do not export more than needed by default
15 1         3 @EXPORT = qw ();
16 1         1 @EXPORT_OK = qw ();
17 1         622 %EXPORT_TAGS = ();
18              
19             use Class::MethodMaker
20 1         7 new_with_init => 'new',
21             new_hash_init => 'hash_init',
22 1     1   4 get_set => [ 'deserializer', 'directory', 'next_logfile_number', ];
  1         1  
23             }
24              
25             ########################################### main pod documentation begin ##
26             # Below is the stub of documentation for your module. You better edit it!
27              
28             =head1 NAME
29              
30             Class::Prevayler::SystemRecoverer - Prevayler implementation - www.prevayler.org
31              
32             =head1 DESCRIPTION
33              
34             this class is an internal part of the Class::Prevayler module.
35              
36             =head1 AUTHOR
37              
38             Nathanael Obermayer
39             CPAN ID: nathanael
40             natom-pause@smi2le.net
41             http://a.galaxy.far.far.away/modules
42              
43             =head1 COPYRIGHT
44              
45             This program is free software; you can redistribute
46             it and/or modify it under the same terms as Perl itself.
47              
48             The full text of the license can be found in the
49             LICENSE file included with this module.
50              
51              
52             =head1 SEE ALSO
53              
54             Class::Prevayler
55              
56             =cut
57              
58             sub init {
59 0     0 0   my $self = shift;
60 0           my %values = (@_);
61 0           $self->hash_init(%values);
62 0 0 0       ( $self->directory && $self->deserializer )
63             or croak "need a directory and a deserializer!";
64 0           return;
65             }
66              
67             sub _read_system {
68 0     0     my ( $self, $filename ) = @_;
69 0           my $filehandle;
70 0 0         open $filehandle, $filename
71             or croak "couldn't open $filename : $!";
72 0           my $file;
73 0           while (<$filehandle>) {
74 0           $file .= $_;
75             }
76 0 0         close $filehandle
77             or croak "couldn't close $filename : $!";
78              
79 0           return $self->deserializer()->($file);
80             }
81              
82             sub _find_last_snapshot_file {
83 0     0     my $self = shift;
84              
85 0           local (*DIRHANDLE);
86 0 0         opendir DIRHANDLE, $self->directory()
87             or croak "couldn't open directory " . $self->directory . ": $!";
88 0           my @snapshots = grep /\.snapshot$/, readdir DIRHANDLE;
89 0 0         return undef unless scalar @snapshots;
90 0           my @sorted_snapshots = sort @snapshots;
91              
92 0           return File::Spec->catfile( $self->directory, $sorted_snapshots[-1] );
93             }
94              
95             sub _recover_snapshot {
96 0     0     my ( $self, $virgin_system ) = @_;
97 0           my $snapshot_file = $self->_find_last_snapshot_file();
98 0           my $system;
99 0 0         if ($snapshot_file) {
100 0           $system = $self->_read_system($snapshot_file);
101 0           $self->next_logfile_number( $self->_number($snapshot_file) + 1 );
102             }
103             else {
104 0           $system = $virgin_system;
105 0           $self->next_logfile_number(1);
106             }
107 0           return $system;
108             }
109              
110             sub recover {
111 0     0 0   my ( $self, $virgin_system ) = @_;
112 0           my $system = $self->_recover_snapshot($virgin_system);
113 0           return $self->_recover_commands($system);
114             }
115              
116             sub _number {
117 0     0     my ( $self, $filename ) = @_;
118              
119 0           my ($number) = $filename =~ /(\d*)\.snapshot/;
120 0           return $number;
121             }
122              
123             sub _recover_commands {
124 0     0     my ( $self, $system ) = @_;
125              
126 0           my $cmd_recoverer = Class::Prevayler::CommandRecoverer->new(
127             directory => $self->directory,
128             deserializer => $self->deserializer,
129             next_logfile_number => $self->next_logfile_number,
130             );
131 0           my $updated_system = $cmd_recoverer->recover($system);
132 0           $self->next_logfile_number( $cmd_recoverer->next_logfile_number() );
133 0           return $updated_system;
134             }
135              
136             1; #this line is important and will help the module return a true value
137             __END__