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>
4 # read-only counterpart for PublicInbox::LeiStore
5 package PublicInbox::LeiSearch;
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);
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));
20 sub msg_keywords { # array or hashref
21 my ($self, $num) = @_;
22 $self->retry_reopen(\&_msg_kw, $num);
25 sub _oid_kw { # retry_reopen callback
26 my ($self, $nums) = @_;
27 my $xdb = $self->xdb; # set {nshard};
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);
37 # returns undef if blob is unknown
39 my ($self, $oidbin) = @_;
40 my @num = $self->over->oidbin_exists($oidbin) or return;
41 $self->retry_reopen(\&_oid_kw, \@num);
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);
54 if ($want_label) { # JSON/JMAP only
55 $x = xap_terms('L', $doc);
59 $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
60 $smsg->{L} = [ sort keys %L ] if scalar(keys(%L));
63 # lookup keywords+labels for external messages
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 $@;
71 # when a message has no Message-IDs at all, this is needed for
72 # unsent Draft messages, at least
75 my $dig = content_digest($eml);
76 my $chash = $dig->clone->digest;
77 my $mids = mids_for_index($eml);
79 $eml->{-lei_fake_mid} = $mids->[0] =
80 PublicInbox::Import::digest2mid($dig, $eml, 0);
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};
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
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;
103 # no lms when used via {ale}:
104 my $lms = $self->{-lms_ro} //= lms($self) if defined($self->{topdir});
105 for my $mid (@$mids) {
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;
120 $git->async_wait_all;
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} //= $_ }
128 scalar(keys %$xoids) ? $xoids : undef;
131 # returns true if $eml is indexed by lei/store and keywords don't match
133 my ($self, $eml, $new_kw_sorted, $docids) = @_;
136 my $xoids = xoids_for($self, $eml) // return;
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);
144 for my $id (@$docids) {
145 $cur_kw = eval { msg_keywords($self, $id) } and last;
147 if (!defined($cur_kw) && $@) {
148 $docids = join(', num:', @$docids);
149 croak "E: num:$docids keyword lookup failure: $@";
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};
157 $cur_kw = join("\0", sort keys %$cur_kw);
158 join("\0", @$new_kw_sorted) eq $cur_kw ? 0 : 1;
162 my ($self, $pfx) = @_;
163 my $xdb = $self->xdb;
164 my $cur = $xdb->allterms_begin($pfx);
165 my $end = $xdb->allterms_end($pfx);
167 for (; $cur != $end; $cur++) {
168 my $tn = $cur->get_termname;
169 index($tn, $pfx) == 0 and
170 $ret{substr($tn, length($pfx))} = undef;
172 wantarray ? (sort keys %ret) : \%ret;
177 my $qp = $self->SUPER::qparse_new; # PublicInbox::Search
178 $qp->add_boolean_prefix('kw', 'K');
179 $qp->add_boolean_prefix('L', 'L');
185 require PublicInbox::LeiMailSync;
186 my $f = "$self->{topdir}/mail_sync.sqlite3";
187 -f $f ? PublicInbox::LeiMailSync->new($f) : undef;
190 # allow SolverGit->resolve_patch to work with "lei index"
192 my ($self, $smsg) = @_;
193 PublicInbox::Inbox::smsg_eml($self, $smsg) // do {
194 my $lms = lms($self);
195 my $bref = $lms ? $lms->local_blob($smsg->{blob}, 1) : undef;
196 $bref ? PublicInbox::Eml->new($bref) : undef;