r11536@llin: dpavlin | 2005-12-05 15:29:47 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:47:51 +0000 (17:47 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:47:51 +0000 (17:47 +0000)
 change on load_ds and save_ds which not accept ONLY hash (and optional
 database name if not specified when calling new WebPAC::Store)

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@217 07558da8-63fa-0310-ba24-9fe276d99e06

conf/log.conf
lib/WebPAC/Normalize.pm
lib/WebPAC/Store.pm
run.pl
t/4-store.t
t/6-unit.t

index dd76d6e..9d15e55 100644 (file)
@@ -10,9 +10,10 @@ log4perl.logger.main=INFO
 
 #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
 
index dcbbed9..d98ee70 100644 (file)
@@ -11,11 +11,11 @@ WebPAC::Normalize - data mungling for normalisation
 
 =head1 VERSION
 
-Version 0.02
+Version 0.03
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 =head1 SYNOPSIS
 
@@ -143,7 +143,7 @@ sub data_structure {
        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");
index b78a05b..8403240 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use base 'WebPAC::Common';
 use Storable;
 use File::Path;
+use Data::Dumper;
 
 =head1 NAME
 
@@ -13,11 +14,11 @@ WebPAC::Store - Store normalized data on disk
 
 =head1 VERSION
 
-Version 0.04
+Version 0.05
 
 =cut
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 =head1 SYNOPSIS
 
@@ -45,6 +46,7 @@ Create new normalised database object
 
   my $db = new WebPAC::Store(
        path => '/path/to/cache/ds/',
+       database => 'name',
        read_only => 1,
   );
 
@@ -116,13 +118,9 @@ sub path {
 
 =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
@@ -135,47 +133,46 @@ Returns hash or undef if cacheing is disabled or unavailable.
 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;
@@ -187,6 +184,7 @@ Store data_structure on disk.
 
   $db->save_ds(
        id => $ds->{000}->[0],
+       database => 'name',
        ds => $ds,
   );
 
@@ -211,7 +209,10 @@ sub save_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");
 
diff --git a/run.pl b/run.pl
index 81e2efe..292c42e 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -47,6 +47,7 @@ while (my ($database, $db_config) = each %{ $config->{databases} }) {
 
        my $db = new WebPAC::Store(
                path => $db_path,
+               database => $database,
        );
 
        my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
index 3ea803d..bc8024c 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 25;
+use Test::More tests => 29;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use blib;
@@ -22,7 +22,7 @@ my $no_log = 1;       # force no log output
 
 diag "NULL Store";
 
-ok($db = new WebPAC::Store( debug => $debug, ), "new");
+ok($db = new WebPAC::Store( debug => $debug ), "new");
 
 ok(! $db->path, "path");
 
@@ -38,7 +38,7 @@ ok(my $path = tempdir( CLEANUP => 1 ), "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");
 
@@ -50,7 +50,7 @@ ok($db->path( $path ), "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";
@@ -77,11 +77,19 @@ ok(my $ds2 = $db->load_ds( id => 1 ), "load_ds with 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;
 
index e714d5d..3c5f67d 100755 (executable)
@@ -44,6 +44,7 @@ ok(my $path = tempdir( CLEANUP => 1 ), "path");
 
 ok(my $db = new WebPAC::Store(
        path => $path,
+       database => '.',
 ), "new Store");
 
 ok(my $n = new WebPAC::Normalize::XML(