]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSearch.pm
lei note-event: ignore kw_changed exceptions
[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); # PublicInbox::Search->reopen
9 use PublicInbox::Search qw(xap_terms);
10 use PublicInbox::ContentHash qw(content_digest content_hash);
11 use PublicInbox::MID qw(mids mids_for_index);
12 use Carp qw(croak);
13
14 # get combined docid from over.num:
15 # (not generic Xapian, only works with our sharding scheme)
16 sub num2docid ($$) {
17         my ($self, $num) = @_;
18         my $nshard = $self->{nshard};
19         ($num - 1) * $nshard + $num % $nshard + 1;
20 }
21
22 sub _msg_kw { # retry_reopen callback
23         my ($self, $num) = @_;
24         my $xdb = $self->xdb; # set {nshard} for num2docid;
25         xap_terms('K', $xdb, num2docid($self, $num));
26 }
27
28 sub msg_keywords { # array or hashref
29         my ($self, $num) = @_;
30         $self->retry_reopen(\&_msg_kw, $num);
31 }
32
33 sub _oid_kw { # retry_reopen callback
34         my ($self, $nums) = @_;
35         my $xdb = $self->xdb; # set {nshard};
36         my %kw;
37         for my $num (@$nums) { # there should only be one...
38                 my $doc = $xdb->get_document(num2docid($self, $num));
39                 my $x = xap_terms('K', $doc);
40                 %kw = (%kw, %$x);
41         }
42         \%kw;
43 }
44
45 # returns undef if blob is unknown
46 sub oidbin_keywords {
47         my ($self, $oidbin) = @_;
48         my @num = $self->over->oidbin_exists($oidbin) or return;
49         $self->retry_reopen(\&_oid_kw, \@num);
50 }
51
52 sub _xsmsg_vmd { # retry_reopen
53         my ($self, $smsg, $want_label) = @_;
54         my $xdb = $self->xdb; # set {nshard};
55         my (%kw, %L, $doc, $x);
56         $kw{flagged} = 1 if delete($smsg->{lei_q_tt_flagged});
57         my @num = $self->over->blob_exists($smsg->{blob});
58         for my $num (@num) { # there should only be one...
59                 $doc = $xdb->get_document(num2docid($self, $num));
60                 $x = xap_terms('K', $doc);
61                 %kw = (%kw, %$x);
62                 if ($want_label) { # JSON/JMAP only
63                         $x = xap_terms('L', $doc);
64                         %L = (%L, %$x);
65                 }
66         }
67         $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
68         $smsg->{L} = [ sort keys %L ] if scalar(keys(%L));
69 }
70
71 # lookup keywords+labels for external messages
72 sub xsmsg_vmd {
73         my ($self, $smsg, $want_label) = @_;
74         return if $smsg->{kw}; # already set by LeiXSearch->mitem_kw
75         eval { $self->retry_reopen(\&_xsmsg_vmd, $smsg, $want_label) };
76         warn "$$ $0 (nshard=$self->{nshard}) $smsg->{blob}: $@" if $@;
77 }
78
79 # when a message has no Message-IDs at all, this is needed for
80 # unsent Draft messages, at least
81 sub content_key ($) {
82         my ($eml) = @_;
83         my $dig = content_digest($eml);
84         my $chash = $dig->clone->digest;
85         my $mids = mids_for_index($eml);
86         unless (@$mids) {
87                 $eml->{-lei_fake_mid} = $mids->[0] =
88                                 PublicInbox::Import::digest2mid($dig, $eml, 0);
89         }
90         ($chash, $mids);
91 }
92
93 sub _cmp_1st { # git->cat_async callback
94         my ($bref, $oid, $type, $size, $cmp) = @_;
95         # cmp: [chash, xoids, smsg, lms]
96         $bref //= $cmp->[3] ? $cmp->[3]->local_blob($oid, 1) : undef;
97         if ($bref && content_hash(PublicInbox::Eml->new($bref)) eq $cmp->[0]) {
98                 $cmp->[1]->{$oid} = $cmp->[2]->{num};
99         }
100 }
101
102 # returns { OID => num } mapping for $eml matches
103 # The `num' hash value only makes sense from LeiSearch itself
104 # and is nonsense from the PublicInbox::LeiALE subclass
105 sub xoids_for {
106         my ($self, $eml, $min) = @_;
107         my ($chash, $mids) = content_key($eml);
108         my @overs = ($self->over // $self->overs_all);
109         my $git = $self->git;
110         my $xoids = {};
111         # no lms when used via {ale}:
112         my $lms = $self->{-lms_ro} //= lms($self) if defined($self->{topdir});
113         for my $mid (@$mids) {
114                 for my $o (@overs) {
115                         my ($id, $prev);
116                         while (my $cur = $o->next_by_mid($mid, \$id, \$prev)) {
117                                 next if $cur->{bytes} == 0 ||
118                                         $xoids->{$cur->{blob}};
119                                 $git->cat_async($cur->{blob}, \&_cmp_1st,
120                                                 [$chash, $xoids, $cur, $lms]);
121                                 if ($min && scalar(keys %$xoids) >= $min) {
122                                         $git->cat_async_wait;
123                                         return $xoids;
124                                 }
125                         }
126                 }
127         }
128         $git->cat_async_wait;
129         scalar(keys %$xoids) ? $xoids : undef;
130 }
131
132 # returns true if $eml is indexed by lei/store and keywords don't match
133 sub kw_changed {
134         my ($self, $eml, $new_kw_sorted, $docids) = @_;
135         my $cur_kw;
136         if ($eml) {
137                 my $xoids = xoids_for($self, $eml) // return;
138                 $docids //= [];
139                 @$docids = sort { $a <=> $b } values %$xoids;
140         }
141         for my $id (@$docids) {
142                 $cur_kw = eval { msg_keywords($self, $id) } and last;
143         }
144         if (!defined($cur_kw) && $@) {
145                 $docids = join(', num:', @$docids);
146                 croak "E: num:$docids keyword lookup failure: $@";
147         }
148         # RFC 5550 sec 5.9 on the $Forwarded keyword states:
149         # "Once set, the flag SHOULD NOT be cleared"
150         if (exists($cur_kw->{forwarded}) &&
151                         !grep(/\Aforwarded\z/, @$new_kw_sorted)) {
152                 delete $cur_kw->{forwarded};
153         }
154         $cur_kw = join("\0", sort keys %$cur_kw);
155         join("\0", @$new_kw_sorted) eq $cur_kw ? 0 : 1;
156 }
157
158 sub all_terms {
159         my ($self, $pfx) = @_;
160         my $xdb = $self->xdb;
161         my $cur = $xdb->allterms_begin($pfx);
162         my $end = $xdb->allterms_end($pfx);
163         my %ret;
164         for (; $cur != $end; $cur++) {
165                 my $tn = $cur->get_termname;
166                 index($tn, $pfx) == 0 and
167                         $ret{substr($tn, length($pfx))} = undef;
168         }
169         wantarray ? (sort keys %ret) : \%ret;
170 }
171
172 sub qparse_new {
173         my ($self) = @_;
174         my $qp = $self->SUPER::qparse_new; # PublicInbox::Search
175         $qp->add_boolean_prefix('kw', 'K');
176         $qp->add_boolean_prefix('L', 'L');
177         $qp
178 }
179
180 sub lms {
181         my ($self) = @_;
182         require PublicInbox::LeiMailSync;
183         my $f = "$self->{topdir}/mail_sync.sqlite3";
184         -f $f ? PublicInbox::LeiMailSync->new($f) : undef;
185 }
186
187 # allow SolverGit->resolve_patch to work with "lei index"
188 sub smsg_eml {
189         my ($self, $smsg) = @_;
190         PublicInbox::Inbox::smsg_eml($self, $smsg) // do {
191                 my $lms = lms($self);
192                 my $bref = $lms ? $lms->local_blob($smsg->{blob}, 1) : undef;
193                 $bref ? PublicInbox::Eml->new($bref) : undef;
194         };
195 }
196
197 1;