r1263@llin: dpavlin | 2007-05-27 14:36:00 +0200
[webpac2] / lib / WebPAC / Store.pm
index 46fc419..e9bbebc 100644 (file)
@@ -6,7 +6,7 @@ use strict;
 use base 'WebPAC::Common';
 use Storable;
 use File::Path;
-use Data::Dumper;
+use Data::Dump qw/dump/;
 
 =head1 NAME
 
@@ -14,11 +14,11 @@ WebPAC::Store - Store WebPAC data on disk
 
 =head1 VERSION
 
-Version 0.10
+Version 0.14
 
 =cut
 
-our $VERSION = '0.10';
+our $VERSION = '0.14';
 
 =head1 SYNOPSIS
 
@@ -38,7 +38,7 @@ For now, this is a prototype version.
 
     use WebPAC::Store;
 
-    my $foo = WebPAC::Store->new();
+    my $store = WebPAC::Store->new();
     ...
 
 =head1 FUNCTIONS
@@ -47,7 +47,7 @@ For now, this is a prototype version.
 
 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,
@@ -59,7 +59,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.
+Optional parametar C<database> will be used used as subdirectory in path if no
+database in specified when calling other functions.
 
 =cut
 
@@ -70,7 +71,7 @@ sub new {
 
        my $log = $self->_get_logger();
 
-       foreach my $p (qw/path database/) {
+       foreach my $p (qw/path/) {
                $log->logconfess("need $p") unless ($self->{$p});
        }
 
@@ -83,12 +84,14 @@ sub new {
 
 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 example C<< $db->{path} >> to get current cache path.
+You can also call this function to get current cache path.
+
+ my $cache_path = $store->path;
 
 =cut
 
@@ -96,6 +99,8 @@ sub path {
        my $self = shift;
 
        my $dir = shift;
+       
+       return $self->{path} unless defined($dir);
 
        my $log = $self->_get_logger();
 
@@ -131,13 +136,17 @@ sub path {
 
 Retrive from disk one data_structure records usually using field 000 as key
 
-  my $ds = $db->load_ds( id => 42, prefix => 'name', database => 'ps' );
+  my $ds = $store->load_ds(
+               database => 'ps',
+               input => 'name',
+               id => 42,
+  );
 
 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
+C<input> is used to differenciate different source input databases
 which are indexed in same database.
 
 C<database> if B<optional> argument which will override database name used when creating
@@ -167,12 +176,11 @@ sub load_ds {
        $log->logconfess("got hash, but without id") unless (defined($id));
        $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
 
-       my $database = $args->{database} || $self->{database};
-       my $prefix = $args->{prefix} || '';
+       my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
 
-       $log->logconfess("can't find database name") unless ($database);
+       my $input = $args->{input} || '';
 
-       my $cache_file = "$cache_path/$database/$prefix/$id";
+       my $cache_file = "$cache_path/ds/$database/$input/$id";
        $cache_file =~ s#//#/#go;
 
        $log->debug("using cache_file $cache_file");
@@ -200,15 +208,14 @@ sub load_ds {
 
 Store data_structure on disk.
 
-  $db->save_ds(
+  $store->save_ds(
+       database => 'name',
+       input => 'name',
        id => $ds->{000}->[0],
-       prefix => 'name',
        ds => $ds,
   );
 
-B<Totally broken, but fast.>
-
-Depends on filename generated by C<load_ds>.
+C<database> and C<input> are optional.
 
 =cut
 
@@ -219,60 +226,204 @@ sub save_ds {
 
        return unless($self->{'path'});
 
-       my $arg = {@_};
+       my $args = {@_};
 
        my $log = $self->_get_logger;
+       $log->debug("save_ds arguments:", sub { dump( \@_ ) });
 
        foreach my $f (qw/id ds/) {
-               $log->logconfess("need $f") unless ($arg->{$f});
+               $log->logconfess("need $f") unless (defined($args->{$f}));
        }
 
-       my $database = $self->{database};
-       $log->logconfess("can't find database name") unless ($database);
+       my $database = $args->{database} || $self->{database};
+       $log->logconfess("can't find database name") unless (defined($database));
 
-       my $prefix = $arg->{prefix} || '';
+       my $input = $args->{input} || '';
 
-       my $cache_file = $self->{path} . '/' . $prefix . '/';
+       my $cache_file = $self->{path} . "/ds/$database/$input/";
        $cache_file =~ s#//#/#go;
 
        mkpath($cache_file) unless (-d $cache_file);
 
-       $cache_file .= $arg->{id};
+       $cache_file .= $args->{id};
 
        $log->debug("creating storable cache file $cache_file");
 
        return store {
-               ds => $arg->{ds},
-               id => $arg->{id},
+               ds => $args->{ds},
+               id => $args->{id},
        }, $cache_file;
 
 }
 
+=head2 load_lookup
+
+Loads lookup hash from file
+
+  $data = $store->load_lookup(
+       database => $database,
+       input => $input,
+       key => $key,
+  );
+
+C<database> is optional.
+
+=cut
+
+sub load_lookup {
+       my $self = shift;
+       my $args = {@_};
+
+       my $log = $self->_get_logger;
+
+       foreach my $r (qw/input key/) {
+               $log->logconfess("need '$r'") unless defined($args->{$r});
+       }
+
+       my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
+
+       my $path = $self->{path} . "/lookup/$database/" . $args->{input} . '/' . $args->{key};
+
+       if (! -e $path) {
+               $log->error("lookup $path doesn't exist, lookups will be disabled. Try re-indexing $database/", $args->{input});
+               return;
+       }
+
+       if (my $data = retrieve($path)) {
+               $log->info("loaded lookup $path");
+               return $data;
+       } else {
+               $log->logwarn("can't load lookup $database/", $args->{input}, "/", $args->{key}, " from $path: $!");
+               return undef;
+       }
+}
+
 =head2 save_lookup
 
-  $db->save_lookup( $database, $input, $key, $lookup );
+Save lookup data to file.
+
+  $store->save_lookup(
+       database => $database,
+       input => $input,
+       key => $key,
+       data => $lookup,
+  );
+
+C<database> is optional.
 
 =cut
 
 sub save_lookup {
        my $self = shift;
-       my ($database, $input, $key, $lookup) = @_;
+       my $args = {@_};
 
        my $log = $self->_get_logger;
 
-       my $path = $self->{'path'} . "/lookup/$input";
+       foreach my $r (qw/input key data/) {
+               $log->logconfess("need '$r'") unless defined($args->{$r});
+       }
+
+       my $database = $args->{database} || $self->{database} || $log->logconfess("no database?");
+
+       my $path = $self->{path} . "/lookup/$database/" . $args->{input};
 
        mkpath($path) unless (-d $path);
 
-       $path .= "/$key";
+       $path .= "/" . $args->{key};
+
+       my $t = time();
 
-       if (store $lookup, $path) {
-               $log->info("saved lookup $path");
+       if (store $args->{data}, $path) {
+               $log->info(sprintf("saved lookup $path in %.2fs", time() - $t));
+               return 1;
        } else {
-               $log->logwarn("can't store lookup $database/$input/$key in $path: $!");
+               $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 row to $path: $!");
+               return undef;
+       }
 }
 
 
@@ -282,7 +433,7 @@ Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
+Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the same terms as Perl itself.