user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] search: simplify indexing operation
@ 2015-08-17 17:48  7% Eric Wong
  0 siblings, 0 replies; 1+ results
From: Eric Wong @ 2015-08-17 17:48 UTC (permalink / raw)
  To: meta

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 | 78 +++++++++++++++++++++++++----------------------
 public-inbox-index        | 10 +++---
 public-inbox-learn        |  6 ++--
 public-inbox-mda          |  6 ++--
 t/search.t                | 14 +++++++--
 5 files changed, 62 insertions(+), 52 deletions(-)

diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 2608a58..617c267 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -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;
diff --git a/public-inbox-index b/public-inbox-index
index 9cfcadc..2fcf562 100755
--- a/public-inbox-index
+++ b/public-inbox-index
@@ -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;
 }
diff --git a/public-inbox-learn b/public-inbox-learn
index 7f525f5..bd59247 100755
--- a/public-inbox-learn
+++ b/public-inbox-learn
@@ -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;
 	};
 }
 
diff --git a/public-inbox-mda b/public-inbox-mda
index 46a24a1..a3c959a 100755
--- a/public-inbox-mda
+++ b/public-inbox-mda
@@ -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;
 	};
 }
diff --git a/t/search.t b/t/search.t
index 2bb6b6c..0ad0886 100644
--- a/t/search.t
+++ b/t/search.t
@@ -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',
-- 
EW


^ permalink raw reply related	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2015-08-17 17:48  7% [PATCH] search: simplify indexing operation Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).