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