From: Dobrica Pavlinusic Date: Fri, 7 Jan 2011 19:12:20 +0000 (+0000) Subject: move search in Search::Estraier X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=commitdiff_plain;h=d7734a81d95ceb58671da223a1f92aa79c628b02 move search in Search::Estraier --- diff --git a/lib/BackupPC/Search/Estraier.pm b/lib/BackupPC/Search/Estraier.pm index ff2b287..a9f9cae 100644 --- a/lib/BackupPC/Search/Estraier.pm +++ b/lib/BackupPC/Search/Estraier.pm @@ -2,7 +2,7 @@ package BackupPC::Search::Estraier; use warnings; use strict; -use Search::Estraier; +use Search::Estraier 0.04; my $debug = $ENV{DEBUG} || 0; @@ -67,4 +67,76 @@ sub commit { warn "# commit not needed"; } +my $sort_mapping = { + share_d => 'sname STRD', + share_a => 'sname STRA', + path_d => 'filepath STRD', + path_a => 'filepath STRA', + num_d => 'backupnum NUMD', + num_a => 'backupnum NUMA', + size_d => 'size NUMD', + size_a => 'size NUMA', + date_d => 'date NUMD', + date_a => 'date NUMA', +}; + +sub search { + my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_; + + warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to"; + + # create a search condition object + my $cond = Search::Estraier::Condition->new(); + + if (length($q) > 0) { + # exact match + $cond->add_attr("filepath ISTRINC $q"); + + $q =~ s/(.)/$1 /g; + # set the search phrase to the search condition object + $cond->set_phrase($q); + } + + $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from); + $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to); + + $cond->add_attr("date NUMGE $files_from") if ($files_from); + $cond->add_attr("date NUMLE $files_to") if ($files_to); + + $cond->add_attr("shareid NUMEQ $shareid") if ($shareid); + + $cond->set_max( $offset + $on_page ); + $cond->set_options( 'SURE' ); + $cond->set_order( $sort_mapping->{$sort} ); + + # get the result of search + my @res; + my ($result, $hits); + + $result = $self->node->search($cond, 0); + if ($result) { + $hits = $result->hits; + } else { + return (0,[]); + } + + # for each document in result + for my $i ($offset .. ($offset + $on_page - 1)) { + last if ($i >= $result->doc_num); + + my $doc = $result->get_doc($i); + + my $row; + foreach my $c (qw/fid hname sname backupnum filepath date type size/) { + $row->{$c} = $doc->attr($c); + } + push @res, $row; + } + + warn "# $hits hits"; + + return ($hits, \@res); + +} + 1; diff --git a/lib/BackupPC/SearchLib.pm b/lib/BackupPC/SearchLib.pm index c19730f..8fe2541 100644 --- a/lib/BackupPC/SearchLib.pm +++ b/lib/BackupPC/SearchLib.pm @@ -10,7 +10,7 @@ use vars qw(%In $MyURL); use Time::HiRes qw/time/; use XML::Writer; use IO::File; -use Search::Estraier 0.04; +use BackupPC::Search::Estraier; my $on_page = 100; my $pager_pages = 10; @@ -145,18 +145,6 @@ my $sort_def = { date_d => 'files.date DESC', date_a => 'files.date ASC', }, - est => { - share_d => 'sname STRD', - share_a => 'sname STRA', - path_d => 'filepath STRD', - path_a => 'filepath STRA', - num_d => 'backupnum NUMD', - num_a => 'backupnum NUMA', - size_d => 'size NUMD', - size_a => 'size NUMA', - date_d => 'date NUMD', - date_a => 'date NUMA', - } }, burn => { default => 'date_a', sql => { @@ -270,81 +258,15 @@ sub getFilesHyperEstraier($) { my $offset = $param->{'offset'} || 0; $offset *= $on_page; - die "no Hyper Estraier node URL?" unless ($hest_node_url); - - # open the database - my $db; - if ($hest_node_url) { - $db ||= Search::Estraier::Node->new($hest_node_url); - $db->set_auth('admin', 'admin'); - } else { - die "BUG: unimplemented"; - } - - # create a search condition object - my $cond = Search::Estraier::Condition->new(); - my $q = $param->{'search_filename'}; my $shareid = $param->{'search_share'}; - - if (length($q) > 0) { - # exact match - $cond->add_attr("filepath ISTRINC $q"); - - $q =~ s/(.)/$1 /g; - # set the search phrase to the search condition object - $cond->set_phrase($q); - } - my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param); - $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from); - $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to); - - $cond->add_attr("date NUMGE $files_from") if ($files_from); - $cond->add_attr("date NUMLE $files_to") if ($files_to); - - $cond->add_attr("shareid NUMEQ $shareid") if ($shareid); - - $cond->set_max( $offset + $on_page ); - $cond->set_options( 'SURE' ); - $cond->set_order( getSort('search', 'est', $param->{'sort'} ) ); - - # get the result of search - my @res; - my ($result, $hits); - - if ($hest_node_url) { - $result = $db->search($cond, 0); - if ($result) { - $hits = $result->hits; - } else { - $hits = 0; - return ($hits,[]); - } - } else { - die "BUG: unimplemented"; - } - - # for each document in result - for my $i ($offset .. ($offset + $on_page - 1)) { - last if ($i >= $result->doc_num); - - my $doc; - if ($hest_node_url) { - $doc = $result->get_doc($i); - } else { - die "BUG: unimplemented"; - } - - my $row; - foreach my $c (qw/fid hname sname backupnum filepath date type size/) { - $row->{$c} = $doc->attr($c); - } - push @res, $row; - } - - return ($hits, \@res); + return BackupPC::Search::Estraier->new( $hest_node_url )->search( + $offset, $on_page, $param->{sort}, + $q, $shareid, $backup_from, $backup_to, $files_from, $files_to + ); + } sub getGzipName($$$)