line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::BasicBot::Pluggable::Store; |
2
|
|
|
|
|
|
|
$Bot::BasicBot::Pluggable::Store::VERSION = '1.20'; |
3
|
15
|
|
|
15
|
|
51859
|
use strict; |
|
15
|
|
|
|
|
22
|
|
|
15
|
|
|
|
|
355
|
|
4
|
15
|
|
|
15
|
|
53
|
use warnings; |
|
15
|
|
|
|
|
16
|
|
|
15
|
|
|
|
|
329
|
|
5
|
15
|
|
|
15
|
|
47
|
use Carp qw( croak ); |
|
15
|
|
|
|
|
18
|
|
|
15
|
|
|
|
|
571
|
|
6
|
15
|
|
|
15
|
|
2222
|
use Data::Dumper; |
|
15
|
|
|
|
|
22693
|
|
|
15
|
|
|
|
|
639
|
|
7
|
15
|
|
|
15
|
|
6722
|
use Storable qw( nfreeze thaw ); |
|
15
|
|
|
|
|
31007
|
|
|
15
|
|
|
|
|
792
|
|
8
|
15
|
|
|
15
|
|
4522
|
use Try::Tiny; |
|
15
|
|
|
|
|
16888
|
|
|
15
|
|
|
|
|
736
|
|
9
|
15
|
|
|
15
|
|
6391
|
use Module::Load qw(); |
|
15
|
|
|
|
|
11896
|
|
|
15
|
|
|
|
|
245
|
|
10
|
15
|
|
|
15
|
|
3379
|
use Log::Log4perl; |
|
15
|
|
|
|
|
176441
|
|
|
15
|
|
|
|
|
103
|
|
11
|
|
|
|
|
|
|
|
12
|
15
|
|
|
15
|
|
527
|
use base qw( ); |
|
15
|
|
|
|
|
19
|
|
|
15
|
|
|
|
|
11311
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
23
|
|
|
23
|
1
|
133
|
my $class = shift; |
16
|
23
|
|
|
|
|
25
|
my $self; |
17
|
23
|
|
|
|
|
94
|
my $logger = Log::Log4perl->get_logger($class); |
18
|
23
|
100
|
66
|
|
|
4835
|
if ( @_ % 2 == 0 ) { |
|
|
100
|
33
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
19
|
16
|
|
|
|
|
43
|
$self = bless {@_} => $class; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
elsif ( @_ == 1 and ref $_[0] eq 'HASH' ) { |
22
|
1
|
|
|
|
|
3
|
$self = $class->new_from_hashref( $_[0] ); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
elsif ( @_ == 1 and !ref $_[0] ) { |
25
|
6
|
|
|
|
|
33
|
$self = $class->new_from_hashref( { type => $_[0] } ); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
elsif ( !@_ ) { |
28
|
0
|
|
|
|
|
0
|
$self = bless {} => $class; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
else { |
31
|
0
|
|
|
|
|
0
|
$logger->warn( |
32
|
|
|
|
|
|
|
"Argument to new() is neither an argument list, a hashref, a string nor empty" |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} |
35
|
23
|
|
|
|
|
100
|
$self->init(); |
36
|
23
|
|
|
|
|
18808
|
$self->load(); |
37
|
23
|
|
|
|
|
77
|
return $self; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new_from_hashref { |
41
|
10
|
|
|
10
|
1
|
19
|
my ( $class, $args ) = @_; |
42
|
10
|
|
|
|
|
52
|
my $logger = Log::Log4perl->get_logger($class); |
43
|
|
|
|
|
|
|
|
44
|
10
|
50
|
|
|
|
918
|
if ( ref($args) ne 'HASH' ) { |
45
|
0
|
|
|
|
|
0
|
$logger->warn('Argument to store_from_hashref must be a hashref'); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
10
|
|
50
|
|
|
42
|
my $store_class = delete $args->{type} || 'Memory'; |
49
|
|
|
|
|
|
|
|
50
|
10
|
50
|
|
|
|
58
|
$store_class = "Bot::BasicBot::Pluggable::Store::$store_class" |
51
|
|
|
|
|
|
|
unless $store_class =~ /::/; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# load the store class |
54
|
10
|
|
|
10
|
|
413
|
try { Module::Load::load $store_class; } |
55
|
10
|
|
|
0
|
|
111
|
catch { $logger->warn("Couldn't load $store_class - $_"); }; |
|
0
|
|
|
|
|
0
|
|
56
|
|
|
|
|
|
|
|
57
|
10
|
|
|
|
|
258
|
my $store = $store_class->new( %{$args} ); |
|
10
|
|
|
|
|
67
|
|
58
|
|
|
|
|
|
|
|
59
|
10
|
50
|
|
|
|
51
|
croak "Couldn't init a $store_class store\n" unless $store; |
60
|
|
|
|
|
|
|
|
61
|
10
|
|
|
|
|
26
|
return $store; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
20
|
|
|
20
|
1
|
21
|
sub init { undef } |
65
|
|
|
|
|
|
|
|
66
|
22
|
|
|
22
|
1
|
38
|
sub load { undef } |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
111
|
1
|
|
sub save { } |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub keys { |
71
|
17
|
|
|
17
|
1
|
86
|
my ( $self, $namespace, %opts ) = @_; |
72
|
17
|
|
100
|
|
|
64
|
my $mod = $self->{store}{$namespace} || {}; |
73
|
17
|
|
|
|
|
57
|
return $self->_keys_aux( $mod, $namespace, %opts ); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub count_keys { |
77
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $namespace, %opts ) = @_; |
78
|
0
|
|
|
|
|
0
|
$opts{_count_only} = 1; |
79
|
0
|
|
|
|
|
0
|
$self->keys( $namespace, %opts ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _keys_aux { |
83
|
18
|
|
|
18
|
|
30
|
my ( $self, $mod, $namespace, %opts ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
18
|
100
|
|
|
|
42
|
my @res = ( exists $opts{res} ) ? @{ $opts{res} } : (); |
|
10
|
|
|
|
|
20
|
|
86
|
|
|
|
|
|
|
|
87
|
18
|
100
|
|
|
|
80
|
return CORE::keys %$mod unless @res; |
88
|
|
|
|
|
|
|
|
89
|
10
|
|
|
|
|
12
|
my @return; |
90
|
10
|
|
|
|
|
9
|
my $count = 0; |
91
|
10
|
|
|
|
|
31
|
OUTER: while ( my ($key) = each %$mod ) { |
92
|
83
|
|
|
|
|
769
|
for my $re (@res) { |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# limit matches |
95
|
83
|
100
|
|
|
|
154
|
$re = "^" . lc($namespace) . "_.*${re}.*" |
96
|
|
|
|
|
|
|
if $re =~ m!^[^\^].*[^\$]$!; |
97
|
83
|
100
|
|
|
|
362
|
next OUTER unless $key =~ m!$re!; |
98
|
|
|
|
|
|
|
} |
99
|
8
|
50
|
|
|
|
22
|
push @return, $key if ( !$opts{_count_only} ); |
100
|
8
|
100
|
100
|
|
|
40
|
last if $opts{limit} && ++$count >= $opts{limit}; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
10
|
50
|
|
|
|
765
|
return ( $opts{_count_only} ) ? $count : @return; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub get { |
108
|
1192
|
|
|
1192
|
1
|
1089
|
my ( $self, $namespace, $key ) = @_; |
109
|
1192
|
|
|
|
|
3786
|
return $self->{store}{$namespace}{$key}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub set { |
113
|
107
|
|
|
107
|
1
|
141
|
my ( $self, $namespace, $key, $value ) = @_; |
114
|
107
|
|
|
|
|
160
|
$self->{store}{$namespace}{$key} = $value; |
115
|
107
|
|
|
|
|
173
|
$self->save($namespace); |
116
|
107
|
|
|
|
|
189
|
return $self; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub unset { |
120
|
7
|
|
|
7
|
1
|
17
|
my ( $self, $namespace, $key ) = @_; |
121
|
7
|
|
|
|
|
15
|
delete $self->{store}{$namespace}{$key}; |
122
|
7
|
|
|
|
|
18
|
$self->save($namespace); |
123
|
7
|
|
|
|
|
18
|
return $self; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub namespaces { |
127
|
2
|
|
|
2
|
1
|
3
|
my $self = shift; |
128
|
2
|
|
|
|
|
2
|
return CORE::keys( %{ $self->{store} } ); |
|
2
|
|
|
|
|
16
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub dump { |
132
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
133
|
0
|
|
|
|
|
|
my $data = {}; |
134
|
0
|
|
|
|
|
|
for my $n ( $self->namespaces ) { |
135
|
0
|
|
|
|
|
|
warn "Dumping namespace '$n'.\n"; |
136
|
0
|
|
|
|
|
|
for my $k ( $self->keys($n) ) { |
137
|
0
|
|
|
|
|
|
$data->{$n}{$k} = $self->get( $n, $k ); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
return nfreeze($data); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub restore { |
144
|
0
|
|
|
0
|
1
|
|
my ( $self, $dump ) = @_; |
145
|
0
|
|
|
|
|
|
my $data = thaw($dump); |
146
|
0
|
|
|
|
|
|
for my $n ( CORE::keys(%$data) ) { |
147
|
0
|
|
|
|
|
|
warn "Restoring namespace '$n'.\n"; |
148
|
0
|
|
|
|
|
|
for my $k ( CORE::keys( %{ $data->{$n} } ) ) { |
|
0
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
$self->set( $n, $k, $data->{$n}{$k} ); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
warn "Complete.\n"; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
156
|
|
|
|
|
|
|
__END__ |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 NAME |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Bot::BasicBot::Pluggable::Store - base class for the back-end pluggable store |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 VERSION |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
version 1.20 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SYNOPSIS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $store = Bot::BasicBot::Pluggable::Store->new( option => "value" ); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $namespace = "MyModule"; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
for ( $store->keys($namespace) ) { |
173
|
|
|
|
|
|
|
my $value = $store->get($namespace, $_); |
174
|
|
|
|
|
|
|
$store->set( $namespace, $_, "$value and your momma." ); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Store classes should subclass this and provide some persistent way of storing things. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 METHODS |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over 4 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item new() |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Standard C<new> method, blesses a hash into the right class and |
186
|
|
|
|
|
|
|
puts any key/value pairs passed to it into the blessed hash. If |
187
|
|
|
|
|
|
|
called with an hash argument as its first argument, new_from_hashref |
188
|
|
|
|
|
|
|
will be run with the hash as its only argument. See L</new_from_hashref> |
189
|
|
|
|
|
|
|
for the possible keys and values. You can also pass a string and |
190
|
|
|
|
|
|
|
it will try to call new_from_hashref with a hash reference { type |
191
|
|
|
|
|
|
|
=> $string }. Calls C<load()> to load any internal variables, then |
192
|
|
|
|
|
|
|
C<init>, which you can also override in your module. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item new_from_hashref( $hashref ) |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Intended to be called as class method to dynamically create a store |
197
|
|
|
|
|
|
|
object. It expects a hash reference as its only argument. The only |
198
|
|
|
|
|
|
|
required hash element is a string specified by I<type>. This should |
199
|
|
|
|
|
|
|
be either a fully qualified classname or a colonless string that |
200
|
|
|
|
|
|
|
is appended to I<Bot::BasicBot::Pluggable::Store>. All other arguments |
201
|
|
|
|
|
|
|
are passed down to the real object constructor. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item init() |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Called as part of new class construction, before C<load()>. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item load() |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Called as part of new class construction, after C<init()>. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item save() |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Subclass me. But, only if you want to. See ...Store::Storable.pm as an example. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item keys($namespace,[$regex]) |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Returns a list of all store keys for the passed C<$namespace>. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
If you pass C<$regex> then it will only pass the keys matching C<$regex> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item get($namespace, $variable) |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Returns the stored value of the C<$variable> from C<$namespace>. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item set($namespace, $variable, $value) |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Sets stored value for C<$variable> to C<$value> in C<$namespace>. Returns store object. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item unset($namespace, $variable) |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Removes the C<$variable> from the store. Returns store object. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item namespaces() |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Returns a list of all namespaces in the store. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item dump() |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Dumps the complete store to a huge Storable scalar. This is mostly so |
240
|
|
|
|
|
|
|
you can convert from one store to another easily, i.e.: |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
my $from = Bot::BasicBot::Pluggable::Store::Storable->new(); |
243
|
|
|
|
|
|
|
my $to = Bot::BasicBot::Pluggable::Store::DBI->new( ... ); |
244
|
|
|
|
|
|
|
$to->restore( $from->dump ); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
C<dump> is written generally so you don't have to re-implement it in subclasses. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item restore($data) |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Restores the store from a L<dump()>. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=back |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 AUTHOR |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Mario Domgoergen <mdom@cpan.org> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
This program is free software; you can redistribute it |
259
|
|
|
|
|
|
|
and/or modify it under the same terms as Perl itself. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 SEE ALSO |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
L<Bot::BasicBot::Pluggable> |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
L<Bot::BasicBot::Pluggable::Module> |