7 use base qw/WebPAC::Common Class::Accessor/;
8 __PACKAGE__->mk_accessors(qw/database/);
12 use Data::Dump qw/dump/;
16 WebPAC::Store - Store WebPAC data on disk
24 our $VERSION = '0.15';
28 This module provides disk storage for normalised data and lookups.
30 It is one of newer components of WebPAC, so it will change from time to
33 I will try to keep backward compatiblity by providing multiple back-ends,
34 but this can't be garanteed. In other words, don't delete your input
35 databases just yet :-)
37 This has additional advantage. I can create single place to plugin other
38 file formats which provide better performance for particular type of data.
44 Create new normalised database object
46 my $store = new WebPAC::Store({
50 Optional parameter C<path> defines path to directory
51 in which cache file for C<data_structure> call will be created.
53 Optional parametar C<database> will be used used as subdirectory in path if no
54 database in specified when calling other functions.
60 Retrive from disk one data_structure records usually using field 000 as key
62 my $ds = $store->load_ds(
68 This function will also perform basic sanity checking on returned
69 data and disable caching if data is corrupted (or changed since last
72 C<input> is used to differenciate different source input databases
73 which are indexed in same database.
75 C<database> if B<optional> argument which will override database name used when creating
76 C<WebPAC::Store> object (for simple retrival from multiple databases).
78 Returns hash or undef if cacheing is disabled or unavailable.
85 my $log = $self->_get_logger;
87 $log->logconfess("arguments for load_ds must be HASH") unless (ref(\@_) eq 'ARRAY' && ($#_ % 2 == 1));
92 $log->logconfess("got hash, but without id") unless (defined($id));
93 $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
95 my $database = $args->{database} || $self->database || $log->logconfess("no database?");
97 my $input = $args->{input} || '';
99 my $cache_file = $self->var_path( 'ds', $database, $input, $id );
101 $log->debug("using cache_file $cache_file");
103 if (-r $cache_file) {
104 my $ds_ref = retrieve($cache_file);
106 $log->debug("cache hit: $cache_file");
107 if ($ds_ref->{'ds'}) {
108 return $ds_ref->{'ds'};
110 $log->warn("cache entry $cache_file corrupt. Use rm $cache_file to re-create it on next run!");
114 #$log->warn("cache entry $cache_file doesn't exist");
123 Store data_structure on disk.
128 id => $ds->{000}->[0],
132 C<database> and C<input> are optional.
141 my $log = $self->_get_logger;
142 $log->debug("save_ds arguments:", sub { dump( \@_ ) });
144 foreach my $f (qw/id ds/) {
145 $log->logconfess("need $f") unless (defined($args->{$f}));
148 my $database = $args->{database} || $self->{database};
149 $log->logconfess("can't find database name") unless (defined($database));
151 my $input = $args->{input} || '';
153 my $cache_file = $self->var_path( 'ds', $database, $input );
155 mkpath($cache_file) unless (-d $cache_file);
157 $cache_file = $self->var_path( 'ds', $database, $input, $args->{id} );
159 $log->debug("creating storable cache file $cache_file");
170 Loads lookup hash from file
172 $data = $store->load_lookup(
173 database => $database,
178 C<database> is optional.
186 my $log = $self->_get_logger;
188 foreach my $r (qw/input key/) {
189 $log->logconfess("need '$r'") unless defined($args->{$r});
192 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
194 my $path = $self->var_path( 'lookup', $database, $args->{input}, $args->{key} );
197 $log->error("lookup $path doesn't exist, lookups will be disabled. Try re-indexing $database/", $args->{input});
201 if (my $data = retrieve($path)) {
202 $log->info("loaded lookup $path ", -s $path, " bytes");
205 $log->logwarn("can't load lookup $database/", $args->{input}, "/", $args->{key}, " from $path: $!");
212 Save lookup data to file.
215 database => $database,
221 C<database> is optional.
229 my $log = $self->_get_logger;
231 foreach my $r (qw/input key data/) {
232 $log->logconfess("need '$r'") unless defined($args->{$r});
235 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
237 my $path = $self->var_path( 'lookup', $database, $args->{input} );
239 mkpath($path) unless (-d $path);
241 $path .= "/" . $args->{key};
245 if (store $args->{data}, $path) {
246 $log->info(sprintf("saved lookup %s %d bytes in %.2fs", $path, -s $path, time() - $t));
249 $log->logwarn("can't save lookup to $path: $!");
256 Loads row from input database cache (used for lookups)
258 $row = $store->load_row(
259 database => $database,
264 C<database> is optional.
272 my $log = $self->_get_logger;
274 foreach my $r (qw/input id/) {
275 $log->logconfess("need '$r'") unless defined($args->{$r});
278 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
280 my $path = $self->var_path( 'row', $database, $args->{input}, $args->{id} );
283 $log->warn("input row $path doesn't exist, skipping");
287 if (my $data = retrieve($path)) {
288 $log->debug("loaded row $path");
291 $log->logwarn("can't load row from $path: $!");
298 Save row data to file.
301 database => $database,
307 C<database> is optional.
315 my $log = $self->_get_logger;
317 foreach my $r (qw/input id row/) {
318 $log->logconfess("need '$r'") unless defined($args->{$r});
321 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
323 my $path = $self->var_path( 'row', $database, $args->{input} );
325 mkpath($path) unless (-d $path);
327 $path .= "/" . $args->{id};
329 if (store $args->{row}, $path) {
330 $log->debug("saved row $path");
333 $log->logwarn("can't save row to $path: $!");
341 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
343 =head1 COPYRIGHT & LICENSE
345 Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved.
347 This program is free software; you can redistribute it and/or modify it
348 under the same terms as Perl itself.
352 1; # End of WebPAC::Store