--- trunk/lib/BackupPC/SearchLib.pm 2005/08/28 10:14:48 83 +++ trunk/lib/BackupPC/SearchLib.pm 2005/08/28 14:05:08 87 @@ -15,24 +15,43 @@ my $dsn = $Conf{SearchDSN}; my $db_user = $Conf{SearchUser} || ''; +my $index_path = $Conf{HyperEstraierIndex}; +if ($index_path) { + $index_path = $TopDir . '/' . $index_path; + $index_path =~ s#//#/#g; +} + +my $dbh; + +sub get_dbh { + $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + return $dbh; +} + sub getUnits() { my @ret; - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); - my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares ORDER BY share} ); + my $dbh = get_dbh(); + my $sth = $dbh->prepare(qq{ + SELECT + shares.id as id, + hosts.name || ':' || shares.name as share + FROM shares + JOIN hosts on hostid = hosts.id + ORDER BY share + } ); $sth->execute(); push @ret, { 'id' => '', 'share' => '-'}; # dummy any while ( my $row = $sth->fetchrow_hashref() ) { push @ret, $row; } - $dbh->disconnect(); return @ret; } sub epoch_to_iso { my $t = shift || return; - my $iso = BackupPC::Lib::timeStamp($t); + my $iso = BackupPC::Lib::timeStamp(undef, $t); $iso =~ s/\s/ /g; return $iso; } @@ -43,25 +62,34 @@ sub mk_epoch_date($$) { my ($name,$suffix) = @_; - my $yyyy = $param->{ $name . '_year_' . $suffix} || return; + my $yyyy = $param->{ $name . '_year_' . $suffix} || return undef; my $mm .= $param->{ $name . '_month_' . $suffix} || ( $suffix eq 'from' ? 1 : 12); my $dd .= $param->{ $name . '_day_' . $suffix} || ( $suffix eq 'from' ? 1 : 31); + + $yyyy =~ s/\D//g; + $mm =~ s/\D//g; + $dd =~ s/\D//g; + my $dt = new DateTime( year => $yyyy, month => $mm, day => $dd ); + print STDERR "mk_epoch_date($name,$suffix) [$yyyy-$mm-$dd] = " . $dt->ymd . " " . $dt->hms . "\n"; return $dt->epoch || 'NULL'; } - return ( + my @ret = ( mk_epoch_date('search_backup', 'from'), mk_epoch_date('search_backup', 'to'), mk_epoch_date('search', 'from'), mk_epoch_date('search', 'to'), ); + + return @ret; + } @@ -85,35 +113,30 @@ } -sub getFiles($$) { - my ($param, $offset) = @_; +sub getFiles($) { + my ($param) = @_; + + my $offset = $param->{'offset'} || 0; + $offset *= $on_page; - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $dbh = get_dbh(); my $sql_cols = qq{ files.id AS fid, hosts.name AS hname, shares.name AS sname, - shares.share AS sharename, - files.backupNum AS backupNum, - files.name AS filename, + files.backupnum AS backupnum, files.path AS filepath, files.date AS date, - files.type AS filetype, - files.size AS size, - -- dvds.name AS dvd - null AS dvd + files.type AS type, + files.size AS size }; my $sql_from = qq{ FROM files INNER JOIN shares ON files.shareID=shares.ID INNER JOIN hosts ON hosts.ID = shares.hostID - INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID - }; - - my $sql_dvd_from = qq{ - -- LEFT JOIN dvds ON dvds.ID = files.dvdid + INNER JOIN backups ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = files.shareID }; my $sql_where; @@ -127,10 +150,7 @@ }; my $sql_count = qq{ select count(files.id) $sql_from $sql_where }; - my $sql_results = qq{ select $sql_cols $sql_from $sql_dvd_from $sql_where $sql_order }; - - $offset ||= 0; - $offset = ($offset * $on_page); + my $sql_results = qq{ select $sql_cols $sql_from $sql_where $sql_order }; my $sth = $dbh->prepare($sql_count); $sth->execute(); @@ -148,44 +168,92 @@ my @ret; while (my $row = $sth->fetchrow_hashref()) { - push(@ret, { - 'hname' => $row->{'hname'}, - 'sname' => $row->{'sname'}, - 'sharename' => $row->{'sharename'}, - 'backupno' => $row->{'backupnum'}, - 'fname' => $row->{'filename'}, - 'fpath' => $row->{'filepath'}, - 'networkpath' => $row->{'networkpath'}, - 'date' => $row->{'date'}, - 'type' => $row->{'filetype'}, - 'size' => $row->{'size'}, - 'id' => $row->{'fid'}, - 'dvd' => $row->{'dvd'} - }); + push @ret, $row; } $sth->finish(); - $dbh->disconnect(); return ($results, \@ret); } +sub getFilesHyperEstraier($) { + my ($param) = @_; + + my $offset = $param->{'offset'} || 0; + $offset *= $on_page; + + die "no index_path?" unless ($index_path); + + use HyperEstraier; + + # open the database + my $db = HyperEstraier::Database->new(); + $db->open($index_path, $HyperEstraier::ESTDBREADER); + + # create a search condition object + my $cond = HyperEstraier::Condition->new(); + + my $q = $param->{'search_filename'}; + my $shareid = $param->{'search_share'}; + + if ($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( $HyperEstraier::Condition::SURE ); + $cond->set_order( 'date NUMA' ); + + # get the result of search + my $result = $db->search($cond, 0); + + my @res; + my $hits = $result->size; + + # for each document in result + for my $i ($offset .. ($offset + $on_page - 1)) { + last if ($i >= $hits); + + my $id = $result->get($i); + my $doc = $db->get_doc($id, 0); + + my $row; + foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) { + $row->{$c} = $doc->attr($c); + } + push @res, $row; + } + + return ($hits, \@res); +} + sub getBackupsNotBurned() { - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $dbh = get_dbh(); my $sql = q{ SELECT backups.hostID AS hostid, min(hosts.name) AS host, - backups.num AS backupno, + backups.num AS backupnum, min(backups.type) AS type, min(backups.date) AS date, min(backups.size) AS size FROM files INNER JOIN shares ON files.shareID=shares.ID INNER JOIN hosts ON hosts.ID = shares.hostID - INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID - WHERE - files.dvdid IS NULL + INNER JOIN backups ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = shares.ID GROUP BY backups.hostID, backups.num ORDER BY min(backups.date) @@ -271,13 +339,13 @@ $retHTML .= "