--- trunk/lib/WebPAC/Normalize.pm 2005/07/17 10:42:23 15 +++ trunk/lib/WebPAC/Normalize.pm 2005/11/12 21:21:50 38 @@ -2,8 +2,8 @@ use warnings; use strict; +use base 'WebPAC::Common'; use Data::Dumper; -use Storable; =head1 NAME @@ -79,18 +79,17 @@ return length($_); }, ... }, - cache_data_structure => './cache/ds/', + db => $db_obj, lookup_regex => $lookup->regex, + lookup => $lookup_obj, ); Parametar C defines user supplied snippets of perl code which can be use with C notation. -Optional parameter C defines path to directory -in which cache file for C call will be created. - Recommended parametar C is used to enable parsing of lookups -in structures. +in structures. If you pass this parametar, you must also pass C +which is C object. =cut @@ -99,49 +98,20 @@ my $self = {@_}; bless($self, $class); - $self->setup_cache_dir( $self->{'cache_data_structure'} ); - - $self ? return $self : return undef; -} - -=head2 setup_cache_dir - -Check if specified cache directory exist, and if not, disable caching. - - $setup_cache_dir('./cache/ds/'); - -If you pass false or zero value to this function, it will disable -cacheing. - -=cut - -sub setup_cache_dir { - my $self = shift; - - my $dir = shift; + my $r = $self->{'lookup_regex'} ? 1 : 0; + my $l = $self->{'lookup'} ? 1 : 0; my $log = $self->_get_logger(); - if ($dir) { - my $msg; - if (! -e $dir) { - $msg = "doesn't exist"; - } elsif (! -d $dir) { - $msg = "is not directory"; - } elsif (! -w $dir) { - $msg = "not writable"; - } - - if ($msg) { - undef $self->{'cache_data_structure'}; - $log->warn("cache_data_structure $dir $msg, disabling..."); - } else { - $log->debug("using cache dir $dir"); - } - } else { - $log->debug("disabling cache"); - undef $self->{'cache_data_structure'}; + # those two must be in pair + if ( ($r & $l) != ($r || $l) ) { + my $log = $self->_get_logger(); + $log->logdie("lookup_regex and lookup must be in pair"); } + + $log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); + + $self ? return $self : return undef; } @@ -172,35 +142,11 @@ my $cache_file; - if (my $cache_path = $self->{'cache_data_structure'}) { - my $id = $rec->{'000'}; - $id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); - unless (defined($id)) { - $log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); - undef $self->{'cache_data_structure'}; - } else { - $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_data_structure $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); - undef $self->{'cache_data_structure'}; - } - } - } - } + if ($self->{'db'}) { + my @ds = $self->{'db'}->load_ds($rec); + $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) }); + return @ds if ($#ds > 0); + $log->debug("cache miss, creating"); } undef $self->{'currnet_filename'}; @@ -225,7 +171,10 @@ #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { - my $format = $tag->{'value'} || $tag->{'content'}; + my $format; + + $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); + $format = $tag->{'value'} || $tag->{'content'}; $log->debug("format: $format"); @@ -308,14 +257,13 @@ } - if ($cache_file) { - store { - ds => \@ds, - current_filename => $self->{'current_filename'}, - headline => $self->{'headline'}, - }, $cache_file; - $log->debug("created storable cache file $cache_file"); - } + $self->{'db'}->save_ds( + ds => \@ds, + current_filename => $self->{'current_filename'}, + headline => $self->{'headline'}, + ) if ($self->{'db'}); + + $log->debug("ds: ", sub { Dumper(@ds) }); return @ds; @@ -513,7 +461,11 @@ } # do we have lookups? if ($self->{'lookup'}) { - return $self->lookup($format); + if ($self->{'lookup'}->can('lookup')) { + return $self->{'lookup'}->lookup($format); + } else { + $log->warn("Have lookup object but can't invoke lookup method"); + } } else { return $format; } @@ -639,7 +591,7 @@ $log->debug("using format $name [$format] on $data to produce: $out"); if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { - return $self->lookup($out); + return $self->{'lookup'}->lookup($out); } else { return $out; }