2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
use base 'WebPAC::Common'; |
6 |
use Data::Dumper; |
use Data::Dumper; |
|
use Storable; |
|
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
79 |
return length($_); |
return length($_); |
80 |
}, ... |
}, ... |
81 |
}, |
}, |
82 |
cache_data_structure => './cache/ds/', |
db => $db_obj, |
83 |
lookup_regex => $lookup->regex, |
lookup_regex => $lookup->regex, |
84 |
|
lookup => $lookup_obj, |
85 |
); |
); |
86 |
|
|
87 |
Parametar C<filter> defines user supplied snippets of perl code which can |
Parametar C<filter> defines user supplied snippets of perl code which can |
88 |
be use with C<filter{...}> notation. |
be use with C<filter{...}> notation. |
89 |
|
|
|
Optional parameter C<cache_data_structure> defines path to directory |
|
|
in which cache file for C<data_structure> call will be created. |
|
|
|
|
90 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
91 |
in structures. |
in structures. If you pass this parametar, you must also pass C<lookup> |
92 |
|
which is C<WebPAC::Lookup> object. |
93 |
|
|
94 |
=cut |
=cut |
95 |
|
|
98 |
my $self = {@_}; |
my $self = {@_}; |
99 |
bless($self, $class); |
bless($self, $class); |
100 |
|
|
101 |
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
my $r = $self->{'lookup_regex'} ? 1 : 0; |
102 |
|
my $l = $self->{'lookup'} ? 1 : 0; |
|
$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; |
|
103 |
|
|
104 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
105 |
|
|
106 |
if ($dir) { |
# those two must be in pair |
107 |
my $msg; |
if ( ($r & $l) != ($r || $l) ) { |
108 |
if (! -e $dir) { |
my $log = $self->_get_logger(); |
109 |
$msg = "doesn't exist"; |
$log->logdie("lookup_regex and lookup must be in pair"); |
|
} 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'}; |
|
110 |
} |
} |
111 |
|
|
112 |
|
$log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); |
113 |
|
|
114 |
|
$self ? return $self : return undef; |
115 |
} |
} |
116 |
|
|
117 |
|
|
142 |
|
|
143 |
my $cache_file; |
my $cache_file; |
144 |
|
|
145 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
if ($self->{'db'}) { |
146 |
my $id = $rec->{'000'}; |
my @ds = $self->{'db'}->load_ds($rec); |
147 |
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) }); |
148 |
unless (defined($id)) { |
return @ds if ($#ds > 0); |
149 |
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
$log->debug("cache miss, creating"); |
|
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'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
150 |
} |
} |
151 |
|
|
152 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
171 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
172 |
|
|
173 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
174 |
my $format = $tag->{'value'} || $tag->{'content'}; |
my $format; |
175 |
|
|
176 |
|
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
177 |
|
$format = $tag->{'value'} || $tag->{'content'}; |
178 |
|
|
179 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
180 |
|
|
257 |
|
|
258 |
} |
} |
259 |
|
|
260 |
if ($cache_file) { |
$self->{'db'}->save_ds( |
261 |
store { |
ds => \@ds, |
262 |
ds => \@ds, |
current_filename => $self->{'current_filename'}, |
263 |
current_filename => $self->{'current_filename'}, |
headline => $self->{'headline'}, |
264 |
headline => $self->{'headline'}, |
) if ($self->{'db'}); |
265 |
}, $cache_file; |
|
266 |
$log->debug("created storable cache file $cache_file"); |
$log->debug("ds: ", sub { Dumper(@ds) }); |
|
} |
|
267 |
|
|
268 |
return @ds; |
return @ds; |
269 |
|
|
461 |
} |
} |
462 |
# do we have lookups? |
# do we have lookups? |
463 |
if ($self->{'lookup'}) { |
if ($self->{'lookup'}) { |
464 |
return $self->lookup($format); |
if ($self->{'lookup'}->can('lookup')) { |
465 |
|
return $self->{'lookup'}->lookup($format); |
466 |
|
} else { |
467 |
|
$log->warn("Have lookup object but can't invoke lookup method"); |
468 |
|
} |
469 |
} else { |
} else { |
470 |
return $format; |
return $format; |
471 |
} |
} |
591 |
$log->debug("using format $name [$format] on $data to produce: $out"); |
$log->debug("using format $name [$format] on $data to produce: $out"); |
592 |
|
|
593 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
594 |
return $self->lookup($out); |
return $self->{'lookup'}->lookup($out); |
595 |
} else { |
} else { |
596 |
return $out; |
return $out; |
597 |
} |
} |