]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSearch.pm
lei q: do not import unnecessarily from externals
[public-inbox.git] / lib / PublicInbox / LeiSearch.pm
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>
3
4 # read-only counterpart for PublicInbox::LeiStore
5 package PublicInbox::LeiSearch;
6 use strict;
7 use v5.10.1;
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);
12
13 # get combined docid from over.num:
14 # (not generic Xapian, only works with our sharding scheme)
15 sub num2docid ($$) {
16         my ($self, $num) = @_;
17         my $nshard = $self->{nshard};
18         ($num - 1) * $nshard + $num % $nshard + 1;
19 }
20
21 sub msg_keywords {
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;
28 }
29
30 # when a message has no Message-IDs at all, this is needed for
31 # unsent Draft messages, at least
32 sub content_key ($) {
33         my ($eml) = @_;
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));
38         unless (@$mids) {
39                 $eml->{-lei_fake_mid} = $mids->[0] =
40                                 PublicInbox::Import::digest2mid($dig, $eml);
41         }
42         ($chash, $mids);
43 }
44
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};
49         }
50 }
51
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);
56         my $git = $self->git;
57         my $found = {};
58         for my $mid (@$mids) {
59                 for my $o (@overs) {
60                         my ($id, $prev);
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) {
66                                         $git->cat_async_wait;
67                                         return $found;
68                                 }
69                         }
70                 }
71         }
72         $git->cat_async_wait;
73         scalar(keys %$found) ? $found : undef;
74 }
75
76 # returns true if $eml is indexed by lei/store and keywords don't match
77 sub kw_changed {
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;
83 }
84
85 1;