6 use base 'WebPAC::Common';
9 use Data::Dump qw/dump/;
13 WebPAC::Store - Store WebPAC data on disk
21 our $VERSION = '0.14';
25 This module provides disk storage for normalised data and lookups.
27 It is one of newer components of WebPAC, so it will change from time to
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 :-)
34 This has additional advantage. I can create single place to plugin other
35 file formats which provide better performance for particular type of data.
37 For now, this is a prototype version.
41 my $store = WebPAC::Store->new();
48 Create new normalised database object
50 my $store = new WebPAC::Store(
51 path => '/path/to/cache/ds/',
56 Optional parameter C<path> defines path to directory
57 in which cache file for C<data_structure> call will be created.
59 If called with C<read_only> it will not disable caching if
60 called without write permission (but will die on C<save_ds>).
62 Optional parametar C<database> will be used used as subdirectory in path if no
63 database in specified when calling other functions.
72 my $log = $self->_get_logger();
74 foreach my $p (qw/path/) {
75 $log->logconfess("need $p") unless ($self->{$p});
78 $self->path( $self->{'path'} );
80 $self ? return $self : return undef;
85 Check if specified cache directory exist, and if not, disable caching.
87 $store->path('./cache/');
89 If you pass false or zero value to this function, it will disable
92 You can also call this function to get current cache path.
94 my $cache_path = $store->path;
103 return $self->{path} unless defined($dir);
105 my $log = $self->_get_logger();
110 if ($self->{'read_only'}) {
111 $msg = "doesn't exist";
113 $log->info("creating $dir");
116 } elsif (! -d $dir) {
117 $msg = "is not directory";
118 } elsif (! -w $dir) {
119 $msg = "not writable" unless ($self->{'read_only'});
123 $log->warn("cache path $dir $msg, disabling...");
124 undef $self->{'path'};
126 $log->debug("using cache dir $dir");
127 $self->{'path'} = $dir;
130 $log->debug("disabling cache");
131 undef $self->{'path'};
137 Retrive from disk one data_structure records usually using field 000 as key
139 my $ds = $store->load_ds(
145 This function will also perform basic sanity checking on returned
146 data and disable caching if data is corrupted (or changed since last
149 C<input> is used to differenciate different source input databases
150 which are indexed in same database.
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).
155 Returns hash or undef if cacheing is disabled or unavailable.
162 my $log = $self->_get_logger;
164 my $cache_path = $self->{'path'};
167 $log->warn("path not set, ignoring load_ds");
171 $log->logconfess("arguments for load_ds must be HASH") unless (ref(\@_) eq 'ARRAY' && ($#_ % 2 == 1));
174 my $id = $args->{id};
176 $log->logconfess("got hash, but without id") unless (defined($id));
177 $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
179 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
181 my $input = $args->{input} || '';
183 my $cache_file = "$cache_path/ds/$database/$input/$id";
184 $cache_file =~ s#//#/#go;
186 $log->debug("using cache_file $cache_file");
188 if (-r $cache_file) {
189 my $ds_ref = retrieve($cache_file);
191 $log->debug("cache hit: $cache_file");
192 if ($ds_ref->{'ds'}) {
193 return $ds_ref->{'ds'};
195 $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!");
196 undef $self->{'path'};
200 #$log->warn("cache entry $cache_file doesn't exist");
209 Store data_structure on disk.
214 id => $ds->{000}->[0],
218 C<database> and C<input> are optional.
225 die "can't write to database in read_only mode!" if ($self->{'read_only'});
227 return unless($self->{'path'});
231 my $log = $self->_get_logger;
232 $log->debug("save_ds arguments:", dump( \@_ ));
234 foreach my $f (qw/id ds/) {
235 $log->logconfess("need $f") unless (defined($args->{$f}));
238 my $database = $args->{database} || $self->{database};
239 $log->logconfess("can't find database name") unless (defined($database));
241 my $input = $args->{input} || '';
243 my $cache_file = $self->{path} . "/ds/$database/$input/";
244 $cache_file =~ s#//#/#go;
246 mkpath($cache_file) unless (-d $cache_file);
248 $cache_file .= $args->{id};
250 $log->debug("creating storable cache file $cache_file");
261 Loads lookup hash from file
263 $data = $store->load_lookup(
264 database => $database,
269 C<database> is optional.
277 my $log = $self->_get_logger;
279 foreach my $r (qw/input key/) {
280 $log->logconfess("need '$r'") unless defined($args->{$r});
283 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
285 my $path = $self->{path} . "/lookup/$database/" . $args->{input} . '/' . $args->{key};
288 $log->error("lookup $path doesn't exist, lookups will be disabled. Try re-indexing $database/", $args->{input});
292 if (my $data = retrieve($path)) {
293 $log->info("loaded lookup $path");
296 $log->logwarn("can't load lookup $database/", $args->{input}, "/", $args->{key}, " from $path: $!");
303 Save lookup data to file.
306 database => $database,
312 C<database> is optional.
320 my $log = $self->_get_logger;
322 foreach my $r (qw/input key data/) {
323 $log->logconfess("need '$r'") unless defined($args->{$r});
326 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
328 my $path = $self->{path} . "/lookup/$database/" . $args->{input};
330 mkpath($path) unless (-d $path);
332 $path .= "/" . $args->{key};
336 if (store $args->{data}, $path) {
337 $log->info(sprintf("saved lookup $path in %.2fs", time() - $t));
340 $log->logwarn("can't save lookup to $path: $!");
347 Loads row from input database cache (used for lookups)
349 $row = $store->load_row(
350 database => $database,
355 C<database> is optional.
363 my $log = $self->_get_logger;
365 foreach my $r (qw/input id/) {
366 $log->logconfess("need '$r'") unless defined($args->{$r});
369 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
371 my $path = $self->{path} . "/row/$database/" . $args->{input} . '/' . $args->{id};
374 $log->warn("input row $path doesn't exist, skipping");
378 if (my $data = retrieve($path)) {
379 $log->debug("loaded row $path");
382 $log->logwarn("can't load row from $path: $!");
389 Save row data to file.
392 database => $database,
398 C<database> is optional.
406 my $log = $self->_get_logger;
408 foreach my $r (qw/input id row/) {
409 $log->logconfess("need '$r'") unless defined($args->{$r});
412 my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
414 my $path = $self->{path} . "/row/$database/" . $args->{input};
416 mkpath($path) unless (-d $path);
418 $path .= "/" . $args->{id};
420 if (store $args->{row}, $path) {
421 $log->debug("saved row $path");
424 $log->logwarn("can't save row to $path: $!");
432 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
434 =head1 COPYRIGHT & LICENSE
436 Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved.
438 This program is free software; you can redistribute it and/or modify it
439 under the same terms as Perl itself.
443 1; # End of WebPAC::Store