=head1 VERSION
-Version 0.12
+Version 0.14
=cut
-our $VERSION = '0.12';
+our $VERSION = '0.14';
=head1 SYNOPSIS
use WebPAC::Store;
- my $foo = WebPAC::Store->new();
+ my $store = WebPAC::Store->new();
...
=head1 FUNCTIONS
Create new normalised database object
- my $db = new WebPAC::Store(
+ my $store = new WebPAC::Store(
path => '/path/to/cache/ds/',
database => 'name',
read_only => 1,
Check if specified cache directory exist, and if not, disable caching.
- $db->path('./cache/ds/');
+ $store->path('./cache/');
If you pass false or zero value to this function, it will disable
cacheing.
You can also call this function to get current cache path.
- my $cache_path = $db->path;
+ my $cache_path = $store->path;
=cut
Retrive from disk one data_structure records usually using field 000 as key
- my $ds = $db->load_ds(
+ my $ds = $store->load_ds(
database => 'ps',
input => 'name',
id => 42,
my $input = $args->{input} || '';
- my $cache_file = "$cache_path/$database/$input/$id";
+ my $cache_file = "$cache_path/ds/$database/$input/$id";
$cache_file =~ s#//#/#go;
$log->debug("using cache_file $cache_file");
Store data_structure on disk.
- $db->save_ds(
+ $store->save_ds(
database => 'name',
input => 'name',
id => $ds->{000}->[0],
my $args = {@_};
my $log = $self->_get_logger;
- $log->debug("save_ds arguments:", dump( \@_ ));
+ $log->debug("save_ds arguments:", sub { dump( \@_ ) });
foreach my $f (qw/id ds/) {
$log->logconfess("need $f") unless (defined($args->{$f}));
my $input = $args->{input} || '';
- my $cache_file = $self->{path} . "/$database/$input/";
+ my $cache_file = $self->{path} . "/ds/$database/$input/";
$cache_file =~ s#//#/#go;
mkpath($cache_file) unless (-d $cache_file);
Loads lookup hash from file
- $data = $db->load_lookup(
+ $data = $store->load_lookup(
database => $database,
input => $input,
key => $key,
my $path = $self->{path} . "/lookup/$database/" . $args->{input} . '/' . $args->{key};
if (! -e $path) {
- $log->warn("lookup $path doesn't exist, skipping");
+ $log->error("lookup $path doesn't exist, lookups will be disabled. Try re-indexing $database/", $args->{input});
return;
}
Save lookup data to file.
- $db->save_lookup(
+ $store->save_lookup(
database => $database,
input => $input,
key => $key,
$path .= "/" . $args->{key};
+ my $t = time();
+
if (store $args->{data}, $path) {
- $log->info("saved lookup $path");
+ $log->info(sprintf("saved lookup $path in %.2fs", time() - $t));
+ return 1;
+ } else {
+ $log->logwarn("can't save lookup to $path: $!");
+ return undef;
+ }
+}
+
+=head2 load_row
+
+Loads row from input database cache (used for lookups)
+
+ $row = $store->load_row(
+ database => $database,
+ input => $input,
+ id => 42,
+ );
+
+C<database> is optional.
+
+=cut
+
+sub load_row {
+ my $self = shift;
+ my $args = {@_};
+
+ my $log = $self->_get_logger;
+
+ foreach my $r (qw/input id/) {
+ $log->logconfess("need '$r'") unless defined($args->{$r});
+ }
+
+ my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
+
+ my $path = $self->{path} . "/row/$database/" . $args->{input} . '/' . $args->{id};
+
+ if (! -e $path) {
+ $log->warn("input row $path doesn't exist, skipping");
+ return;
+ }
+
+ if (my $data = retrieve($path)) {
+ $log->debug("loaded row $path");
+ return $data;
+ } else {
+ $log->logwarn("can't load row from $path: $!");
+ return undef;
+ }
+}
+
+=head2 save_row
+
+Save row data to file.
+
+ $store->save_row(
+ database => $database,
+ input => $input,
+ id => $mfn,
+ row => $lookup,
+ );
+
+C<database> is optional.
+
+=cut
+
+sub save_row {
+ my $self = shift;
+ my $args = {@_};
+
+ my $log = $self->_get_logger;
+
+ foreach my $r (qw/input id row/) {
+ $log->logconfess("need '$r'") unless defined($args->{$r});
+ }
+
+ my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
+
+ my $path = $self->{path} . "/row/$database/" . $args->{input};
+
+ mkpath($path) unless (-d $path);
+
+ $path .= "/" . $args->{id};
+
+ if (store $args->{row}, $path) {
+ $log->debug("saved row $path");
return 1;
} else {
- $log->logwarn("can't save lookup $database/", $args->{input}, "/", $args->{key}, " in $path: $!");
+ $log->logwarn("can't save row to $path: $!");
return undef;
}
}