#log4perl.logger.WebPAC=DEBUG
-#log4perl.logger.WebPAC.DB=DEBUG
-#log4perl.logger.WebPAC.DB.new=DEBUG
-#log4perl.logger.WebPAC.DB.save_ds=DEBUG
+#log4perl.logger.WebPAC.Store=DEBUG
+#log4perl.logger.WebPAC.Store.new=DEBUG
+#log4perl.logger.WebPAC.Store.save_ds=DEBUG
+#log4perl.logger.WebPAC.Store.load_ds=DEBUG
#log4perl.logger.WebPAC.Lookup=DEBUG
=head1 VERSION
-Version 0.02
+Version 0.03
=cut
-our $VERSION = '0.02';
+our $VERSION = '0.03';
=head1 SYNOPSIS
my $cache_file;
if ($self->{'db'}) {
- my $ds = $self->{'db'}->load_ds( $mfn );
+ my $ds = $self->{'db'}->load_ds( id => $mfn );
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
return $ds if ($ds);
$log->debug("cache miss, creating");
use base 'WebPAC::Common';
use Storable;
use File::Path;
+use Data::Dumper;
=head1 NAME
=head1 VERSION
-Version 0.04
+Version 0.05
=cut
-our $VERSION = '0.04';
+our $VERSION = '0.05';
=head1 SYNOPSIS
my $db = new WebPAC::Store(
path => '/path/to/cache/ds/',
+ database => 'name',
read_only => 1,
);
=head2 load_ds
-Retrive from disk one data_structure records using field 000 as key
+Retrive from disk one data_structure records usually using field 000 as key
- my $ds = $db->load_ds( 42 );
-
-There is also a more verbose form, similar to C<save_ds>
-
- my $ds = $db->load_ds( id => 42 );
+ my $ds = $db->load_ds( id => 42, database => 'name' );
This function will also perform basic sanity checking on returned
data and disable caching if data is corrupted (or changed since last
sub load_ds {
my $self = shift;
- return unless $self->{'path'};
-
my $log = $self->_get_logger;
my $cache_path = $self->{'path'};
- my $id = shift;
- if (lc($id) eq 'id') {
- $id = shift;
- $log->logconfess("got hash, but without key id") unless (defined($id));
- $log->logconfess("got hash, but id isn't number") unless ($id =~ /^\d+$/);
+ if (! $cache_path) {
+ $log->warn("path not set, ignoring load_ds");
+ return;
}
- if (! defined($id)) {
- $log->warn("called without id");
- return undef;
- } else {
- my $cache_file = "$cache_path/$id";
- if (-r $cache_file) {
- my $ds_ref = retrieve($cache_file);
- if ($ds_ref) {
- $log->debug("cache hit: $cache_file");
- my $ok = 1;
-# foreach my $f (qw(current_filename headline)) {
-# if ($ds_ref->{$f}) {
-# $self->{$f} = $ds_ref->{$f};
-# } else {
-# $ok = 0;
-# }
-# };
- if ($ok && $ds_ref->{'ds'}) {
- return $ds_ref->{'ds'};
- } else {
- $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!");
- undef $self->{'path'};
- }
+ $log->logconfess("arguments for load_ds must be HASH") unless (ref(\@_) eq 'ARRAY' && ($#_ % 2 == 1));
+
+ my $args = {@_};
+ my $id = $args->{id};
+
+ $log->logconfess("got hash, but without id") unless (defined($id));
+ $log->logconfess("got hash, but id isn't number") unless ($id =~ /^\d+$/);
+
+ my $database = $args->{database} || $self->{database};
+
+ $log->logconfess("can't find database name") unless ($database);
+
+ my $cache_file = "$cache_path/$database#$id";
+ $cache_file =~ s#//#/#g;
+
+ $log->debug("using cache_file $cache_file");
+
+ if (-r $cache_file) {
+ my $ds_ref = retrieve($cache_file);
+ if ($ds_ref) {
+ $log->debug("cache hit: $cache_file");
+ if ($ds_ref->{'ds'}) {
+ return $ds_ref->{'ds'};
+ } else {
+ $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!");
+ undef $self->{'path'};
}
- } else {
- #$log->warn("cache entry $cache_file doesn't exist");
- return undef;
}
+ } else {
+ #$log->warn("cache entry $cache_file doesn't exist");
+ return undef;
}
return undef;
$db->save_ds(
id => $ds->{000}->[0],
+ database => 'name',
ds => $ds,
);
$log->logconfess("need $f") unless ($arg->{$f});
}
- my $cache_file = $self->{path} . '/' . $arg->{id};
+ my $database = $arg->{database} || $self->{database};
+ $log->logconfess("can't find database name") unless ($database);
+
+ my $cache_file = $self->{path} . '/' . $database . '#' . $arg->{id};
$log->debug("creating storable cache file $cache_file");
my $db = new WebPAC::Store(
path => $db_path,
+ database => $database,
);
my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
#!/usr/bin/perl -w
-use Test::More tests => 25;
+use Test::More tests => 29;
use Test::Exception;
use Cwd qw/abs_path/;
use blib;
diag "NULL Store";
-ok($db = new WebPAC::Store( debug => $debug, ), "new");
+ok($db = new WebPAC::Store( debug => $debug ), "new");
ok(! $db->path, "path");
diag "Store path: $path";
-ok($db = new WebPAC::Store( path => $path, debug => $debug, no_log => $no_log ), "new");
+ok($db = new WebPAC::Store( path => $path, database => '.', debug => $debug, no_log => $no_log ), "new");
cmp_ok($db->{'path'}, 'eq', $path, "path");
cmp_ok($db->{'path'}, 'eq', $path, "path");
-ok(! $db->load_ds(), 'load_ds');
+throws_ok { $db->load_ds() } qr/without id/, 'load_ds without arguments';
ok(! $db->load_ds( id => 000 ), 'load_ds');
throws_ok { $db->save_ds() } qr/id/, "save_ds - need id";
is_deeply($ds, $ds2, "loaded data");
-ok($ds2 = $db->load_ds( 1 ), "load_ds without id");
+throws_ok { $ds2 = $db->load_ds( 1 ) } qr/HASH/, "load_ds without hash";
+
+ok($ds2 = $db->load_ds( id => 1 ), "load_ds");
is_deeply($ds, $ds2, "loaded data");
ok(! $db->load_ds( id => 42 ), "load_ds non-existing");
+ok($db = new WebPAC::Store( path => $path, debug => $debug, no_log => $no_log ), "new without database");
+
+throws_ok { $ds2 = $db->load_ds( id => 1 ) } qr/database/, "load_ds without database";
+
+ok($ds2 = $db->load_ds( id => 1, database => '.' ), "load_ds");
+
undef $db;
ok(my $db = new WebPAC::Store(
path => $path,
+ database => '.',
), "new Store");
ok(my $n = new WebPAC::Normalize::XML(