pre-seed with on disk backups
[BackupPC.git] / lib / BackupPC / Search.pm
index 70f084b..7f75add 100644 (file)
@@ -32,7 +32,7 @@ sub search_module {
        if ( $@ ) {
                warn "ERROR: $search_module: $!";
        } else {
-               warn "# using $search_module for full-text search";
+               #warn "# using $search_module for full-text search";
        }
 
        return $search_module->new( %Conf );
@@ -133,12 +133,7 @@ sub getWhere($) {
        push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
        push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'});
 
-       if ( $param->{burned} ) {
-               my $is_what = 'is null';
-               $is_what = '= 1' if ($param->{burned} eq 'burned');
-               push @conditions, "archive_burned.part $is_what";
-               push @conditions, "archive_burned.copy $is_what";
-       }
+       push @conditions, join(' ' , 'burned is', $param->{burned} eq 'burned' ? '' : 'not', 'true') if $param->{burned};
 
        return join(" and ", @conditions);
 }
@@ -226,43 +221,31 @@ sub getFiles($) {
        # do we have to add tables for burned media?
        if ( $param->{burned} ) {
                $sql_from .= qq{
-                       LEFT OUTER JOIN archive_backup_parts on backup_id = backups.id
-                       LEFT OUTER JOIN archive_burned on archive_burned.archive_id = archive_id
+                       LEFT OUTER JOIN backups_burned on backup_id = backups.id
                };
        }
 
        my $order = getSort('search', 'sql', $param->{'sort'});
 
+       # XXX LIMIT $on_page doesn't work since we don't get correct number of results
        my $sql_order = qq{
                ORDER BY $order
-               LIMIT $on_page
                OFFSET ?
        };
 
-       my $sql_count = qq{ select count(files.id) $sql_from $sql_where };
        my $sql_results = qq{ select $sql_cols $sql_from $sql_where $sql_order };
-
-       my $sth = $dbh->prepare($sql_count);
-       $sth->execute();
-       my ($results) = $sth->fetchrow_array();
-
-       $sth = $dbh->prepare($sql_results);
-       $sth->execute( $offset );
-
-       if ($sth->rows != $results) {
-               my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows;
-               $bug =~ s/\s+/ /gs;
-               print STDERR "$bug\n";
-       }
+       my $sth = $dbh->prepare($sql_results);
+       my $rows = $sth->execute( $offset );
 
        my @ret;
-      
+
        while (my $row = $sth->fetchrow_hashref()) {
                push @ret, $row;
+               last if $#ret + 1 >= $on_page;
        }
      
        $sth->finish();
-       return ($results, \@ret);
+       return ($rows, \@ret);
 }
 
 sub getFilesHyperEstraier($) {
@@ -371,6 +354,45 @@ sub getGzipSize($$)
        );
 }
 
+sub host_backup_nums {
+       my $host = shift;
+       my $sth = get_dbh->prepare(qq{
+               select
+                       hosts.name as host, -- FIXME for debug
+                       backups.num as num,
+                       inc_size,
+                       size,
+                       inc_deleted
+               from backups
+               join hosts on hosts.id = hostid
+               where hosts.name = ?
+       });
+       $sth->execute($host);
+       # and inc_size < 0 and size > 0 and not inc_deleted
+
+       my $all_backup_numbers;
+       # pre-seed with on disk backups
+       $all_backup_numbers->{ $_->{num} }++ foreach $bpc->BackupInfoRead($host);
+
+       while( my $row = $sth->fetchrow_hashref ) {
+warn "# row ",dump $row;
+               $all_backup_numbers->{ $row->{num} } =
+               $row->{inc_deleted}  ? 0 :
+               $row->{size}    == 0 ? 0 :
+               $row->{inc_size} > 0 ? 0 :
+               $row->{size}     > 0 ? 1 :
+               0;
+       }
+
+warn "# host $host all_backup_numbers = ",dump($all_backup_numbers);
+       my @backup_nums = 
+               sort { $a <=> $b }
+               grep { $all_backup_numbers->{$_} }
+               keys %$all_backup_numbers;
+
+       return @backup_nums;
+}
+
 
 sub getBackupsNotBurned($) {