--- trunk/run.pl 2005/12/18 21:06:51 287 +++ trunk/run.pl 2006/04/17 15:09:54 430 @@ -13,36 +13,99 @@ use WebPAC::Store 0.03; use WebPAC::Normalize::XML; use WebPAC::Output::TT; -use WebPAC::Output::Estraier 0.05; use YAML qw/LoadFile/; -use LWP::Simple; +use Getopt::Long; +use File::Path; +use Time::HiRes qw/time/; -my $limit = shift @ARGV; +=head1 NAME -my $config = LoadFile('conf/config.yml'); +run.pl - start WebPAC indexing -print "config = ",Dumper($config); +B + +Options: + +=over 4 + +=item --offset 42 + +start loading (all) databases at offset 42 + +=item --limit 100 + +limit loading to 100 records + +=item --clean + +remove database and Hyper Estraier index before indexing + +=item --only=database_name + +reindex just single database (legacy name is --one) + +=item --config conf/config.yml + +path to YAML configuration file + +=back + +=cut + +my $offset; +my $limit; + +my $clean = 0; +my $config = 'conf/config.yml'; +my $debug = 0; +my $only_db_name; + +GetOptions( + "limit=i" => \$limit, + "offset=i" => \$offset, + "clean" => \$clean, + "one=s" => \$only_db_name, + "only=s" => \$only_db_name, + "config" => \$config, + "debug" => \$debug, +); + +$config = LoadFile($config); + +print "config = ",Dumper($config) if ($debug); die "no databases in config file!\n" unless ($config->{databases}); +my $use_indexer = $config->{use_indexer} || 'hyperestraier'; my $total_rows = 0; +my $start_t = time(); while (my ($database, $db_config) = each %{ $config->{databases} }) { + next if ($only_db_name && $database !~ m/$only_db_name/i); + my $log = _new WebPAC::Common()->_get_logger(); - # - # open Hyper Estraier database - # + my $indexer; - my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration"); - $est_config->{database} = $database; + if ($use_indexer eq 'hyperestraier') { - $log->info("using HyperEstraier URL $est_config->{masterurl}"); + # + # open Hyper Estraier database + # - my $est = new WebPAC::Output::Estraier( - %{ $est_config }, - ); + use WebPAC::Output::Estraier '0.10'; + my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration"); + $est_config->{database} = $database; + $est_config->{clean} = $clean; + $est_config->{label} = $db_config->{name}; + + $indexer = new WebPAC::Output::Estraier( %{ $est_config } ); + } else { + $log->logdie("unknown use_indexer: $use_indexer"); + } + + $log->logide("can't continue without valid indexer") unless ($indexer); # # now WebPAC::Store @@ -52,12 +115,17 @@ my $db_path = $config->{webpac}->{db_path} . '/' . $database; - $log->info("working on $database in $db_path"); + if ($clean) { + $log->info("creating new database $database in $db_path"); + rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); + } else { + $log->debug("working on $database in $db_path"); + } my $db = new WebPAC::Store( path => $db_path, database => $database, - debug => 1, + debug => $debug, ); @@ -88,13 +156,15 @@ my $input_module = $config->{webpac}->{inputs}->{$type}; - $log->info("working on input $input->{path} [$input->{type}] using $input_module"); + $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'"); my $input_db = new WebPAC::Input( module => $input_module, code_page => $config->{webpac}->{webpac_encoding}, - limit_mfn => $input->{limit}, + limit => $limit || $input->{limit}, + offset => $offset, lookup => $lookup, + recode => $input->{recode}, ); $log->logdie("can't create input using $input_module") unless ($input); @@ -125,18 +195,24 @@ ); } - for ( 0 ... $input_db->size ) { + foreach my $pos ( 0 ... $input_db->size ) { my $row = $input_db->fetch || next; - my $mfn = $row->{'000'}->[0] || die "can't find MFN"; + my $mfn = $row->{'000'}->[0]; + + if (! $mfn || $mfn !~ m#^\d+$#) { + $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); + $mfn = $pos; + push @{ $row->{'000'} }, $pos; + } my $ds = $n->data_structure($row); - $est->add( - id => $input->{name} . "#" . $mfn, + $indexer->add( + id => $input->{name} . "/" . $mfn, ds => $ds, - type => $config->{hyperestraier}->{type}, + type => $config->{$use_indexer}->{type}, ); $total_rows++; @@ -144,20 +220,29 @@ }; - $log->info("$total_rows records indexed"); + my $dt = time() - $start_t; + $log->info("$total_rows records indexed in " . + sprintf("%.2f sec [%.2f rec/sec]", + $dt, ($total_rows / $dt) + ) + ); - # - # add Hyper Estraier links to other databases - # - if (ref($db_config->{links}) eq 'ARRAY') { - foreach my $link (@{ $db_config->{links} }) { - $log->info("adding link $database -> $link->{to} [$link->{credit}]"); - $est->add_link( - from => $database, - to => $link->{to}, - credit => $link->{credit}, - ); + if ($use_indexer eq 'hyperestraier') { + # + # add Hyper Estraier links to other databases + # + if (ref($db_config->{links}) eq 'ARRAY') { + foreach my $link (@{ $db_config->{links} }) { + $log->info("adding link $database -> $link->{to} [$link->{credit}]"); + $indexer->add_link( + from => $database, + to => $link->{to}, + credit => $link->{credit}, + ); + } } + } else { + $log->warn("links not implemented for $use_indexer"); } }