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