]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
nntp: use Inbox->uidvalidity instead of ->mm->created_at
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 32fa16f5c227ff880f8d0008a973542231f30c20..18390602faada97021c0b774ac944550208ac70e 100644 (file)
@@ -22,9 +22,10 @@ use PublicInbox::OverIdx;
 use PublicInbox::Spawn qw(spawn nodatacow_dir);
 use PublicInbox::Git qw(git_unquote);
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
-our @EXPORT_OK = qw(crlf_adjust log2stack is_ancestor check_size prepare_stack);
+our @EXPORT_OK = qw(crlf_adjust log2stack is_ancestor check_size prepare_stack
+       index_text term_generator add_val);
 my $X = \%PublicInbox::Search::X;
-my ($DB_CREATE_OR_OPEN, $DB_OPEN);
+our ($DB_CREATE_OR_OPEN, $DB_OPEN);
 our $DB_NO_SYNC = 0;
 our $BATCH_BYTES = $ENV{XAPIAN_FLUSH_THRESHOLD} ? 0x7fffffff : 1_000_000;
 use constant DEBUG => !!$ENV{DEBUG};
@@ -154,7 +155,7 @@ sub term_generator ($) { # write-only
 
        $self->{term_generator} //= do {
                my $tg = $X->{TermGenerator}->new;
-               $tg->set_stemmer($self->stemmer);
+               $tg->set_stemmer(PublicInbox::Search::stemmer($self));
                $tg;
        }
 }
@@ -370,8 +371,6 @@ sub add_xapian ($$$$) {
 
        if (defined(my $eidx_key = $smsg->{eidx_key})) {
                $doc->add_boolean_term('O'.$eidx_key);
-               $doc->add_boolean_term('P'.
-                               "$eidx_key:$smsg->{num}:$smsg->{blob}");
        }
        msg_iter($eml, \&index_xapian, [ $self, $doc ]);
        index_ids($self, $doc, $eml, $mids);
@@ -456,57 +455,43 @@ sub _get_doc ($$$) {
        }
 }
 
-sub add_xref3 {
-       my ($self, $docid, $xnum, $oid, $eidx_key, $eml) = @_;
+sub add_eidx_info {
+       my ($self, $docid, $oid, $eidx_key, $eml) = @_;
        begin_txn_lazy($self);
        my $doc = _get_doc($self, $docid, $oid) or return;
        term_generator($self)->set_document($doc);
        $doc->add_boolean_term('O'.$eidx_key);
-       $doc->add_boolean_term('P'."$eidx_key:$xnum:$oid");
        index_list_id($self, $doc, $eml);
        $self->{xdb}->replace_document($docid, $doc);
 }
 
-sub remove_xref3 {
+sub remove_eidx_info {
        my ($self, $docid, $oid, $eidx_key, $eml) = @_;
        begin_txn_lazy($self);
        my $doc = _get_doc($self, $docid, $oid) or return;
-       my $xref3 = PublicInbox::Smsg::xref3(undef, $doc);
-       my %x3 = map { $_ => undef } @$xref3;
-       for (grep(/\A\Q$eidx_key\E:[0-9]+:\Q$oid\E\z/, @$xref3)) {
-               delete $x3{$_};
-               $doc->remove_term('P' . $_);
-       }
-       if (scalar(keys(%x3)) == 0) {
-               $self->{xdb}->delete_document($docid);
-               if (my $del_fh = $self->{del_fh}) { # TODO
-                       print $del_fh $docid, "\n" or die "E: print $!";
-               }
-       } else {
-               if (!grep(/\A\Q$eidx_key\E:/, keys %x3)) {
-                       $doc->remove_term('O'.$eidx_key);
-               }
-               for my $l ($eml->header_raw('List-Id')) {
-                       $l =~ /<([^>]+)>/ or next;
-                       my $lid = lc $1;
-                       $doc->remove_term('G' . $lid);
-
-                       # nb: we don't remove the XL probabilistic terms
-                       # since terms may overlap if cross-posted.
-                       #
-                       # IOW, a message which has both <foo.example.com>
-                       # and <bar.example.com> would have overlapping
-                       # "XLexample" and "XLcom" as terms and which we
-                       # wouldn't know if they're safe to remove if we just
-                       # unindex <foo.example.com> while preserving
-                       # <bar.example.com>.
-                       #
-                       # In any case, this entire sub is will likely never
-                       # be needed and users using the "l:" prefix are probably
-                       # rarer.
-               }
-               $self->{xdb}->replace_document($docid, $doc);
+       eval { $doc->remove_term('O'.$eidx_key) };
+       warn "W: ->remove_term O$eidx_key: $@\n" if $@;
+       for my $l ($eml->header_raw('List-Id')) {
+               $l =~ /<([^>]+)>/ or next;
+               my $lid = lc $1;
+               eval { $doc->remove_term('G' . $lid) };
+               warn "W: ->remove_term G$lid: $@\n" if $@;
+
+               # nb: we don't remove the XL probabilistic terms
+               # since terms may overlap if cross-posted.
+               #
+               # IOW, a message which has both <foo.example.com>
+               # and <bar.example.com> would have overlapping
+               # "XLexample" and "XLcom" as terms and which we
+               # wouldn't know if they're safe to remove if we just
+               # unindex <foo.example.com> while preserving
+               # <bar.example.com>.
+               #
+               # In any case, this entire sub is will likely never
+               # be needed and users using the "l:" prefix are probably
+               # rarer.
        }
+       $self->{xdb}->replace_document($docid, $doc);
 }
 
 sub get_val ($$) {
@@ -570,8 +555,8 @@ sub unindex_eml {
                $tmp{$_}++ for @removed;
        }
        if (!$nr) {
-               $mids = join('> <', @$mids);
-               warn "W: <$mids> missing for removal from overview\n";
+               my $m = join('> <', @$mids);
+               warn "W: <$m> missing for removal from overview\n";
        }
        while (my ($num, $nr) = each %tmp) {
                warn "BUG: $num appears >1 times ($nr) for $oid\n" if $nr != 1;
@@ -624,11 +609,17 @@ sub index_both { # git->cat_async callback
        $smsg->{num} = index_mm($self, $eml, $oid, $sync) or
                die "E: could not generate NNTP article number for $oid";
        add_message($self, $eml, $smsg, $sync);
+       my $cur_cmt = $sync->{cur_cmt} // die 'BUG: {cur_cmt} missing';
+       ${$sync->{latest_cmt}} = $cur_cmt;
 }
 
 sub unindex_both { # git->cat_async callback
-       my ($bref, $oid, $type, $size, $self) = @_;
-       unindex_eml($self, $oid, PublicInbox::Eml->new($bref));
+       my ($bref, $oid, $type, $size, $sync) = @_;
+       unindex_eml($sync->{sidx}, $oid, PublicInbox::Eml->new($bref));
+       # may be undef if leftover
+       if (defined(my $cur_cmt = $sync->{cur_cmt})) {
+               ${$sync->{latest_cmt}} = $cur_cmt;
+       }
 }
 
 sub with_umask {
@@ -662,34 +653,33 @@ sub v1_checkpoint ($$;$) {
        my ($self, $sync, $stk) = @_;
        $self->{ibx}->git->async_wait_all;
 
-       # latest_cmt may be undef
-       my $newest = $stk ? $stk->{latest_cmt} : undef;
-       if ($newest) {
+       # $newest may be undef
+       my $newest = $stk ? $stk->{latest_cmt} : ${$sync->{latest_cmt}};
+       if (defined($newest)) {
                my $cur = $self->{mm}->last_commit || '';
                if (need_update($self, $cur, $newest)) {
                        $self->{mm}->last_commit($newest);
                }
-       } else {
-               ${$sync->{max}} = $self->{batch_bytes};
        }
+       ${$sync->{max}} = $self->{batch_bytes};
 
        $self->{mm}->{dbh}->commit;
-       if ($newest && need_xapian($self)) {
-               my $xdb = $self->{xdb};
+       my $xdb = need_xapian($self) ? $self->{xdb} : undef;
+       if ($newest && $xdb) {
                my $cur = $xdb->get_metadata('last_commit');
                if (need_update($self, $cur, $newest)) {
                        $xdb->set_metadata('last_commit', $newest);
                }
-
+       }
+       if ($stk) { # all done if $stk is passed
                # let SearchView know a full --reindex was done so it can
                # generate ->has_threadid-dependent links
-               if ($sync->{reindex} && !ref($sync->{reindex})) {
+               if ($xdb && $sync->{reindex} && !ref($sync->{reindex})) {
                        my $n = $xdb->get_metadata('has_threadid');
                        $xdb->set_metadata('has_threadid', '1') if $n ne '1';
                }
+               $self->{oidx}->rethread_done($sync->{-opt}); # all done
        }
-
-       $self->{oidx}->rethread_done($sync->{-opt}) if $newest; # all done
        commit_txn_lazy($self);
        $sync->{ibx}->git->cleanup;
        my $nr = ${$sync->{nr}};
@@ -713,21 +703,24 @@ sub process_stack {
        $sync->{nr} = \$nr;
        $sync->{max} = \$max;
        $sync->{sidx} = $self;
+       $sync->{latest_cmt} = \(my $latest_cmt);
 
        $self->{mm}->{dbh}->begin_work;
        if (my @leftovers = keys %{delete($sync->{D}) // {}}) {
                warn('W: unindexing '.scalar(@leftovers)." leftovers\n");
                for my $oid (@leftovers) {
                        $oid = unpack('H*', $oid);
-                       $git->cat_async($oid, \&unindex_both, $self);
+                       $git->cat_async($oid, \&unindex_both, $sync);
                }
        }
        if ($sync->{max_size} = $sync->{-opt}->{max_size}) {
                $sync->{index_oid} = \&index_both;
        }
-       while (my ($f, $at, $ct, $oid) = $stk->pop_rec) {
+       while (my ($f, $at, $ct, $oid, $cur_cmt) = $stk->pop_rec) {
+               my $arg = { %$sync, cur_cmt => $cur_cmt };
                if ($f eq 'm') {
-                       my $arg = { %$sync, autime => $at, cotime => $ct };
+                       $arg->{autime} = $at;
+                       $arg->{cotime} = $ct;
                        if ($sync->{max_size}) {
                                $git->check_async($oid, \&check_size, $arg);
                        } else {
@@ -735,7 +728,7 @@ sub process_stack {
                        }
                        v1_checkpoint($self, $sync) if $max <= 0;
                } elsif ($f eq 'd') {
-                       $git->cat_async($oid, \&unindex_both, $self);
+                       $git->cat_async($oid, \&unindex_both, $arg);
                }
        }
        v1_checkpoint($self, $sync, $stk);
@@ -759,17 +752,18 @@ sub log2stack ($$$) {
        my $fh = $git->popen(qw(log --raw -r --pretty=tformat:%at-%ct-%H
                                --no-notes --no-color --no-renames --no-abbrev),
                                $range);
-       my ($at, $ct, $stk);
+       my ($at, $ct, $stk, $cmt);
        while (<$fh>) {
+               return if $sync->{quit};
                if (/\A([0-9]+)-([0-9]+)-($OID)$/o) {
-                       ($at, $ct) = ($1 + 0, $2 + 0);
-                       $stk //= PublicInbox::IdxStack->new($3);
+                       ($at, $ct, $cmt) = ($1 + 0, $2 + 0, $3);
+                       $stk //= PublicInbox::IdxStack->new($cmt);
                } elsif (/$del/) {
                        my $oid = $1;
                        if ($D) { # reindex case
                                $D->{pack('H*', $oid)}++;
                        } else { # non-reindex case:
-                               $stk->push_rec('d', $at, $ct, $oid);
+                               $stk->push_rec('d', $at, $ct, $oid, $cmt);
                        }
                } elsif (/$add/) {
                        my $oid = $1;
@@ -777,12 +771,10 @@ sub log2stack ($$$) {
                                my $oid_bin = pack('H*', $oid);
                                my $nr = --$D->{$oid_bin};
                                delete($D->{$oid_bin}) if $nr <= 0;
-
                                # nr < 0 (-1) means it never existed
-                               $stk->push_rec('m', $at, $ct, $oid) if $nr < 0;
-                       } else {
-                               $stk->push_rec('m', $at, $ct, $oid);
+                               next if $nr >= 0;
                        }
+                       $stk->push_rec('m', $at, $ct, $oid, $cmt);
                }
        }
        close $fh or die "git log failed: \$?=$?";