/[webpac2]/trunk/lib/WebPAC/Normalize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Normalize.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 17 by dpavlin, Sun Jul 17 10:42:23 2005 UTC revision 18 by dpavlin, Sun Jul 17 14:53:37 2005 UTC
# Line 3  package WebPAC::Normalize; Line 3  package WebPAC::Normalize;
3  use warnings;  use warnings;
4  use strict;  use strict;
5  use Data::Dumper;  use Data::Dumper;
 use Storable;  
6    
7  =head1 NAME  =head1 NAME
8    
# Line 79  Create new normalisation object Line 78  Create new normalisation object
78                          return length($_);                          return length($_);
79                  }, ...                  }, ...
80          },          },
81          cache_data_structure => './cache/ds/',          db => $webpac_db_obj,
82          lookup_regex => $lookup->regex,          lookup_regex => $lookup->regex,
83    );    );
84    
85  Parametar C<filter> defines user supplied snippets of perl code which can  Parametar C<filter> defines user supplied snippets of perl code which can
86  be use with C<filter{...}> notation.  be use with C<filter{...}> notation.
87    
 Optional parameter C<cache_data_structure> defines path to directory  
 in which cache file for C<data_structure> call will be created.  
   
88  Recommended parametar C<lookup_regex> is used to enable parsing of lookups  Recommended parametar C<lookup_regex> is used to enable parsing of lookups
89  in structures.  in structures.
90    
# Line 99  sub new { Line 95  sub new {
95          my $self = {@_};          my $self = {@_};
96          bless($self, $class);          bless($self, $class);
97    
         $self->setup_cache_dir( $self->{'cache_data_structure'} );  
   
98          $self ? return $self : return undef;          $self ? return $self : return undef;
99  }  }
100    
 =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 $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'};  
         }  
 }  
   
101    
102  =head2 data_structure  =head2 data_structure
103    
# Line 172  sub data_structure { Line 126  sub data_structure {
126    
127          my $cache_file;          my $cache_file;
128    
129          if (my $cache_path = $self->{'cache_data_structure'}) {          if ($self->{'db'}) {
130                  my $id = $rec->{'000'};                  my @ds = $self->{'db'}->get_ds($rec);
131                  $id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o);                  return @ds if (@ds);
                 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'};  
                                         }  
                                 }  
                         }  
                 }  
132          }          }
133    
134          undef $self->{'currnet_filename'};          undef $self->{'currnet_filename'};
# Line 308  sub data_structure { Line 236  sub data_structure {
236    
237          }          }
238    
239          if ($cache_file) {          $self->{'db'}->put_gs(
240                  store {                  ds => \@ds,
241                          ds => \@ds,                  current_filename => $self->{'current_filename'},
242                          current_filename => $self->{'current_filename'},                  headline => $self->{'headline'},
243                          headline => $self->{'headline'},          ) if ($self->{'db'});
                 }, $cache_file;  
                 $log->debug("created storable cache file $cache_file");  
         }  
244    
245          return @ds;          return @ds;
246    

Legend:
Removed from v.17  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.26