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