make indexer and searcher lazy
[BackupPC.git] / lib / BackupPC / Search / KinoSearch.pm
index 043f8ab..3efcf8c 100644 (file)
@@ -15,6 +15,17 @@ sub new {
        my $class = shift @_;
        my %Conf = @_;
 
        my $class = shift @_;
        my %Conf = @_;
 
+       my $index_path = $Conf{KinoPath} || die "no KinoPath";
+
+       my $self = bless { index => $index_path }, $class;
+       warn "# ",dump($self);
+       return $self;
+}
+
+sub indexer {
+       my $self = shift;
+       return $self->{_indexer} if defined $self->{_indexer};
+
        my $schema = KinoSearch::Plan::Schema->new;
 
 
        my $schema = KinoSearch::Plan::Schema->new;
 
 
@@ -47,33 +58,33 @@ sub new {
 
 #      $schema->spec_field( name => '_doc', type => $blob_type );
 
 
 #      $schema->spec_field( name => '_doc', type => $blob_type );
 
-       my $index_path = $Conf{KinoPath} || die "no KinoPath";
-
        my $indexer = KinoSearch::Index::Indexer->new(
                schema => $schema,
        my $indexer = KinoSearch::Index::Indexer->new(
                schema => $schema,
-               index  => $index_path,
+               index  => $self->{index},
                create => 1,
        );
 
                create => 1,
        );
 
-       warn "# using $index_path";
-
        $indexer->commit; # make sure that index exists
 
        $indexer->commit; # make sure that index exists
 
-       my $self = bless {
-               indexer => $indexer,
-               searcher => KinoSearch::Search::IndexSearcher->new(
-                       index => $index_path,
-               ),
+       warn "# created indexer";
 
 
-       }, $class;
-       return $self;
+       return $self->{_indexer} = $indexer;
+
+};
+
+our $searcher;
+sub searcher {
+       my $self = shift;
+       return $self->{_searcher} if $self->{_searcher};
+       $self->{_searcher} =
+       KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
 }
 
 sub exists {
        my ($self,$row) = @_;
 
        my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
 }
 
 sub exists {
        my ($self,$row) = @_;
 
        my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
-       my $hits = $self->{searcher}->hits( query => "_uri:$uri" );
+       my $hits = $self->searcher->hits( query => "_uri:$uri" );
        return $hits->total_hits;
 }
 
        return $hits->total_hits;
 }
 
@@ -87,13 +98,13 @@ sub add_doc {
 
        warn "XXX ",dump($row) if $ENV{DEBUG};
 
 
        warn "XXX ",dump($row) if $ENV{DEBUG};
 
-       $self->{indexer}->add_doc( $row );
+       $self->indexer->add_doc( $row );
 
 }
 
 sub commit {
        my $self = shift;
 
 }
 
 sub commit {
        my $self = shift;
-       $self->{indexer}->commit;
+       $self->indexer->commit;
        warn "# commit index";
 }
 
        warn "# commit index";
 }
 
@@ -110,7 +121,7 @@ sub search {
        my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
 
 #      $q =~ s/(.)/$1 /g;
        my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
 
 #      $q =~ s/(.)/$1 /g;
-       my $hits = $self->{searcher}->hits(
+       my $hits = $self->searcher->hits(
                query => $q,
                sort_spec => $sort_spec,
        );
                query => $q,
                sort_spec => $sort_spec,
        );