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);
11 use PublicInbox::MID qw(mids mids_for_index);
14 # get combined docid from over.num:
15 # (not generic Xapian, only works with our sharding scheme)
17 my ($self, $num) = @_;
18 my $nshard = $self->{nshard};
19 ($num - 1) * $nshard + $num % $nshard + 1;
22 sub _msg_kw { # retry_reopen callback
23 my ($self, $num) = @_;
24 my $xdb = $self->xdb; # set {nshard} for num2docid;
25 xap_terms('K', $xdb, num2docid($self, $num));
28 sub msg_keywords { # array or hashref
29 my ($self, $num) = @_;
30 $self->retry_reopen(\&_msg_kw, $num);
33 sub _oid_kw { # retry_reopen callback
34 my ($self, $nums) = @_;
35 my $xdb = $self->xdb; # set {nshard};
37 for my $num (@$nums) { # there should only be one...
38 my $doc = $xdb->get_document(num2docid($self, $num));
39 my $x = xap_terms('K', $doc);
45 # returns undef if blob is unknown
47 my ($self, $oidbin) = @_;
48 my @num = $self->over->oidbin_exists($oidbin) or return;
49 $self->retry_reopen(\&_oid_kw, \@num);
52 sub _xsmsg_vmd { # retry_reopen
53 my ($self, $smsg, $want_label) = @_;
54 my $xdb = $self->xdb; # set {nshard};
55 my (%kw, %L, $doc, $x);
56 $kw{flagged} = 1 if delete($smsg->{lei_q_tt_flagged});
57 my @num = $self->over->blob_exists($smsg->{blob});
58 for my $num (@num) { # there should only be one...
59 $doc = $xdb->get_document(num2docid($self, $num));
60 $x = xap_terms('K', $doc);
62 if ($want_label) { # JSON/JMAP only
63 $x = xap_terms('L', $doc);
67 $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
68 $smsg->{L} = [ sort keys %L ] if scalar(keys(%L));
71 # lookup keywords+labels for external messages
73 my ($self, $smsg, $want_label) = @_;
74 return if $smsg->{kw}; # already set by LeiXSearch->mitem_kw
75 eval { $self->retry_reopen(\&_xsmsg_vmd, $smsg, $want_label) };
76 warn "$$ $0 (nshard=$self->{nshard}) $smsg->{blob}: $@" if $@;
79 # when a message has no Message-IDs at all, this is needed for
80 # unsent Draft messages, at least
83 my $dig = content_digest($eml);
84 my $chash = $dig->clone->digest;
85 my $mids = mids_for_index($eml);
87 $eml->{-lei_fake_mid} = $mids->[0] =
88 PublicInbox::Import::digest2mid($dig, $eml, 0);
93 sub _cmp_1st { # git->cat_async callback
94 my ($bref, $oid, $type, $size, $cmp) = @_;
95 # cmp: [chash, xoids, smsg, lms]
96 $bref //= $cmp->[3] ? $cmp->[3]->local_blob($oid, 1) : undef;
97 if ($bref && content_hash(PublicInbox::Eml->new($bref)) eq $cmp->[0]) {
98 $cmp->[1]->{$oid} = $cmp->[2]->{num};
102 # returns { OID => num } mapping for $eml matches
103 # The `num' hash value only makes sense from LeiSearch itself
104 # and is nonsense from the PublicInbox::LeiALE subclass
106 my ($self, $eml, $min) = @_;
107 my ($chash, $mids) = content_key($eml);
108 my @overs = ($self->over // $self->overs_all);
109 my $git = $self->git;
111 # no lms when used via {ale}:
112 my $lms = $self->{-lms_ro} //= lms($self) if defined($self->{topdir});
113 for my $mid (@$mids) {
116 while (my $cur = $o->next_by_mid($mid, \$id, \$prev)) {
117 next if $cur->{bytes} == 0 ||
118 $xoids->{$cur->{blob}};
119 $git->cat_async($cur->{blob}, \&_cmp_1st,
120 [$chash, $xoids, $cur, $lms]);
121 if ($min && scalar(keys %$xoids) >= $min) {
122 $git->async_wait_all;
128 $git->async_wait_all;
129 scalar(keys %$xoids) ? $xoids : undef;
132 # returns true if $eml is indexed by lei/store and keywords don't match
134 my ($self, $eml, $new_kw_sorted, $docids) = @_;
137 my $xoids = xoids_for($self, $eml) // return;
139 @$docids = sort { $a <=> $b } values %$xoids;
141 for my $id (@$docids) {
142 $cur_kw = eval { msg_keywords($self, $id) } and last;
144 if (!defined($cur_kw) && $@) {
145 $docids = join(', num:', @$docids);
146 croak "E: num:$docids keyword lookup failure: $@";
148 # RFC 5550 sec 5.9 on the $Forwarded keyword states:
149 # "Once set, the flag SHOULD NOT be cleared"
150 if (exists($cur_kw->{forwarded}) &&
151 !grep(/\Aforwarded\z/, @$new_kw_sorted)) {
152 delete $cur_kw->{forwarded};
154 $cur_kw = join("\0", sort keys %$cur_kw);
155 join("\0", @$new_kw_sorted) eq $cur_kw ? 0 : 1;
159 my ($self, $pfx) = @_;
160 my $xdb = $self->xdb;
161 my $cur = $xdb->allterms_begin($pfx);
162 my $end = $xdb->allterms_end($pfx);
164 for (; $cur != $end; $cur++) {
165 my $tn = $cur->get_termname;
166 index($tn, $pfx) == 0 and
167 $ret{substr($tn, length($pfx))} = undef;
169 wantarray ? (sort keys %ret) : \%ret;
174 my $qp = $self->SUPER::qparse_new; # PublicInbox::Search
175 $qp->add_boolean_prefix('kw', 'K');
176 $qp->add_boolean_prefix('L', 'L');
182 require PublicInbox::LeiMailSync;
183 my $f = "$self->{topdir}/mail_sync.sqlite3";
184 -f $f ? PublicInbox::LeiMailSync->new($f) : undef;
187 # allow SolverGit->resolve_patch to work with "lei index"
189 my ($self, $smsg) = @_;
190 PublicInbox::Inbox::smsg_eml($self, $smsg) // do {
191 my $lms = lms($self);
192 my $bref = $lms ? $lms->local_blob($smsg->{blob}, 1) : undef;
193 $bref ? PublicInbox::Eml->new($bref) : undef;