| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Armadito::Agent::Storage; |
|
2
|
|
|
|
|
|
|
|
|
3
|
24
|
|
|
24
|
|
418605
|
use strict; |
|
|
24
|
|
|
|
|
38
|
|
|
|
24
|
|
|
|
|
568
|
|
|
4
|
24
|
|
|
24
|
|
90
|
use warnings; |
|
|
24
|
|
|
|
|
25
|
|
|
|
24
|
|
|
|
|
669
|
|
|
5
|
24
|
|
|
24
|
|
84
|
use Config; |
|
|
24
|
|
|
|
|
90
|
|
|
|
24
|
|
|
|
|
1749
|
|
|
6
|
24
|
|
|
24
|
|
88
|
use File::Path qw(mkpath); |
|
|
24
|
|
|
|
|
165
|
|
|
|
24
|
|
|
|
|
2599
|
|
|
7
|
24
|
|
|
24
|
|
120
|
use English qw(-no_match_vars); |
|
|
24
|
|
|
|
|
34
|
|
|
|
24
|
|
|
|
|
127
|
|
|
8
|
24
|
|
|
24
|
|
22238
|
use Storable; |
|
|
24
|
|
|
|
|
66809
|
|
|
|
24
|
|
|
|
|
1333
|
|
|
9
|
24
|
|
|
24
|
|
10764
|
use Armadito::Agent::Logger; |
|
|
24
|
|
|
|
|
47
|
|
|
|
24
|
|
|
|
|
9256
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
|
12
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
|
13
|
|
|
|
|
|
|
|
|
14
|
0
|
0
|
|
|
|
|
die "no directory parameter" unless $params{directory}; |
|
15
|
0
|
0
|
|
|
|
|
if ( !-d $params{directory} ) { |
|
16
|
0
|
|
|
|
|
|
eval { mkpath( $params{directory} ); }; |
|
|
0
|
|
|
|
|
|
|
|
17
|
0
|
0
|
|
|
|
|
die "Can't create $params{directory}: $EVAL_ERROR" if $EVAL_ERROR; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $self = { |
|
21
|
|
|
|
|
|
|
logger => $params{logger}, |
|
22
|
|
|
|
|
|
|
directory => $params{directory} |
|
23
|
0
|
|
|
|
|
|
}; |
|
24
|
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
bless $self, $class; |
|
26
|
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
return $self; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub getDirectory { |
|
31
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
32
|
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return $self->{directory}; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _getFilePath { |
|
37
|
0
|
|
|
0
|
|
|
my ( $self, %params ) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
die "no name parameter given" unless $params{name}; |
|
40
|
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return $self->{directory} . '/' . $params{name} . '.dump'; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub has { |
|
45
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $file = $self->_getFilePath(%params); |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return -f $file; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub save { |
|
53
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
|
54
|
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $file = $self->_getFilePath(%params); |
|
56
|
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
store( $params{data}, $file ) or warn; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub restore { |
|
61
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
|
62
|
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $file = $self->_getFilePath(%params); |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
return unless -f $file; |
|
66
|
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $result; |
|
68
|
0
|
|
|
|
|
|
eval { $result = retrieve($file); }; |
|
|
0
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
if ($EVAL_ERROR) { |
|
70
|
0
|
|
|
|
|
|
$self->{logger}->error("Can't read corrupted $file, removing it"); |
|
71
|
0
|
|
|
|
|
|
unlink $file; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $result; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub remove { |
|
78
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $file = $self->_getFilePath(%params); |
|
81
|
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
unlink $file or $self->{logger}->error("can't unlink $file"); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
|
86
|
|
|
|
|
|
|
__END__ |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 NAME |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Armadito::Agent::Storage - A data serializer/deserializer |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $storage = Armadito::Agent::Storage->new( |
|
95
|
|
|
|
|
|
|
directory => '/tmp' |
|
96
|
|
|
|
|
|
|
); |
|
97
|
|
|
|
|
|
|
my $data = $storage->restore( |
|
98
|
|
|
|
|
|
|
module => "Armadito::Agent" |
|
99
|
|
|
|
|
|
|
); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$data->{foo} = 'bar'; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$storage->save(data => $data); |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This is the object used by the agent to ensure data persistancy between |
|
108
|
|
|
|
|
|
|
invocations. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Each data structure is saved in a file, whose name is automatically determined |
|
111
|
|
|
|
|
|
|
according to object class name. An optional index number can be used to |
|
112
|
|
|
|
|
|
|
differentiate between consecutives usages. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 METHODS |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 new(%params) |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
The constructor. The following parameters are allowed, as keys of the %params |
|
119
|
|
|
|
|
|
|
hash: |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=over |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item I<logger> |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
the logger object to use |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item I<directory> |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
the directory to use for storing data (mandatory) |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=back |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 getDirectory |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Returns the underlying directory for this storage. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 has(%params) |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Returns true if a saved data structure exists. The following arguments are |
|
140
|
|
|
|
|
|
|
allowed: |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=over |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item I<name> |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
The file name to use for saving the data structure (mandatory). |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=back |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 save(%params) |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Save given data structure. The following parameters are allowed, as keys of the |
|
153
|
|
|
|
|
|
|
%params hash: |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=over |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item I<name> |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
The file name to use for saving the data structure (mandatory). |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=back |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 restore(%params) |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Restore a saved data structure. The following parameters are allowed, as keys |
|
166
|
|
|
|
|
|
|
of the %params hash: |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=over |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item I<name> |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
The file name to use for saving the data structure (mandatory). |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=back |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 remove(%params) |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Delete the file containing a seralized data structure for a given module. The |
|
179
|
|
|
|
|
|
|
following parameters are allowed, as keys of the %params hash: |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item I<name> |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
The file name to use for saving the data structure (mandatory). |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |