1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # read-only counterpart for PublicInbox::LeiStore
5 package PublicInbox::LeiSearch;
8 use parent qw(PublicInbox::ExtSearch);
9 use PublicInbox::Search qw(xap_terms);
10 use PublicInbox::ContentHash qw(content_digest content_hash);
11 use PublicInbox::MID qw(mids mids_in);
13 # get combined docid from over.num:
14 # (not generic Xapian, only works with our sharding scheme)
16 my ($self, $num) = @_;
17 my $nshard = $self->{nshard};
18 ($num - 1) * $nshard + $num % $nshard + 1;
22 my ($self, $num) = @_; # num_or_mitem
23 my $xdb = $self->xdb; # set {nshard};
24 my $docid = ref($num) ? $num->get_docid : num2docid($self, $num);
25 my $kw = xap_terms('K', $xdb, $docid);
26 warn "E: #$docid ($num): $@\n" if $@;
27 wantarray ? sort(keys(%$kw)) : $kw;
30 # when a message has no Message-IDs at all, this is needed for
31 # unsent Draft messages, at least
34 my $dig = content_digest($eml);
35 my $chash = $dig->clone->digest;
36 my $mids = mids_in($eml,
37 qw(Message-ID X-Alt-Message-ID Resent-Message-ID));
39 $eml->{-lei_fake_mid} = $mids->[0] =
40 PublicInbox::Import::digest2mid($dig, $eml);
45 sub _cmp_1st { # git->cat_async callback
46 my ($bref, $oid, $type, $size, $cmp) = @_; # cmp: [chash, found, smsg]
47 if (content_hash(PublicInbox::Eml->new($bref)) eq $cmp->[0]) {
48 $cmp->[1]->{$oid} = $cmp->[2]->{num};
52 sub xids_for { # returns { OID => docid } mapping for $eml matches
53 my ($self, $eml, $min) = @_;
54 my ($chash, $mids) = content_key($eml);
55 my @overs = ($self->over // $self->overs_all);
58 for my $mid (@$mids) {
61 while (my $cur = $o->next_by_mid($mid, \$id, \$prev)) {
62 next if $found->{$cur->{blob}};
63 $git->cat_async($cur->{blob}, \&_cmp_1st,
64 [ $chash, $found, $cur ]);
65 if ($min && scalar(keys %$found) >= $min) {
73 scalar(keys %$found) ? $found : undef;
76 # returns true if $eml is indexed by lei/store and keywords don't match
78 my ($self, $eml, $new_kw_sorted) = @_;
79 my $found = xids_for($self, $eml, 1) // return;
80 my ($num) = values %$found;
81 my @cur_kw = msg_keywords($self, $num);
82 join("\0", @$new_kw_sorted) eq join("\0", @cur_kw) ? 0 : 1;