]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
searchidx: use new `references' method for parsing References
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index f9207e9464cd1dd3afe89b3b1052635db1682ea5..57aed75c31d154b79499eabb93b588f2829d6213 100644 (file)
@@ -11,10 +11,8 @@ use strict;
 use warnings;
 use Fcntl qw(:flock :DEFAULT);
 use PublicInbox::MIME;
-use Email::MIME::ContentType;
-$Email::MIME::ContentType::STRICT_PARAMS = 0;
 use base qw(PublicInbox::Search);
-use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
+use PublicInbox::MID qw/mid_clean id_compress mid_mime mids references/;
 use PublicInbox::MsgIter;
 use Carp qw(croak);
 use POSIX qw(strftime);
@@ -143,18 +141,20 @@ sub add_val ($$$) {
        $doc->add_value($col, $num);
 }
 
-sub add_values ($$$$) {
-       my ($smsg, $bytes, $num, $lines) = @_;
+sub add_values ($$) {
+       my ($doc, $values) = @_;
 
-       my $ts = $smsg->ts;
-       my $doc = $smsg->{doc};
-       add_val($doc, &PublicInbox::Search::TS, $ts);
+       my $ts = $values->[PublicInbox::Search::TS];
+       add_val($doc, PublicInbox::Search::TS, $ts);
 
-       defined($num) and add_val($doc, &PublicInbox::Search::NUM, $num);
+       my $num = $values->[PublicInbox::Search::NUM];
+       defined($num) and add_val($doc, PublicInbox::Search::NUM, $num);
 
-       defined($bytes) and add_val($doc, &PublicInbox::Search::BYTES, $bytes);
+       my $bytes = $values->[PublicInbox::Search::BYTES];
+       defined($bytes) and add_val($doc, PublicInbox::Search::BYTES, $bytes);
 
-       add_val($doc, &PublicInbox::Search::LINES, $lines);
+       my $lines = $values->[PublicInbox::Search::LINES];
+       add_val($doc, PublicInbox::Search::LINES, $lines);
 
        my $yyyymmdd = strftime('%Y%m%d', gmtime($ts));
        add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd);
@@ -284,8 +284,12 @@ sub add_message {
        my $db = $self->{xdb};
 
        my ($doc_id, $old_tid);
-       my $mid = mid_clean(mid_mime($mime));
-       my $threader = $self->{threader};
+       my @mids = mid_mime($mime);
+       if (@mids > 1) {
+               warn "Multi-MID: ( ",join(' | ', @mids)," )\n";
+       }
+       my $mid = mid_clean($mids[0]);
+       my $skel = $self->{skeleton};
 
        eval {
                die 'Message-ID too long' if length($mid) > MAX_MID_SIZE;
@@ -294,7 +298,7 @@ sub add_message {
                        # convert a ghost to a regular message
                        # it will also clobber any existing regular message
                        $doc_id = $smsg->{doc_id};
-                       $old_tid = $smsg->thread_id unless $threader;
+                       $old_tid = $smsg->thread_id unless $skel;
                }
                $smsg = PublicInbox::SearchMsg->new($mime);
                my $doc = $smsg->{doc};
@@ -309,7 +313,8 @@ sub add_message {
                }
 
                my $lines = $mime->body_raw =~ tr!\n!\n!;
-               add_values($smsg, $bytes, $num, $lines);
+               my @values = ($smsg->ts, $num, $bytes, $lines);
+               add_values($doc, \@values);
 
                my $tg = $self->term_generator;
 
@@ -361,8 +366,9 @@ sub add_message {
                # populates smsg->references for smsg->to_doc_data
                my $refs = parse_references($smsg);
                my $data = $smsg->to_doc_data($blob);
-               if ($threader) {
-                       $threader->thread_msg($mid, $smsg->ts, $xpath, $data);
+               if ($skel) {
+                       push @values, $mid, $xpath, $data;
+                       $skel->index_skeleton(\@values);
                } else {
                        link_message($self, $smsg, $refs, $old_tid);
                }
@@ -441,33 +447,24 @@ sub next_thread_id {
 
 sub parse_references ($) {
        my ($smsg) = @_;
-       my $doc = $smsg->{doc};
-       my $mid = $smsg->mid;
        my $mime = $smsg->{mime};
        my $hdr = $mime->header_obj;
-
-       # last References should be IRT, but some mail clients do things
-       # out of order, so trust IRT over References iff IRT exists
-       my @refs = (($hdr->header_raw('References') || '') =~ /<([^>]+)>/g);
-       push(@refs, (($hdr->header_raw('In-Reply-To') || '') =~ /<([^>]+)>/g));
-
-       if (@refs) {
-               my %uniq = ($mid => 1);
-               my @orig_refs = @refs;
-               @refs = ();
-
-               # 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;
+       my $refs = references($hdr);
+       return $refs if scalar(@$refs) == 0;
+
+       # prevent circular references via References here:
+       my %mids = map { $_ => 1 } @{mids($hdr)};
+       my @keep;
+       foreach my $ref (@$refs) {
+               # FIXME: this is an archive-prevention vector like X-No-Archive
+               if (length($ref) > MAX_MID_SIZE) {
+                       warn "References: <$ref> too long, ignoring\n";
                }
+               next if $mids{$ref};
+               push @keep, $ref;
        }
-       $smsg->{references} = '<'.join('> <', @refs).'>' if @refs;
-       \@refs
+       $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
+       \@keep;
 }
 
 sub link_message {
@@ -493,11 +490,6 @@ sub link_message {
        $smsg->{doc}->add_term('G' . $tid);
 }
 
-sub index_blob {
-       my ($self, $mime, $bytes, $num, $blob) = @_;
-       $self->add_message($mime, $bytes, $num, $blob);
-}
-
 sub index_git_blob_id {
        my ($doc, $pfx, $objid) = @_;
 
@@ -515,13 +507,12 @@ sub unindex_blob {
 }
 
 sub index_mm {
-       my ($self, $mime, $warn_existing) = @_;
+       my ($self, $mime) = @_;
        my $mid = mid_clean(mid_mime($mime));
        my $mm = $self->{mm};
        my $num = $mm->mid_insert($mid);
        return $num if defined $num;
 
-       warn "<$mid> reused\n" if $warn_existing;
        # fallback to num_for since filters like RubyLang set the number
        $mm->num_for($mid);
 }
@@ -534,7 +525,7 @@ sub unindex_mm {
 sub index_mm2 {
        my ($self, $mime, $bytes, $blob) = @_;
        my $num = $self->{mm}->num_for(mid_clean(mid_mime($mime)));
-       index_blob($self, $mime, $bytes, $num, $blob);
+       add_message($self, $mime, $bytes, $num, $blob);
 }
 
 sub unindex_mm2 {
@@ -546,7 +537,7 @@ sub unindex_mm2 {
 sub index_both {
        my ($self, $mime, $bytes, $blob) = @_;
        my $num = index_mm($self, $mime);
-       index_blob($self, $mime, $bytes, $num, $blob);
+       add_message($self, $mime, $bytes, $num, $blob);
 }
 
 sub unindex_both {
@@ -713,7 +704,7 @@ sub _index_sync {
                }
        } else {
                # user didn't install DBD::SQLite and DBI
-               rlog($self, $xlog, *index_blob, *unindex_blob, $cb);
+               rlog($self, $xlog, *add_message, *unindex_blob, $cb);
        }
 }
 
@@ -743,15 +734,22 @@ sub create_ghost {
 sub merge_threads {
        my ($self, $winner_tid, $loser_tid) = @_;
        return if $winner_tid == $loser_tid;
-       my ($head, $tail) = $self->find_doc_ids('G' . $loser_tid);
        my $db = $self->{xdb};
 
-       for (; $head != $tail; $head->inc) {
-               my $docid = $head->get_docid;
-               my $doc = $db->get_document($docid);
-               $doc->remove_term('G' . $loser_tid);
-               $doc->add_term('G' . $winner_tid);
-               $db->replace_document($docid, $doc);
+       my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
+       while (1) {
+               my ($head, $tail) = $self->find_doc_ids('G' . $loser_tid);
+               return if $head == $tail;
+               my @ids;
+               for (; $head != $tail && @ids < $batch_size; $head->inc) {
+                       push @ids, $head->get_docid;
+               }
+               foreach my $docid (@ids) {
+                       my $doc = $db->get_document($docid);
+                       $doc->remove_term('G' . $loser_tid);
+                       $doc->add_term('G' . $winner_tid);
+                       $db->replace_document($docid, $doc);
+               }
        }
 }
 
@@ -820,7 +818,7 @@ sub DESTROY {
        $_[0]->{lockfh} = undef;
 }
 
-# remote_* subs are only used by SearchIdxPart and SearchIdxThread:
+# remote_* subs are only used by SearchIdxPart and SearchIdxSkeleton
 sub remote_commit {
        my ($self) = @_;
        print { $self->{w} } "commit\n" or die "failed to write commit: $!";
@@ -833,7 +831,7 @@ sub remote_close {
        print $w "close\n" or die "failed to write to pid:$pid: $!\n";
        close $w or die "failed to close pipe for pid:$pid: $!\n";
        waitpid($pid, 0) == $pid or die "remote process did not finish";
-       $? == 0 or die ref($self)." exited with: $?";
+       $? == 0 or die ref($self)." pid:$pid exited with: $?";
 }
 
 1;