/[webpac2]/trunk/run.pl
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/run.pl

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

revision 508 by dpavlin, Mon May 15 13:32:18 2006 UTC revision 539 by dpavlin, Thu Jun 29 15:29:32 2006 UTC
# Line 11  use WebPAC::Common 0.02; Line 11  use WebPAC::Common 0.02;
11  use WebPAC::Lookup;  use WebPAC::Lookup;
12  use WebPAC::Input 0.03;  use WebPAC::Input 0.03;
13  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
14  use WebPAC::Normalize::XML;  use WebPAC::Normalize;
 use WebPAC::Normalize::Set;  
15  use WebPAC::Output::TT;  use WebPAC::Output::TT;
16    use WebPAC::Validate;
17  use YAML qw/LoadFile/;  use YAML qw/LoadFile/;
18  use Getopt::Long;  use Getopt::Long;
19  use File::Path;  use File::Path;
# Line 42  limit loading to 100 records Line 42  limit loading to 100 records
42    
43  remove database and Hyper Estraier index before indexing  remove database and Hyper Estraier index before indexing
44    
45  =item --only=database_name  =item --only=database_name/input_filter
46    
47  reindex just single database (legacy name is --one)  reindex just single database (legacy name is --one)
48    
49    C</input_filter> is optional part which can be C<name>
50    or C<type> from input
51    
52  =item --config conf/config.yml  =item --config conf/config.yml
53    
54  path to YAML configuration file  path to YAML configuration file
55    
56  =item --force-set  =item --stats
57    
58  force conversion C<< normalize->path >> in C<config.yml> from  disable indexing and dump statistics about field and subfield
59  C<.xml> to C<.pl>  usage for each input
60    
61  =item --stats  =item --validate path/to/validation_file
62    
63  dump statistics about used fields and subfields in each input  turn on extra validation of imput records, see L<WebPAC::Validation>
64    
65  =back  =back
66    
# Line 69  my $limit; Line 72  my $limit;
72  my $clean = 0;  my $clean = 0;
73  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
74  my $debug = 0;  my $debug = 0;
75  my $only_db_name;  my $only_filter;
 my $force_set = 0;  
76  my $stats = 0;  my $stats = 0;
77    my $validate_path;
78    
79  GetOptions(  GetOptions(
80          "limit=i" => \$limit,          "limit=i" => \$limit,
81          "offset=i" => \$offset,          "offset=i" => \$offset,
82          "clean" => \$clean,          "clean" => \$clean,
83          "one=s" => \$only_db_name,          "one=s" => \$only_filter,
84          "only=s" => \$only_db_name,          "only=s" => \$only_filter,
85          "config" => \$config,          "config" => \$config,
86          "debug" => \$debug,          "debug" => \$debug,
         "force-set" => \$force_set,  
87          "stats" => \$stats,          "stats" => \$stats,
88            "validate=s" => \$validate_path,
89  );  );
90    
91  $config = LoadFile($config);  $config = LoadFile($config);
# Line 92  print "config = ",Dumper($config) if ($d Line 95  print "config = ",Dumper($config) if ($d
95  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
96    
97  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
98    $log->info( "-" x 79 );
99    
100    my $validate;
101    $validate = new WebPAC::Validate(
102            path => $validate_path,
103    ) if ($validate_path);
104    
105  my $use_indexer = $config->{use_indexer} || 'hyperestraier';  my $use_indexer = $config->{use_indexer} || 'hyperestraier';
106  $log->info("using $use_indexer indexing engine...");  if ($stats) {
107            $log->debug("option --stats disables update of indexing engine...");
108            $use_indexer = undef;
109    } else {
110            $log->info("using $use_indexer indexing engine...");
111    }
112    
113  my $total_rows = 0;  my $total_rows = 0;
114  my $start_t = time();  my $start_t = time();
115    
116    my @links;
117    my $indexer;
118    
119  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
120    
121          next if ($only_db_name && $database !~ m/$only_db_name/i);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
122            next if ($only_database && $database !~ m/$only_database/i);
123    
124          my $indexer;          if ($use_indexer) {
125                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
126                    $indexer_config->{database} = $database;
127                    $indexer_config->{clean} = $clean;
128                    $indexer_config->{label} = $db_config->{name};
129    
130                    if ($use_indexer eq 'hyperestraier') {
131    
132                            # open Hyper Estraier database
133                            use WebPAC::Output::Estraier '0.10';
134                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
135                    
136                    } elsif ($use_indexer eq 'kinosearch') {
137    
138                            # open KinoSearch
139                            use WebPAC::Output::KinoSearch;
140                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
141                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
142    
143          my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");                  } else {
144          $indexer_config->{database} = $database;                          $log->logdie("unknown use_indexer: $use_indexer");
145          $indexer_config->{clean} = $clean;                  }
         $indexer_config->{label} = $db_config->{name};  
   
         if ($use_indexer eq 'hyperestraier') {  
   
                 # open Hyper Estraier database  
                 use WebPAC::Output::Estraier '0.10';  
                 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );  
           
         } elsif ($use_indexer eq 'kinosearch') {  
   
                 # open KinoSearch  
                 use WebPAC::Output::KinoSearch;  
                 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});  
                 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );  
146    
147          } else {                  $log->logide("can't continue without valid indexer") unless ($indexer);
                 $log->logdie("unknown use_indexer: $use_indexer");  
148          }          }
149    
         $log->logide("can't continue without valid indexer") unless ($indexer);  
150    
151          #          #
152          # now WebPAC::Store          # now WebPAC::Store
# Line 138  while (my ($database, $db_config) = each Line 157  while (my ($database, $db_config) = each
157          my $db_path = $config->{webpac}->{db_path} . '/' . $database;          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
158    
159          if ($clean) {          if ($clean) {
160                  $log->info("creating new database $database in $db_path");                  $log->info("creating new database '$database' in $db_path");
161                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
162          } else {          } else {
163                  $log->debug("working on $database in $db_path");                  $log->info("working on database '$database' in $db_path");
164          }          }
165    
166          my $db = new WebPAC::Store(          my $db = new WebPAC::Store(
# Line 168  while (my ($database, $db_config) = each Line 187  while (my ($database, $db_config) = each
187    
188          foreach my $input (@inputs) {          foreach my $input (@inputs) {
189    
190                    next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
191    
192                  my $type = lc($input->{type});                  my $type = lc($input->{type});
193    
194                  die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));                  die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
195    
196                  my $lookup = new WebPAC::Lookup(                  my $lookup = new WebPAC::Lookup(
197                          lookup_file => $input->{lookup},                          lookup_file => $input->{lookup},
198                  );                  ) if ($input->{lookup});
199    
200                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
201    
202                  $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");                  $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
203                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
204                    );
205    
206                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
207                          module => $input_module,                          module => $input_module,
# Line 194  while (my ($database, $db_config) = each Line 217  while (my ($database, $db_config) = each
217                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
218                          path => $input->{path},                          path => $input->{path},
219                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
220                            %{ $input },
221                  );                  );
222    
223                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
224                  #       filter => { 'foo' => sub { shift } },                          @{ $input->{normalize} } : ( $input->{normalize} );
                         db => $db,  
                         lookup_regex => $lookup->regex,  
                         lookup => $lookup,  
                         prefix => $input->{name},  
                 );  
225    
226                  my $rules;                  foreach my $normalize (@norm_array) {
                 my $normalize_path = $input->{normalize}->{path};  
227    
228                  if ($force_set) {                          my $rules;
229                          my $new_norm_path = $normalize_path;                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
                         $new_norm_path =~ s/\.xml$/.pl/;  
                         if (-e $new_norm_path) {  
                                 $log->debug("--force-set replaced $normalize_path with $new_norm_path");  
                                 $normalize_path = $new_norm_path;  
                         } else {  
                                 $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");  
                         }  
                 }  
230    
231                  if ($normalize_path =~ m/\.xml$/i) {                          $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
                         $n->open(  
                                 tag => $input->{normalize}->{tag},  
                                 xml_file => $normalize_path,  
                         );  
                 } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {  
                         $n->open_yaml(  
                                 path => $normalize_path,  
                                 tag => $input->{normalize}->{tag},  
                         );  
                 } elsif ($normalize_path =~ m/\.(?:pl)$/i) {  
                         $n = undef;  
                         $log->info("using WebPAC::Normalize::Set to process $normalize_path");  
                         $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";  
                 }  
232    
233                  foreach my $pos ( 0 ... $input_db->size ) {                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
234    
235                          my $row = $input_db->fetch || next;                          $log->info("Using $normalize_path for normalization...");
236    
237                          my $mfn = $row->{'000'}->[0];                          # reset position in database
238                            $input_db->seek(1);
239    
240                          if (! $mfn || $mfn !~ m#^\d+$#) {                          foreach my $pos ( 0 ... $input_db->size ) {
                                 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
                                 $mfn = $pos;  
                                 push @{ $row->{'000'} }, $pos;  
                         }  
241    
242                                                                    my $row = $input_db->fetch || next;
243                          my $ds;  
244                          if ($n) {                                  my $mfn = $row->{'000'}->[0];
245                                  $ds = $n->data_structure($row);  
246                          } else {                                  if (! $mfn || $mfn !~ m#^\d+$#) {
247                                  $ds = WebPAC::Normalize::Set::data_structure(                                          $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
248                                            $mfn = $pos;
249                                            push @{ $row->{'000'} }, $pos;
250                                    }
251    
252    
253                                    if ($validate) {
254                                            my @errors = $validate->validate_errors( $row );
255                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
256                                    }
257    
258                                            
259                                    my $ds = WebPAC::Normalize::data_structure(
260                                          row => $row,                                          row => $row,
261                                          rules => $rules,                                          rules => $rules,
262                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
263                                  );                                  );
264    
265                                  $db->save_ds(                                  $db->save_ds(
266                                          id => $mfn,                                          id => $mfn,
267                                          ds => $ds,                                          ds => $ds,
268                                          prefix => $input->{name},                                          prefix => $input->{name},
269                                  ) if ($ds);                                  ) if ($ds && !$stats);
                         }  
270    
271                          $indexer->add(                                  $indexer->add(
272                                  id => $input->{name} . "/" . $mfn,                                          id => $input->{name} . "/" . $mfn,
273                                  ds => $ds,                                          ds => $ds,
274                                  type => $config->{$use_indexer}->{type},                                          type => $config->{$use_indexer}->{type},
275                          );                                  ) if ($indexer);
276    
277                          $total_rows++;                                  $total_rows++;
278                  }                          }
279    
280                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                          $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
281    
282          };                  };
283    
284          eval { $indexer->finish } if ($indexer->can('finish'));          }
285    
286            eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
287    
288          my $dt = time() - $start_t;          my $dt = time() - $start_t;
289          $log->info("$total_rows records indexed in " .          $log->info("$total_rows records ", $indexer ? "indexed " : "",
290                  sprintf("%.2f sec [%.2f rec/sec]",                  sprintf("in %.2f sec [%.2f rec/sec]",
291                          $dt, ($total_rows / $dt)                          $dt, ($total_rows / $dt)
292                  )                  )
293          );          );
# Line 292  while (my ($database, $db_config) = each Line 298  while (my ($database, $db_config) = each
298          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY') {
299                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
300                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
301                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
302                                  $indexer->add_link(                                  push @links, {
303                                          from => $database,                                          from => $database,
304                                          to => $link->{to},                                          to => $link->{to},
305                                          credit => $link->{credit},                                          credit => $link->{credit},
306                                  );                                  };
307                          } else {                          } else {
308                                  $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");                                  $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
309                          }                          }
# Line 306  while (my ($database, $db_config) = each Line 312  while (my ($database, $db_config) = each
312    
313  }  }
314    
315    foreach my $link (@links) {
316            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
317            $indexer->add_link( %{ $link } );
318    }

Legend:
Removed from v.508  
changed lines
  Added in v.539

  ViewVC Help
Powered by ViewVC 1.1.26