]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSearch.pm
lei q: import flags when clobbering/augmenting Maildirs
[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         return if defined($cmp->[1]->[0]); # $found->[0]
48         if (content_hash(PublicInbox::Eml->new($bref)) eq $cmp->[0]) {
49                 push @{$cmp->[1]}, $cmp->[2]->{num};
50         }
51 }
52
53 # returns true if $eml is indexed by lei/store and keywords don't match
54 sub kw_changed {
55         my ($self, $eml, $new_kw_sorted) = @_;
56         my ($chash, $mids) = content_key($eml);
57         my $over = $self->over;
58         my $git = $self->git;
59         my $found = [];
60         for my $mid (@$mids) {
61                 my ($id, $prev);
62                 while (my $cur = $over->next_by_mid($mid, \$id, \$prev)) {
63                         $git->cat_async($cur->{blob}, \&_cmp_1st,
64                                         [ $chash, $found, $cur ]);
65                         last if scalar(@$found);
66                 }
67         }
68         $git->cat_async_wait;
69         my $num = $found->[0] // return;
70         my @cur_kw = msg_keywords($self, $num);
71         join("\0", @$new_kw_sorted) eq join("\0", @cur_kw) ? 0 : 1;
72 }
73
74 1;