r11539@llin: dpavlin | 2005-12-05 16:18:58 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:48:00 +0000 (17:48 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2005 17:48:00 +0000 (17:48 +0000)
 WebPAC::Store now uses prefix in load_ds and save_ds and requires database
 when created with new [0.06]

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

Makefile.PL
TODO
lib/WebPAC/Store.pm
t/4-store.t

index fca110f..39a5043 100644 (file)
@@ -54,6 +54,7 @@ sf:
 run:
        rm -f log
        test ! -e conf/config.yml && ln -s /data/Webpacus/config.yml conf/ || true
+       test -d db && rm -Rf db/*
        #estcall raw -auth admin admin 'http://localhost:1978/master?action=nodedel&name=webpac2' || true
        #estcall raw -auth admin admin 'http://localhost:1978/master?action=nodeadd&name=webpac2&label=WebPAC%20test' || true
        ./run.pl
diff --git a/TODO b/TODO
index e9ba4a0..51985ca 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,3 +1,4 @@
 - add multiple databases
+- delete unused files in database directories
 - scoring for various fields in input/*.xml
 - write pure perl Search::HyperEstraier
index 8403240..b15d42e 100644 (file)
@@ -14,11 +14,11 @@ WebPAC::Store - Store normalized data on disk
 
 =head1 VERSION
 
-Version 0.05
+Version 0.06
 
 =cut
 
-our $VERSION = '0.05';
+our $VERSION = '0.06';
 
 =head1 SYNOPSIS
 
@@ -56,6 +56,8 @@ in which cache file for C<data_structure> call will be created.
 If called with C<read_only> it will not disable caching if
 called without write permission (but will die on C<save_ds>).
 
+Mandatory parametar C<database> is used as subdirectory in database directory.
+
 =cut
 
 sub new {
@@ -63,6 +65,12 @@ sub new {
         my $self = {@_};
         bless($self, $class);
 
+       my $log = $self->_get_logger();
+
+       foreach my $p (qw/path database/) {
+               $log->logconfess("need $p") unless ($self->{$p});
+       }
+
        $self->path( $self->{'path'} );
 
        $self ? return $self : return undef;
@@ -120,12 +128,15 @@ sub path {
 
 Retrive from disk one data_structure records usually using field 000 as key
 
-  my $ds = $db->load_ds( id => 42, database => 'name' );
+  my $ds = $db->load_ds( id => 42, prefix => 'name' );
 
 This function will also perform basic sanity checking on returned
 data and disable caching if data is corrupted (or changed since last
 update).
 
+C<prefix> is used to differenciate different source input databases
+which are indexed in same database.
+
 Returns hash or undef if cacheing is disabled or unavailable.
 
 =cut
@@ -148,14 +159,19 @@ sub load_ds {
        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+$/);
+       $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
 
-       my $database = $args->{database} || $self->{database};
+       my $database = $self->{database};
+       my $prefix = $args->{prefix} || '';
 
        $log->logconfess("can't find database name") unless ($database);
 
-       my $cache_file = "$cache_path/$database#$id";
-       $cache_file =~ s#//#/#g;
+       my $cache_file = "$cache_path/$prefix/$database#$id";
+       $cache_file =~ s#//#/#go;
+
+open(my $fh, '>>', '/tmp/foo');
+print $fh "$cache_file\n";
+close($fh);
 
        $log->debug("using cache_file $cache_file");
 
@@ -184,7 +200,7 @@ Store data_structure on disk.
 
   $db->save_ds(
        id => $ds->{000}->[0],
-       database => 'name',
+       prefix => 'name',
        ds => $ds,
   );
 
@@ -209,10 +225,13 @@ sub save_ds {
                $log->logconfess("need $f") unless ($arg->{$f});
        }
 
-       my $database = $arg->{database} ||  $self->{database};
+       my $database = $self->{database};
        $log->logconfess("can't find database name") unless ($database);
 
-       my $cache_file = $self->{path} . '/' . $database . '#' . $arg->{id};
+       my $prefix = $arg->{prefix} || '';
+
+       my $cache_file = $self->{path} . "/$prefix/$database#" . $arg->{id};
+       $cache_file =~ s#//#/#go;
 
        $log->debug("creating storable cache file $cache_file");
 
index bc8024c..23d36a2 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 29;
+use Test::More tests => 31;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use blib;
@@ -22,7 +22,11 @@ my $no_log = 1;      # force no log output
 
 diag "NULL Store";
 
-ok($db = new WebPAC::Store( debug => $debug ), "new");
+throws_ok { new WebPAC::Store() } qr/path/, 'new without path';
+
+throws_ok { new WebPAC::Store( path => '/tmp' ) } qr/database/, 'new without database';
+
+ok($db = new WebPAC::Store( path => '/tmp', database => '.', debug => $debug ), "new");
 
 ok(! $db->path, "path");
 
@@ -85,9 +89,9 @@ 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");
+ok($db = new WebPAC::Store( path => $path, database => '.', debug => $debug, no_log => $no_log ), "new");
 
-throws_ok { $ds2 = $db->load_ds( id => 1 ) } qr/database/, "load_ds without database";
+ok(! $db->load_ds( id => 1, prefix => 'foobar' ), "load_ds with invalid prefix");
 
 ok($ds2 = $db->load_ds( id => 1, database => '.' ), "load_ds");