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.10 |
18 |
|
|
19 |
=cut |
=cut |
20 |
|
|
21 |
our $VERSION = '0.07'; |
our $VERSION = '0.10'; |
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. |
65 |
|
|
66 |
sub new { |
sub new { |
67 |
my $class = shift; |
my $class = shift; |
68 |
my $self = {@_}; |
my $self = {@_}; |
69 |
bless($self, $class); |
bless($self, $class); |
70 |
|
|
71 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
72 |
|
|
131 |
|
|
132 |
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 |
133 |
|
|
134 |
my $ds = $db->load_ds( id => 42, prefix => 'name' ); |
my $ds = $db->load_ds( id => 42, prefix => 'name', database => 'ps' ); |
135 |
|
|
136 |
This function will also perform basic sanity checking on returned |
This function will also perform basic sanity checking on returned |
137 |
data and disable caching if data is corrupted (or changed since last |
data and disable caching if data is corrupted (or changed since last |
140 |
C<prefix> is used to differenciate different source input databases |
C<prefix> is used to differenciate different source input databases |
141 |
which are indexed in same database. |
which are indexed in same database. |
142 |
|
|
143 |
|
C<database> if B<optional> argument which will override database name used when creating |
144 |
|
C<WebPAC::Store> object (for simple retrival from multiple databases). |
145 |
|
|
146 |
Returns hash or undef if cacheing is disabled or unavailable. |
Returns hash or undef if cacheing is disabled or unavailable. |
147 |
|
|
148 |
=cut |
=cut |
167 |
$log->logconfess("got hash, but without id") unless (defined($id)); |
$log->logconfess("got hash, but without id") unless (defined($id)); |
168 |
$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+$/); |
169 |
|
|
170 |
my $database = $self->{database}; |
my $database = $args->{database} || $self->{database}; |
171 |
my $prefix = $args->{prefix} || ''; |
my $prefix = $args->{prefix} || ''; |
172 |
|
|
173 |
$log->logconfess("can't find database name") unless ($database); |
$log->logconfess("can't find database name") unless ($database); |
174 |
|
|
175 |
my $cache_file = "$cache_path/$database/$prefix#$id"; |
my $cache_file = "$cache_path/$database/$prefix/$id"; |
176 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
177 |
|
|
|
open(my $fh, '>>', '/tmp/foo'); |
|
|
print $fh "LOAD $cache_path / $database / $prefix # $id ==> $cache_file\n"; |
|
|
close($fh); |
|
|
|
|
178 |
$log->debug("using cache_file $cache_file"); |
$log->debug("using cache_file $cache_file"); |
179 |
|
|
180 |
if (-r $cache_file) { |
if (-r $cache_file) { |
232 |
|
|
233 |
my $prefix = $arg->{prefix} || ''; |
my $prefix = $arg->{prefix} || ''; |
234 |
|
|
235 |
my $cache_file = $self->{path} . "/$prefix#" . $arg->{id}; |
my $cache_file = $self->{path} . '/' . $prefix . '/'; |
236 |
$cache_file =~ s#//#/#go; |
$cache_file =~ s#//#/#go; |
237 |
|
|
238 |
|
mkpath($cache_file) unless (-d $cache_file); |
239 |
|
|
240 |
|
$cache_file .= $arg->{id}; |
241 |
|
|
242 |
$log->debug("creating storable cache file $cache_file"); |
$log->debug("creating storable cache file $cache_file"); |
243 |
|
|
244 |
return store { |
return store { |
248 |
|
|
249 |
} |
} |
250 |
|
|
251 |
|
=head2 save_lookup |
252 |
|
|
253 |
|
$db->save_lookup( $database, $input, $key, $lookup ); |
254 |
|
|
255 |
|
=cut |
256 |
|
|
257 |
|
sub save_lookup { |
258 |
|
my $self = shift; |
259 |
|
my ($database, $input, $key, $lookup) = @_; |
260 |
|
|
261 |
|
my $log = $self->_get_logger; |
262 |
|
|
263 |
|
my $path = $self->{'path'} . "/lookup/$input"; |
264 |
|
|
265 |
|
mkpath($path) unless (-d $path); |
266 |
|
|
267 |
|
$path .= "/$key"; |
268 |
|
|
269 |
|
if (store $lookup, $path) { |
270 |
|
$log->info("saved lookup $path"); |
271 |
|
} else { |
272 |
|
$log->logwarn("can't store lookup $database/$input/$key in $path: $!"); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
=head1 AUTHOR |
=head1 AUTHOR |
280 |
|
|
281 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |