--- Webpacus/lib/Webpacus/Model/WebPAC.pm 2006/01/22 00:44:42 381 +++ Webpacus/lib/Webpacus/Model/WebPAC.pm 2006/01/22 02:52:24 382 @@ -7,7 +7,7 @@ Catalyst::Model /; use WebPAC::Store 0.08; -use WebPAC::Search::Estraier 0.05; +use Search::Estraier 0.04; use File::Slurp; use Time::HiRes qw/time/; use Encode qw/encode decode from_to/; @@ -74,7 +74,17 @@ $est_cfg->{database} = $defaultnode; } - $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); + my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database}; + + $log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}"); + + $self->{est_node} = Search::Estraier::Node->new( + url => $url, + user => $est_cfg->{user}, + passwd => $est_cfg->{passwd}, + ); + + $log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); # save config parametars in object foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) { @@ -101,7 +111,7 @@ "'" ); - $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config"); + $self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config"); # create Template toolkit instance $self->{'tt'} = Template->new( @@ -185,74 +195,125 @@ $log->warn("using default search depth $default"); } - my @results = $self->{est}->search( %{ $args } ); + $log->debug("searching for maximum $args->{max} results using depth $args->{depth}"); + + # + # construct condition for Hyper Estraier + # + my $cond = Search::Estraier::Condition->new(); + if ( ref($args->{add_attr}) eq 'ARRAY' ) { + $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); + map { + $cond->add_attr( _convert( $_ ) ); + $log->debug(" + $_"); + } @{ $args->{add_attr} }; + }; + + $cond->set_phrase( $query ) if ($query); + $cond->set_options( $args->{options} ) if ($args->{options}); + $cond->set_order( $args->{order} ) if ($args->{order}); + + my $max = $args->{max} || 7; + my $page = $args->{page} || 1; + if ($page < 1) { + $log->warn("page number $page < 1"); + $page = 1; + } $times->{est} += time() - $t; - my $hits = $#results + 1; + $cond->set_max( $page * $max ); - $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); + my $result = $self->{est_node}->search($cond, ( $args->{depth} || 0 )); + my $hits = $result->doc_num; - # just return results? - return @results unless ($args->{'template'}); + $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); # - # construct HTML results + # fetch results # - my @html_results; + my @results; - for my $i ( 0 .. $#results ) { + for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { - my ($database, $prefix, $id); - if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { - ($database, $prefix,$id) = ($1,$2,$3); - } else { - $log->warn("can't decode database/prefix/id from " . $results[$i]->{'@uri'}); + $t = time(); + + #$log->debug("get_doc($i)"); + my $doc = $result->get_doc( $i ); + if (! $doc) { + $log->warn("can't find result $i"); next; } - #$log->debug("load_ds( id => $id, prefix => '$prefix' )"); - - $t = time(); + my $hash; - my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); - if (! $ds) { - $log->error("can't load_ds( ${database}/${prefix}/${id} )"); - next; + foreach my $attr (@{ $args->{get_attr} }) { + my $val = $doc->attr( $attr ); + #$log->debug("attr $attr = ", $val || 'undef'); + $hash->{$attr} = $val if (defined($val)); } - $times->{db} += time() - $t; + $times->{hash} += time() - $t; - #$log->debug( "ds = " . Dumper( \@html_results ) ); + next unless ($hash); - $t = time(); + if (! $args->{'template'}) { + push @results, $hash; + } else { + my ($database, $prefix, $id); - my $html = $self->apply( - template => $template_filename, - data => $ds, - record_uri => "${database}/${prefix}/${id}", - config => $self->{databases}->{$database}, - ); + if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { + ($database, $prefix,$id) = ($1,$2,$3); + } else { + $log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); + next; + } - $times->{out} += time() - $t; + #$log->debug("load_ds( id => $id, prefix => '$prefix' )"); - $t = time(); + $t = time(); + + my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); + if (! $ds) { + $log->error("can't load_ds( ${database}/${prefix}/${id} )"); + next; + } + + $times->{db} += time() - $t; - $html = decode($self->{webpac_encoding}, $html); + #$log->debug( "ds = " . Dumper( \@html_results ) ); - push @html_results, $html; + $t = time(); + + my $html = $self->apply( + template => $template_filename, + data => $ds, + record_uri => "${database}/${prefix}/${id}", + config => $self->{databases}->{$database}, + ); + + $times->{apply} += time() - $t; + + $t = time(); + + $html = decode($self->{webpac_encoding}, $html); + + $times->{decode} += time() - $t; + + push @results, $html; + } } - #$log->debug( '@html_results = ' . Dumper( \@html_results ) ); + $log->debug( '@results = ' . Dumper( \@results ) ); $log->debug( sprintf( - "duration breakdown: store %.6fs, apply %.6fs, total: %.6fs", - $times->{db}, $times->{out}, time() - $search_start_t, + "duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", + $times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, ) ); - return \@html_results; + return \@results; } =head2 record @@ -385,7 +446,7 @@ my $log = $self->{log} || die "no log?"; foreach my $a (qw/template data/) { - $log->logconfess("need $a") unless ($args->{$a}); + $log->fatal("need $a") unless ($args->{$a}); } =head3 tt_filter_type