6 |
use base 'WebPAC::Common'; |
use base 'WebPAC::Common'; |
7 |
use Storable; |
use Storable; |
8 |
use File::Path; |
use File::Path; |
9 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
13 |
WebPAC::Store - Store normalized data on disk |
WebPAC::Store - Store WebPAC data on disk |
14 |
|
|
15 |
=head1 VERSION |
=head1 VERSION |
16 |
|
|
17 |
Version 0.07 |
Version 0.11 |
18 |
|
|
19 |
=cut |
=cut |
20 |
|
|
21 |
our $VERSION = '0.07'; |
our $VERSION = '0.11'; |
22 |
|
|
23 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
24 |
|
|
25 |
This module provides disk storage for normalised data. |
This module provides disk storage for normalised data and lookups. |
26 |
|
|
27 |
It is newest component of WebPAC, so it will change quite often or be in |
It is one of newer components of WebPAC, so it will change from time to |
28 |
flux. However, I will try to keep backward compatiblity by providing |
time. |
29 |
multiple back-ends. |
|
30 |
|
I will try to keep backward compatiblity by providing multiple back-ends, |
31 |
|
but this can't be garanteed. In other words, don't delete your input |
32 |
|
databases just yet :-) |
33 |
|
|
34 |
This has additional advantage. I can create single place to plugin other |
This has additional advantage. I can create single place to plugin other |
35 |
file formats which provide better performance for particular type of data. |
file formats which provide better performance for particular type of data. |
59 |
If called with C<read_only> it will not disable caching if |
If called with C<read_only> it will not disable caching if |
60 |
called without write permission (but will die on C<save_ds>). |
called without write permission (but will die on C<save_ds>). |
61 |
|
|
62 |
Mandatory parametar C<database> is used as subdirectory in database directory. |
Optional parametar C<database> will be used used as subdirectory in path if no |
63 |
|
database in specified when calling other functions. |
64 |
|
|
65 |
=cut |
=cut |
66 |
|
|
67 |
sub new { |
sub new { |
68 |
my $class = shift; |
my $class = shift; |
69 |
my $self = {@_}; |
my $self = {@_}; |
70 |
bless($self, $class); |
bless($self, $class); |
71 |
|
|
72 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
73 |
|
|
74 |
foreach my $p (qw/path database/) { |
foreach my $p (qw/path/) { |
75 |
$log->logconfess("need $p") unless ($self->{$p}); |
$log->logconfess("need $p") unless ($self->{$p}); |
76 |
} |
} |
77 |
|
|
89 |
If you pass false or zero value to this function, it will disable |
If you pass false or zero value to this function, it will disable |
90 |
cacheing. |
cacheing. |
91 |
|
|
92 |
You can also example C<< $db->{path} >> to get current cache path. |
You can also call this function to get current cache path. |
93 |
|
|
94 |
|
my $cache_path = $db->path; |
95 |
|
|
96 |
=cut |
=cut |
97 |
|
|
99 |
my $self = shift; |
my $self = shift; |
100 |
|
|
101 |
my $dir = shift; |
my $dir = shift; |
102 |
|
|
103 |
|
return $self->{path} unless defined($dir); |
104 |
|
|
105 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
106 |
|
|
136 |
|
|
137 |
Retrive from disk one data_structure records usually using field 000 as key |
Retrive from disk one data_structure records usually using field 000 as key |
138 |
|
|
139 |
my $ds = $db->load_ds( id => 42, prefix => 'name' ); |
my $ds = $db->load_ds( |
140 |
|
database => 'ps', |
141 |
|
input => 'name', |
142 |
|
id => 42, |
143 |
|
); |
144 |
|
|
145 |
This function will also perform basic sanity checking on returned |
This function will also perform basic sanity checking on returned |
146 |
data and disable caching if data is corrupted (or changed since last |
data and disable caching if data is corrupted (or changed since last |
147 |
update). |
update). |
148 |
|
|
149 |
C<prefix> is used to differenciate different source input databases |
C<input> is used to differenciate different source input databases |
150 |
which are indexed in same database. |
which are indexed in same database. |
151 |
|
|
152 |
|
C<database> if B<optional> argument which will override database name used when creating |
153 |
|
C<WebPAC::Store> object (for simple retrival from multiple databases). |
154 |
|
|
155 |
Returns hash or undef if cacheing is disabled or unavailable. |
Returns hash or undef if cacheing is disabled or unavailable. |
156 |
|
|
157 |
=cut |
=cut |
176 |
$log->logconfess("got hash, but without id") unless (defined($id)); |
$log->logconfess("got hash, but without id") unless (defined($id)); |
177 |
$log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/); |
$log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/); |
178 |
|
|
179 |
my $database = $self->{database}; |
my $database = $args->{database} || $self->{database} || $log->logconfess("no database?"); |
|
my $prefix = $args->{prefix} || ''; |
|
180 |
|
|
181 |
$log->logconfess("can't find database name") unless ($database); |
my $input = $args->{input} || ''; |
182 |
|
|
183 |
my $cache_file = "$cache_path/$database/$prefix#$id"; |
my $cache_file = "$cache_path/$database/$input/$id"; |
184 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
185 |
|
|
|
open(my $fh, '>>', '/tmp/foo'); |
|
|
print $fh "LOAD $cache_path / $database / $prefix # $id ==> $cache_file\n"; |
|
|
close($fh); |
|
|
|
|
186 |
$log->debug("using cache_file $cache_file"); |
$log->debug("using cache_file $cache_file"); |
187 |
|
|
188 |
if (-r $cache_file) { |
if (-r $cache_file) { |
209 |
Store data_structure on disk. |
Store data_structure on disk. |
210 |
|
|
211 |
$db->save_ds( |
$db->save_ds( |
212 |
|
database => 'name', |
213 |
|
input => 'name', |
214 |
id => $ds->{000}->[0], |
id => $ds->{000}->[0], |
|
prefix => 'name', |
|
215 |
ds => $ds, |
ds => $ds, |
216 |
); |
); |
217 |
|
|
228 |
|
|
229 |
return unless($self->{'path'}); |
return unless($self->{'path'}); |
230 |
|
|
231 |
my $arg = {@_}; |
my $args = {@_}; |
232 |
|
|
233 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
234 |
|
$log->debug("save_ds arguments:", dump( \@_ )); |
235 |
|
|
236 |
foreach my $f (qw/id ds/) { |
foreach my $f (qw/id ds/) { |
237 |
$log->logconfess("need $f") unless ($arg->{$f}); |
$log->logconfess("need $f") unless (defined($args->{$f})); |
238 |
} |
} |
239 |
|
|
240 |
my $database = $self->{database}; |
my $database = $args->{database} || $self->{database}; |
241 |
$log->logconfess("can't find database name") unless ($database); |
$log->logconfess("can't find database name") unless (defined($database)); |
242 |
|
|
243 |
my $prefix = $arg->{prefix} || ''; |
my $input = $args->{input} || ''; |
244 |
|
|
245 |
my $cache_file = $self->{path} . "/$prefix#" . $arg->{id}; |
my $cache_file = $self->{path} . "/$database/$input/"; |
246 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
247 |
|
|
248 |
|
mkpath($cache_file) unless (-d $cache_file); |
249 |
|
|
250 |
|
$cache_file .= $args->{id}; |
251 |
|
|
252 |
$log->debug("creating storable cache file $cache_file"); |
$log->debug("creating storable cache file $cache_file"); |
253 |
|
|
254 |
return store { |
return store { |
255 |
ds => $arg->{ds}, |
ds => $args->{ds}, |
256 |
id => $arg->{id}, |
id => $args->{id}, |
257 |
}, $cache_file; |
}, $cache_file; |
258 |
|
|
259 |
} |
} |
260 |
|
|
261 |
|
=head2 save_lookup |
262 |
|
|
263 |
|
$db->save_lookup( |
264 |
|
database => $database, |
265 |
|
input => $input, |
266 |
|
key => $key, |
267 |
|
data => $lookup, |
268 |
|
); |
269 |
|
|
270 |
|
=cut |
271 |
|
|
272 |
|
sub save_lookup { |
273 |
|
my $self = shift; |
274 |
|
my $args = {@_}; |
275 |
|
|
276 |
|
my $log = $self->_get_logger; |
277 |
|
|
278 |
|
foreach my $r (qw/input key data/) { |
279 |
|
$log->logconfess("need '$r'") unless defined($args->{$r}); |
280 |
|
} |
281 |
|
|
282 |
|
my $database = $args->{database} || $self->{database} || $log->logconfess("no database?"); |
283 |
|
|
284 |
|
my $path = $self->{path} . "/lookup/$database/" . $args->{input}; |
285 |
|
|
286 |
|
mkpath($path) unless (-d $path); |
287 |
|
|
288 |
|
$path .= "/" . $args->{key}; |
289 |
|
|
290 |
|
if (store $args->{data}, $path) { |
291 |
|
$log->info("saved lookup $path"); |
292 |
|
return 1; |
293 |
|
} else { |
294 |
|
$log->logwarn("can't store lookup $database/", $args->{input}, "/", $args->{key}, " in $path: $!"); |
295 |
|
return undef; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
=head1 AUTHOR |
=head1 AUTHOR |
301 |
|
|
302 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |