]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
use raw header for Message-ID
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 44f6bc1728a986e8259756da77773538a510d38b..63be68101b40c8ae7f4f8d8e39ee94492594a589 100644 (file)
@@ -1,13 +1,20 @@
 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
 # based on notmuch, but with no concept of folders, files or flags
+#
+# Indexes mail with Xapian and our (SQLite-based) ::Msgmap for use
+# with the web and NNTP interfaces.  This index maintains thread
+# relationships for use by Mail::Thread.  This writes to the search
+# index.
 package PublicInbox::SearchIdx;
 use strict;
 use warnings;
 use base qw(PublicInbox::Search);
-use PublicInbox::MID qw/mid_clean mid_compress/;
+use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
+require PublicInbox::Git;
 *xpfx = *PublicInbox::Search::xpfx;
 
+use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
 use constant {
        PERM_UMASK => 0,
        OLD_PERM_GROUP => 1,
@@ -36,16 +43,23 @@ sub new {
        $self;
 }
 
+sub add_val {
+       my ($doc, $col, $num) = @_;
+       $num = Search::Xapian::sortable_serialise($num);
+       $doc->add_value($col, $num);
+}
+
 sub add_message {
-       my ($self, $mime) = @_; # mime = Email::MIME object
+       my ($self, $mime, $bytes, $num) = @_; # mime = Email::MIME object
        my $db = $self->{xdb};
 
        my $doc_id;
-       my $mid = mid_clean($mime->header('Message-ID'));
+       my $mid = mid_clean(mid_mime($mime));
        my $was_ghost = 0;
        my $ct_msg = $mime->header('Content-Type') || 'text/plain';
 
        eval {
+               die 'Message-ID too long' if length($mid) > MAX_MID_SIZE;
                my $smsg = $self->lookup_message($mid);
                my $doc;
 
@@ -74,14 +88,20 @@ sub add_message {
                my $subj = $smsg->subject;
 
                if ($subj ne '') {
-                       $doc->add_term(xpfx('subject') . $subj);
-
                        my $path = $self->subject_path($subj);
-                       $doc->add_term(xpfx('path') . mid_compress($path));
+                       $doc->add_term(xpfx('path') . id_compress($path));
                }
 
-               my $ts = Search::Xapian::sortable_serialise($smsg->ts);
-               $doc->add_value(PublicInbox::Search::TS, $ts);
+               add_val($doc, &PublicInbox::Search::TS, $smsg->ts);
+
+               defined($num) and
+                       add_val($doc, &PublicInbox::Search::NUM, $num);
+
+               defined($bytes) and
+                       add_val($doc, &PublicInbox::Search::BYTES, $bytes);
+
+               add_val($doc, &PublicInbox::Search::LINES,
+                               $mime->body_raw =~ tr!\n!\n!);
 
                my $tg = $self->term_generator;
 
@@ -91,7 +111,7 @@ sub add_message {
                $tg->index_text($subj) if $subj;
                $tg->increase_termpos;
 
-               $tg->index_text($smsg->from->format);
+               $tg->index_text($smsg->from);
                $tg->increase_termpos;
 
                $mime->walk_parts(sub {
@@ -175,8 +195,6 @@ sub term_generator { # write-only
        $self->{term_generator} = $tg;
 }
 
-sub next_doc_id { $_[0]->{xdb}->get_lastdocid + 1 }
-
 # increments last_thread_id counter
 # returns a 64-bit integer represented as a hex string
 sub next_thread_id {
@@ -204,9 +222,10 @@ sub link_message_to_parents {
        my $doc = $smsg->{doc};
        my $mid = $smsg->mid;
        my $mime = $smsg->mime;
-       my $refs = $mime->header('References');
+       my $hdr = $mime->header_obj;
+       my $refs = $hdr->header_raw('References');
        my @refs = $refs ? ($refs =~ /<([^>]+)>/g) : ();
-       if (my $irt = $mime->header('In-Reply-To')) {
+       if (my $irt = $hdr->header_raw('In-Reply-To')) {
                # last References should be $irt
                # we will de-dupe later
                push @refs, mid_clean($irt);
@@ -220,13 +239,16 @@ sub link_message_to_parents {
 
                # prevent circular references via References: here:
                foreach my $ref (@orig_refs) {
+                       if (length($ref) > MAX_MID_SIZE) {
+                               warn "References: <$ref> too long, ignoring\n";
+                       }
                        next if $uniq{$ref};
                        $uniq{$ref} = 1;
                        push @refs, $ref;
                }
        }
        if (@refs) {
-               $smsg->{references_sorted} = '<'.join('><', @refs).'>';
+               $smsg->{references} = '<'.join('> <', @refs).'>';
 
                # first ref *should* be the thread root,
                # but we can never trust clients to do the right thing
@@ -247,24 +269,54 @@ sub link_message_to_parents {
 }
 
 sub index_blob {
-       my ($self, $git, $blob) = @_;
-       my $mime = do_cat_mail($git, $blob) or return;
-       eval { $self->add_message($mime) };
-       warn "W: index_blob $blob: $@\n" if $@;
+       my ($self, $git, $mime, $bytes, $num) = @_;
+       $self->add_message($mime, $bytes, $num);
 }
 
 sub unindex_blob {
-       my ($self, $git, $blob) = @_;
-       my $mime = do_cat_mail($git, $blob) or return;
-       my $mid = $mime->header('Message-ID');
-       eval { $self->remove_message($mid) } if defined $mid;
-       warn "W: unindex_blob $blob: $@\n" if $@;
+       my ($self, $git, $mime) = @_;
+       my $mid = eval { mid_clean(mid_mime($mime)) };
+       $self->remove_message($mid) if defined $mid;
+}
+
+sub index_mm {
+       my ($self, $git, $mime) = @_;
+       $self->{mm}->mid_insert(mid_clean(mid_mime($mime)));
+}
+
+sub unindex_mm {
+       my ($self, $git, $mime) = @_;
+       $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
+}
+
+sub index_mm2 {
+       my ($self, $git, $mime, $bytes) = @_;
+       my $num = $self->{mm}->num_for(mid_clean(mid_mime($mime)));
+       index_blob($self, $git, $mime, $bytes, $num);
+}
+
+sub unindex_mm2 {
+       my ($self, $git, $mime) = @_;
+       $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
+       unindex_blob($self, $git, $mime);
+}
+
+sub index_both {
+       my ($self, $git, $mime, $bytes) = @_;
+       my $num = index_mm($self, $git, $mime);
+       index_blob($self, $git, $mime, $bytes, $num);
+}
+
+sub unindex_both {
+       my ($self, $git, $mime) = @_;
+       unindex_blob($self, $git, $mime);
+       unindex_mm($self, $git, $mime);
 }
 
 sub do_cat_mail {
-       my ($git, $blob) = @_;
+       my ($git, $blob, $sizeref) = @_;
        my $mime = eval {
-               my $str = $git->cat_file($blob);
+               my $str = $git->cat_file($blob, $sizeref);
                Email::MIME->new($str);
        };
        $@ ? undef : $mime;
@@ -281,25 +333,22 @@ sub rlog {
        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}$!;
-       my $git_dir = $self->{git_dir};
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($git_dir);
-       my @cmd = ('git', "--git-dir=$git_dir", "log",
-                   qw/--reverse --no-notes --no-color --raw -r --no-abbrev/,
-                   $range);
+       my $git = PublicInbox::Git->new($self->{git_dir});
+       my $log = $git->popen(qw/log --reverse --no-notes --no-color
+                               --raw -r --no-abbrev/, $range);
        my $latest;
-       my $pid = open(my $log, '-|', @cmd) or
-               die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
-       while (my $line = <$log>) {
+       my $bytes;
+       while (defined(my $line = <$log>)) {
                if ($line =~ /$addmsg/o) {
-                       $add_cb->($self, $git, $1);
+                       my $mime = do_cat_mail($git, $1, \$bytes) or next;
+                       $add_cb->($self, $git, $mime, $bytes);
                } elsif ($line =~ /$delmsg/o) {
-                       $del_cb->($self, $git, $1);
+                       my $mime = do_cat_mail($git, $1) or next;
+                       $del_cb->($self, $git, $mime);
                } elsif ($line =~ /^commit ($h40)/o) {
                        $latest = $1;
                }
        }
-       close $log;
        $latest;
 }
 
@@ -308,17 +357,45 @@ sub _index_sync {
        my ($self, $head) = @_;
        my $db = $self->{xdb};
        $head ||= 'HEAD';
+       my $mm = $self->{mm} = eval {
+               require PublicInbox::Msgmap;
+               PublicInbox::Msgmap->new($self->{git_dir}, 1);
+       };
 
        $db->begin_transaction;
-       eval {
-               my $latest = $db->get_metadata('last_commit');
-               my $range = $latest eq '' ? $head : "$latest..$head";
-               $latest = $self->rlog($range, *index_blob, *unindex_blob);
-               $db->set_metadata('last_commit', $latest) if defined $latest;
-       };
+       my $lx = $db->get_metadata('last_commit');
+       my $range = $lx eq '' ? $head : "$lx..$head";
+       if ($mm) {
+               $mm->{dbh}->begin_work;
+               my $lm = $mm->last_commit || '';
+               if ($lm eq $lx) {
+                       # Common case is the indexes are synced,
+                       # we only need to run git-log once:
+                       $lx = $self->rlog($range, *index_both, *unindex_both);
+                       $mm->{dbh}->commit;
+                       if (defined $lx) {
+                               $db->set_metadata('last_commit', $lx);
+                               $mm->last_commit($lx);
+                       }
+               } else {
+                       # dumb case, msgmap and xapian are out-of-sync
+                       # do not care for performance:
+                       my $r = $lm eq '' ? $head : "$lm..$head";
+                       $lm = $self->rlog($r, *index_mm, *unindex_mm);
+                       $mm->{dbh}->commit;
+                       $mm->last_commit($lm) if defined $lm;
+
+                       $lx = $self->rlog($range, *index_mm2, *unindex_mm2);
+                       $db->set_metadata('last_commit', $lx) if defined $lx;
+               }
+       } else {
+               # user didn't install DBD::SQLite and DBI
+               $lx = $self->rlog($range, *index_blob, *unindex_blob);
+               $db->set_metadata('last_commit', $lx) if defined $lx;
+       }
        if ($@) {
-               warn "indexing failed: $@\n";
                $db->cancel_transaction;
+               $mm->{dbh}->rollback if $mm;
        } else {
                $db->commit_transaction;
        }
@@ -366,12 +443,9 @@ sub merge_threads {
 
 sub _read_git_config_perm {
        my ($self) = @_;
-       my @cmd = ('git', "--git-dir=$self->{git_dir}",
-                  qw(config core.sharedRepository));
-       my $pid = open(my $fh, '-|', @cmd) or
-               die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
+       my @cmd = qw(config core.sharedRepository);
+       my $fh = PublicInbox::Git->new($self->{git_dir})->popen(@cmd);
        my $perm = <$fh>;
-       close $fh;
        chomp $perm if defined $perm;
        $perm;
 }