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