--- trunk/lib/WebPAC/Store.pm 2006/10/01 20:14:14 734 +++ trunk/lib/WebPAC/Store.pm 2006/10/02 09:31:25 735 @@ -14,11 +14,11 @@ =head1 VERSION -Version 0.13 +Version 0.14 =cut -our $VERSION = '0.13'; +our $VERSION = '0.14'; =head1 SYNOPSIS @@ -38,7 +38,7 @@ use WebPAC::Store; - my $foo = WebPAC::Store->new(); + my $store = WebPAC::Store->new(); ... =head1 FUNCTIONS @@ -47,7 +47,7 @@ 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, @@ -84,14 +84,14 @@ 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 @@ -136,7 +136,7 @@ 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, @@ -208,7 +208,7 @@ Store data_structure on disk. - $db->save_ds( + $store->save_ds( database => 'name', input => 'name', id => $ds->{000}->[0], @@ -260,7 +260,7 @@ Loads lookup hash from file - $data = $db->load_lookup( + $data = $store->load_lookup( database => $database, input => $input, key => $key, @@ -302,7 +302,7 @@ Save lookup data to file. - $db->save_lookup( + $store->save_lookup( database => $database, input => $input, key => $key, @@ -335,7 +335,91 @@ $log->info("saved lookup $path"); return 1; } else { - $log->logwarn("can't save lookup $database/", $args->{input}, "/", $args->{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 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 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; } }