]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
searchidx: warn about vivifying multiple ghosts
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index ef723a4b23161362a9da9ca033f32d8832d5bfe0..446cfb0d50afd8be723a24c4f6f5751de80ba548 100644 (file)
@@ -352,7 +352,7 @@ sub add_message {
 
                # populates smsg->references for smsg->to_doc_data
                my $refs = parse_references($smsg);
-               $mid0 = $mids->[0] unless defined $mid0;
+               $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility
                my $data = $smsg->to_doc_data($oid, $mid0);
                foreach my $mid (@$mids) {
                        $tg->index_text($mid, 1, 'XM');
@@ -369,10 +369,12 @@ sub add_message {
                        }
                }
 
+               $self->delete_article($num) if defined $num; # for reindexing
                if ($skel) {
                        push @values, $mids, $xpath, $data;
                        $skel->index_skeleton(\@values);
                        $doc->add_boolean_term('Q' . $_) foreach @$mids;
+                       $doc->add_boolean_term('XNUM' . $num) if defined $num;
                        $doc_id = $self->{xdb}->add_document($doc);
                } else {
                        $doc_id = link_and_save($self, $doc, $mids, $refs,
@@ -421,6 +423,16 @@ sub remove_message {
        }
 }
 
+sub delete_article {
+       my ($self, $num) = @_;
+       my $ndel = 0;
+       batch_do($self, 'XNUM' . $num, sub {
+               my ($ids) = @_;
+               $ndel += scalar @$ids;
+               $self->{xdb}->delete_document($_) for @$ids;
+       });
+}
+
 # MID is a hint in V2
 sub remove_by_oid {
        my ($self, $oid, $mid) = @_;
@@ -549,6 +561,10 @@ sub link_and_save {
                        1;
                });
        }
+       if ($vivified > 1) {
+               my $id = '<'.join('> <', @$mids).'>';
+               warn "BUG: vivified multiple ($vivified) ghosts for $id\n";
+       }
        # not really important, but we return any vivified ghost docid, here:
        return $doc_id if defined $doc_id;
        link_doc($self, $doc, $refs, $old_tid);