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;
31 my ($self, $smsg) = @_;
32 return if $smsg->{kw};
33 my $xdb = $self->xdb; # set {nshard};
35 $kw{flagged} = 1 if delete($smsg->{lei_q_tt_flagged});
36 my @num = $self->over->blob_exists($smsg->{blob});
37 for my $num (@num) { # there should only be one...
38 my $kw = xap_terms('K', $xdb, num2docid($self, $num));
41 $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
44 # when a message has no Message-IDs at all, this is needed for
45 # unsent Draft messages, at least
48 my $dig = content_digest($eml);
49 my $chash = $dig->clone->digest;
50 my $mids = mids_in($eml,
51 qw(Message-ID X-Alt-Message-ID Resent-Message-ID));
53 $eml->{-lei_fake_mid} = $mids->[0] =
54 PublicInbox::Import::digest2mid($dig, $eml);
59 sub _cmp_1st { # git->cat_async callback
60 my ($bref, $oid, $type, $size, $cmp) = @_; # cmp: [chash, xoids, smsg]
61 if ($bref && content_hash(PublicInbox::Eml->new($bref)) eq $cmp->[0]) {
62 $cmp->[1]->{$oid} = $cmp->[2]->{num};
66 # returns { OID => num } mapping for $eml matches
67 # The `num' hash value only makes sense from LeiSearch itself
68 # and is nonsense from the PublicInbox::LeiALE subclass
70 my ($self, $eml, $min) = @_;
71 my ($chash, $mids) = content_key($eml);
72 my @overs = ($self->over // $self->overs_all);
75 for my $mid (@$mids) {
78 while (my $cur = $o->next_by_mid($mid, \$id, \$prev)) {
79 next if $cur->{bytes} == 0 ||
80 $xoids->{$cur->{blob}};
81 $git->cat_async($cur->{blob}, \&_cmp_1st,
82 [ $chash, $xoids, $cur ]);
83 if ($min && scalar(keys %$xoids) >= $min) {
91 scalar(keys %$xoids) ? $xoids : undef;
94 # returns true if $eml is indexed by lei/store and keywords don't match
96 my ($self, $eml, $new_kw_sorted) = @_;
97 my $xoids = xoids_for($self, $eml, 1) // return;
98 my ($num) = values %$xoids;
99 my @cur_kw = msg_keywords($self, $num);
100 join("\0", @$new_kw_sorted) eq join("\0", @cur_kw) ? 0 : 1;