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