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