]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSearch.pm
lei q: support vmd for external-only messages
[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 sub xsmsg_vmd {
31         my ($self, $smsg) = @_;
32         return if $smsg->{kw};
33         my $xdb = $self->xdb; # set {nshard};
34         my %kw;
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));
39                 %kw = (%kw, %$kw);
40         }
41         $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
42 }
43
44 # when a message has no Message-IDs at all, this is needed for
45 # unsent Draft messages, at least
46 sub content_key ($) {
47         my ($eml) = @_;
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));
52         unless (@$mids) {
53                 $eml->{-lei_fake_mid} = $mids->[0] =
54                                 PublicInbox::Import::digest2mid($dig, $eml);
55         }
56         ($chash, $mids);
57 }
58
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};
63         }
64 }
65
66 sub xoids_for { # returns { OID => docid } mapping for $eml matches
67         my ($self, $eml, $min) = @_;
68         my ($chash, $mids) = content_key($eml);
69         my @overs = ($self->over // $self->overs_all);
70         my $git = $self->git;
71         my $xoids = {};
72         for my $mid (@$mids) {
73                 for my $o (@overs) {
74                         my ($id, $prev);
75                         while (my $cur = $o->next_by_mid($mid, \$id, \$prev)) {
76                                 next if $cur->{bytes} == 0 ||
77                                         $xoids->{$cur->{blob}};
78                                 $git->cat_async($cur->{blob}, \&_cmp_1st,
79                                                 [ $chash, $xoids, $cur ]);
80                                 if ($min && scalar(keys %$xoids) >= $min) {
81                                         $git->cat_async_wait;
82                                         return $xoids;
83                                 }
84                         }
85                 }
86         }
87         $git->cat_async_wait;
88         scalar(keys %$xoids) ? $xoids : undef;
89 }
90
91 # returns true if $eml is indexed by lei/store and keywords don't match
92 sub kw_changed {
93         my ($self, $eml, $new_kw_sorted) = @_;
94         my $xoids = xoids_for($self, $eml, 1) // return;
95         my ($num) = values %$xoids;
96         my @cur_kw = msg_keywords($self, $num);
97         join("\0", @$new_kw_sorted) eq join("\0", @cur_kw) ? 0 : 1;
98 }
99
100 1;