]> Sergey Matveev's repositories - public-inbox.git/commitdiff
search: simplify indexing operation
authorEric Wong <e@80x24.org>
Mon, 17 Aug 2015 16:49:31 +0000 (16:49 +0000)
committerEric Wong <e@80x24.org>
Mon, 17 Aug 2015 17:46:56 +0000 (17:46 +0000)
There's no need to make a transaction for each message when doing
incremental indexing against a git repository.  While we're at it,
simplify the interface for callers, too and do not auto-create
the Xapian database if it was not explicitly enabled.

lib/PublicInbox/Search.pm
public-inbox-index
public-inbox-learn
public-inbox-mda
t/search.t

index 2608a58f4f4860388f7cd1cece9cbb99e60a91a0..617c267b789534ad10a31d5da4c747197fbc41bf 100644 (file)
@@ -59,10 +59,13 @@ sub new {
 
        if ($writable) { # not used by the WWW interface
                require Search::Xapian::WritableDatabase;
-               require File::Path;
-               File::Path::mkpath($dir);
-               $db = Search::Xapian::WritableDatabase->new($dir,
-                                       Search::Xapian::DB_CREATE_OR_OPEN);
+               my $flag = Search::Xapian::DB_OPEN;
+               if ($writable == 1) {
+                       require File::Path;
+                       File::Path::mkpath($dir);
+                       $flag = Search::Xapian::DB_CREATE_OR_OPEN;
+               }
+               $db = Search::Xapian::WritableDatabase->new($dir, $flag);
        } else {
                $db = Search::Xapian::Database->new($dir);
        }
@@ -82,7 +85,6 @@ sub add_message {
        my $ct_msg = $mime->header('Content-Type') || 'text/plain';
        my $enc_msg = PublicInbox::View::enc_for($ct_msg);
 
-       $db->begin_transaction;
        eval {
                my $smsg = $self->lookup_message($mid);
                my $doc;
@@ -175,9 +177,7 @@ sub add_message {
 
        if ($@) {
                warn "failed to index message <$mid>: $@\n";
-               $db->cancel_transaction;
-       } else {
-               $db->commit_transaction;
+               return undef;
        }
        $doc_id;
 }
@@ -190,7 +190,6 @@ sub remove_message {
        $mid = mid_clean($mid);
        $mid = mid_compressed($mid);
 
-       $db->begin_transaction;
        eval {
                $doc_id = $self->find_unique_doc_id('mid', $mid);
                $db->delete_document($doc_id) if defined $doc_id;
@@ -198,9 +197,7 @@ sub remove_message {
 
        if ($@) {
                warn "failed to remove message <$mid>: $@\n";
-               $db->cancel_transaction;
-       } else {
-               $db->commit_transaction;
+               return undef;
        }
        $doc_id;
 }
@@ -512,40 +509,47 @@ sub enquire {
 
 # indexes all unindexed messages
 sub index_sync {
-       my ($self, $git) = @_;
+       my ($self) = @_;
+       require PublicInbox::GitCatFile;
        my $db = $self->{xdb};
-       my $latest = $db->get_metadata('last_commit');
-       my $range = length $latest ? "$latest..HEAD" : 'HEAD';
-       $latest = undef;
-
        my $hex = '[a-f0-9]';
        my $h40 = $hex .'{40}';
        my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
        my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
 
-       # get indexed messages
-       my @cmd = ('git', "--git-dir=$git->{git_dir}", "log",
-                   qw/--reverse --no-notes --no-color --raw -r --no-abbrev/,
-                   $range);
-
-       my $pid = open(my $log, '-|', @cmd) or
-               die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
-       my $last;
-       while (my $line = <$log>) {
-               if ($line =~ /$addmsg/o) {
-                       $self->index_blob($git, $1);
-               } elsif ($line =~ /$delmsg/o) {
-                       $self->unindex_blob($git, $1);
-               } elsif ($line =~ /^commit ($h40)/o) {
-                       my $commit = $1;
-                       if (defined $latest) {
-                               $db->set_metadata('last_commit', $latest)
+       $db->begin_transaction;
+       eval {
+               my $git = PublicInbox::GitCatFile->new($self->{git_dir});
+
+               my $latest = $db->get_metadata('last_commit');
+               my $range = length $latest ? "$latest..HEAD" : 'HEAD';
+               $latest = undef;
+
+               # get indexed messages
+               my @cmd = ('git', "--git-dir=$self->{git_dir}", "log",
+                           qw/--reverse --no-notes --no-color --raw -r
+                              --no-abbrev/, $range);
+               my $pid = open(my $log, '-|', @cmd) or
+                       die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
+
+               while (my $line = <$log>) {
+                       if ($line =~ /$addmsg/o) {
+                               $self->index_blob($git, $1);
+                       } elsif ($line =~ /$delmsg/o) {
+                               $self->unindex_blob($git, $1);
+                       } elsif ($line =~ /^commit ($h40)/o) {
+                               $latest = $1;
                        }
-                       $latest = $commit;
                }
+               close $log;
+               $db->set_metadata('last_commit', $latest) if defined $latest;
+       };
+       if ($@) {
+               warn "indexing failed: $@\n";
+               $db->cancel_transaction;
+       } else {
+               $db->commit_transaction;
        }
-       close $log;
-       $db->set_metadata('last_commit', $latest) if defined $latest;
 }
 
 1;
index 9cfcadc2df5d65bff8da98334d310f9f1797d06c..2fcf5627d3fc7a59d8f117e11a57bab0874b0db3 100755 (executable)
@@ -10,8 +10,11 @@ use strict;
 use warnings;
 my $usage = "public-inbox-index GIT_DIR";
 use PublicInbox::Config;
-use PublicInbox::Search;
-use PublicInbox::GitCatFile;
+eval { require PublicInbox::Search };
+if ($@) {
+       print STDERR "Search::Xapian required for $0\n";
+       exit 1;
+}
 
 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
 if (@ARGV) {
@@ -23,7 +26,6 @@ if (@ARGV) {
 sub index_dir {
        my ($git_dir) = @_;
        -d $git_dir or die "$git_dir does not appear to be a git repository\n";
-       my $git = PublicInbox::GitCatFile->new($git_dir);
        my $s = PublicInbox::Search->new($git_dir, 1);
-       $s->index_sync($git);
+       $s->index_sync;
 }
index 7f525f558f5d17b26b2c21742bab48631b984a08..bd592471652422f638a9fb6e6da677edf803fe44 100755 (executable)
@@ -79,11 +79,9 @@ foreach my $recipient (keys %dests) {
 
        $err or eval {
                require PublicInbox::Search;
-               require PublicInbox::GitCatFile;
-               my $git = PublicInbox::GitCatFile->new($git_dir);
                umask 0022; # XXX FIXME use git config core.sharedRepository
-               my $s = PublicInbox::Search->new($git_dir, 1);
-               $s->index_sync($git);
+               my $s = PublicInbox::Search->new($git_dir, 2);
+               $s->index_sync;
        };
 }
 
index 46a24a14b8df80ab9b084e784a78deedc0bda2d9..a3c959ac1ddeaf218b1b11bc59a8852d0963f189 100755 (executable)
@@ -89,10 +89,8 @@ sub search_index_sync {
        my ($git_dir) = @_;
        eval {
                require PublicInbox::Search;
-               require PublicInbox::GitCatFile;
-               my $git = PublicInbox::GitCatFile->new($git_dir);
                umask 0022; # XXX FIXME use git config core.sharedRepository
-               my $s = PublicInbox::Search->new($git_dir, 1);
-               $s->index_sync($git);
+               my $s = PublicInbox::Search->new($git_dir, 2);
+               $s->index_sync;
        };
 }
index 2bb6b6cf02eaaa3017b07b86e0d12200d0d2a135..0ad0886b7b859c2d018d7063374d6bbf65dda81c 100644 (file)
@@ -18,6 +18,10 @@ ok($@, "exception raised on non-existent DB");
 
 my $rw = PublicInbox::Search->new($git_dir, 1);
 my $ro = PublicInbox::Search->new($git_dir);
+my $rw_commit = sub {
+       $rw = undef;
+       $rw = PublicInbox::Search->new($git_dir, 1);
+};
 
 {
        my $root = Email::MIME->create(
@@ -53,6 +57,7 @@ sub filter_mids {
 }
 
 {
+       $rw_commit->();
        $ro->reopen;
        my $found = $ro->lookup_message('<root@s>');
        ok($found, "message found");
@@ -101,7 +106,7 @@ sub filter_mids {
 
 # ghost vivication
 {
-       $rw->reopen;
+       $rw_commit->();
        my $rmid = '<ghost-message@s>';
        my $reply_to_ghost = Email::MIME->create(
                header_str => [
@@ -135,6 +140,7 @@ sub filter_mids {
 
 # search thread on ghost
 {
+       $rw_commit->();
        $ro->reopen;
 
        # Subject:
@@ -150,7 +156,7 @@ sub filter_mids {
 
 # long message-id
 {
-       $rw->reopen;
+       $rw_commit->();
        $ro->reopen;
        my $long_mid = 'last' . ('x' x 60). '@s';
        my $long_midc = Digest::SHA::sha1_hex($long_mid);
@@ -169,6 +175,7 @@ sub filter_mids {
        my $long_id = $rw->add_message($long);
        is($long_id, int($long_id), "long_id is an integer: $long_id");
 
+       $rw_commit->();
        $ro->reopen;
        my $res = $ro->query('references:root@s');
        my @res = filter_mids($res);
@@ -194,6 +201,7 @@ sub filter_mids {
                body => "no References\n");
        ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
 
+       $rw_commit->();
        $ro->reopen;
        my $t = $ro->get_thread('root@s');
        is($t->{count}, 4, "got all 4 mesages in thread");
@@ -204,7 +212,7 @@ sub filter_mids {
 
 # quote prioritization
 {
-       $rw->reopen;
+       $rw_commit->();
        $rw->add_message(Email::MIME->create(
                header_str => [
                        Date => 'Sat, 02 Oct 2010 00:00:01 +0000',