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 # Combine any combination of PublicInbox::Search,
5 # PublicInbox::ExtSearch, and PublicInbox::LeiSearch objects
7 package PublicInbox::LeiXSearch;
10 use parent qw(PublicInbox::LeiSearch PublicInbox::IPC);
11 use PublicInbox::DS qw(dwaitpid);
12 use PublicInbox::OpPipe;
13 use PublicInbox::Import;
14 use File::Temp 0.19 (); # 0.19 for ->newdir
16 use PublicInbox::Search qw(xap_terms);
20 PublicInbox::Search::load_xapian();
22 qp_flags => $PublicInbox::Search::QP_FLAGS |
23 PublicInbox::Search::FLAG_PURE_NOT(),
28 my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
29 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
30 my $srch = $ibxish->search or
31 return warn("$desc not indexed for Xapian\n");
32 my @shards = $srch->xdb_shards_flat or
33 return warn("$desc has no Xapian shardsXapian\n");
35 if (delete $self->{xdb}) { # XXX: do we need this?
36 # clobber existing {xdb} if amending
37 my $expect = delete $self->{nshard};
38 my $shards = delete $self->{shards_flat};
39 scalar(@$shards) == $expect or die
40 "BUG: {nshard}$expect != shards=".scalar(@$shards);
43 for my $old_ibxish (@{$self->{shard2ibx}}) {
44 next if $prev == $old_ibxish;
46 my @shards = $old_ibxish->search->xdb_shards_flat;
47 push @{$self->{shards_flat}}, @shards;
49 my $nr = scalar(@{$self->{shards_flat}});
51 "BUG: reloaded $nr shards, expected $expect"
53 push @{$self->{shards_flat}}, @shards;
54 push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
57 # returns a list of local inboxes (or count in scalar context)
58 sub locals { @{$_[0]->{locals} // []} }
60 sub remotes { @{$_[0]->{remotes} // []} }
62 # called by PublicInbox::Search::xdb
63 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
67 my ($self, $mitem) = @_;
68 # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
69 my $nshard = $self->{nshard};
70 my $docid = $mitem->get_docid;
71 my $shard = ($docid - 1) % $nshard;
72 my $num = int(($docid - 1) / $nshard) + 1;
73 my $ibx = $self->{shard2ibx}->[$shard];
74 my $smsg = $ibx->over->get_art($num);
75 if (ref($ibx->can('msg_keywords'))) {
76 my $kw = xap_terms('K', $mitem->get_document);
77 $smsg->{kw} = [ sort keys %$kw ];
79 $smsg->{docid} = $docid;
84 my ($self, $qstr, $opt) = @_;
86 $opt->{relevance} //= -2;
87 $self->mset($qstr //= 'bytes:1..', $opt);
94 my $size = $mset->size;
95 $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
98 # $startq will EOF when query_prepare is done augmenting and allow
99 # query_mset and query_thread_mset to proceed.
100 sub wait_startq ($) {
103 read($startq, my $query_prepare_done, 1);
106 sub query_thread_mset { # for --thread
107 my ($self, $lei, $ibxish) = @_;
108 local $0 = "$0 query_thread_mset";
109 my $startq = delete $self->{5};
110 my %sig = $lei->atfork_child_wq($self);
111 local @SIG{keys %sig} = values %sig;
113 my ($srch, $over) = ($ibxish->search, $ibxish->over);
114 unless ($srch && $over) {
115 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
116 warn "$desc not indexed by Xapian\n";
119 my $mo = { %{$lei->{mset_opt}} };
121 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $ibxish);
122 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
123 $dedupe->prepare_dedupe;
125 $mset = $srch->mset($mo->{qstr}, $mo);
126 my $ids = $srch->mset_to_artnums($mset, $mo);
127 my $ctx = { ids => $ids };
129 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
130 while ($over->expand_thread($ctx)) {
131 for my $n (@{$ctx->{xids}}) {
132 my $smsg = $over->get_art($n) or next;
133 wait_startq($startq) if $startq;
134 next if $dedupe->is_smsg_dup($smsg);
135 my $mitem = delete $n2item{$smsg->{num}};
136 $each_smsg->($smsg, $mitem);
138 @{$ctx->{xids}} = ();
140 } while (_mset_more($mset, $mo));
141 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
142 $lei->{ovv}->ovv_atexit_child($lei);
145 sub query_mset { # non-parallel for non-"--thread" users
146 my ($self, $lei) = @_;
147 local $0 = "$0 query_mset";
148 my $startq = delete $self->{5};
149 my %sig = $lei->atfork_child_wq($self);
150 local @SIG{keys %sig} = values %sig;
151 my $mo = { %{$lei->{mset_opt}} };
153 for my $loc (locals($self)) {
154 attach_external($self, $loc);
156 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $self);
157 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
158 $dedupe->prepare_dedupe;
160 $mset = $self->mset($mo->{qstr}, $mo);
161 for my $mitem ($mset->items) {
162 my $smsg = smsg_for($self, $mitem) or next;
163 wait_startq($startq) if $startq;
164 next if $dedupe->is_smsg_dup($smsg);
165 $each_smsg->($smsg, $mitem);
167 } while (_mset_more($mset, $mo));
168 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
169 $lei->{ovv}->ovv_atexit_child($lei);
172 sub query_remote_mboxrd {
173 my ($self, $lei, $uri) = @_;
179 my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
180 for my $ibx (@{$self->{shard2ibx} // []}) {
181 my $d = File::Spec->canonpath($ibx->git->{git_dir});
182 $seen{$d} //= push @dirs, "$d/objects\n"
184 my $git_dir = $tmp->dirname;
185 PublicInbox::Import::init_bare($git_dir);
186 my $f = "$git_dir/objects/info/alternates";
187 open my $alt, '>', $f or die "open($f): $!";
188 print $alt @dirs or die "print $f: $!";
189 close $alt or die "close $f: $!";
190 my $git = PublicInbox::Git->new($git_dir);
195 sub query_done { # EOF callback
197 my $has_l2m = exists $lei->{l2m};
198 for my $f (qw(lxs l2m)) {
199 my $wq = delete $lei->{$f} or next;
202 $lei->{ovv}->ovv_end($lei);
203 if ($has_l2m) { # close() calls LeiToMail reap_compress
204 close(delete($lei->{1})) if $lei->{1};
210 sub do_post_augment {
211 my ($lei, $zpipe, $au_done) = @_;
212 my $l2m = $lei->{l2m} or die 'BUG: no {l2m}';
213 eval { $l2m->post_augment($lei, $zpipe) };
215 if (my $lxs = delete $lei->{lxs}) {
221 close $au_done; # triggers wait_startq
224 sub start_query { # always runs in main (lei-daemon) process
225 my ($self, $io, $lei) = @_;
226 if ($lei->{opt}->{thread}) {
227 for my $ibxish (locals($self)) {
228 $self->wq_do('query_thread_mset', $io, $lei, $ibxish);
231 $self->wq_do('query_mset', $io, $lei);
234 for my $uri (remotes($self)) {
235 $self->wq_do('query_remote_mboxrd', $io, $lei, $uri);
240 sub query_prepare { # called by wq_do
241 my ($self, $lei) = @_;
242 local $0 = "$0 query_prepare";
243 my %sig = $lei->atfork_child_wq($self);
244 -p $lei->{0} or die "BUG: \$done pipe expected";
245 local @SIG{keys %sig} = values %sig;
246 eval { $lei->{l2m}->do_augment($lei) };
247 $lei->fail($@) if $@;
248 syswrite($lei->{0}, '.') == 1 or die "do_post_augment trigger: $!";
251 sub sigpipe_handler { # handles SIGPIPE from l2m/lxs workers
253 my $lxs = delete $lei->{lxs};
254 if ($lxs && $lxs->wq_kill_old) {
258 close(delete $lei->{1}) if $lei->{1};
262 my ($self, $lei_orig) = @_;
263 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
265 pipe(my $done, $io[0]) or die "pipe $!";
267 $lei_orig->event_step_init; # wait for shutdowns
269 '' => [ \&query_done, $lei_orig ],
270 '!' => [ \&sigpipe_handler, $lei_orig ]
272 my $in_loop = exists $lei_orig->{sock};
273 $done = PublicInbox::OpPipe->new($done, $done_op, $in_loop);
274 my $l2m = $lei->{l2m};
276 # may redirect $lei->{1} for mbox
277 my $zpipe = $l2m->pre_augment($lei_orig);
278 $io[1] = $lei_orig->{1};
279 pipe(my ($startq, $au_done)) or die "pipe: $!";
280 $done_op->{'.'} = [ \&do_post_augment, $lei_orig,
282 local $io[4] = *STDERR{GLOB}; # don't send l2m->{-wq_s1}
283 die "BUG: unexpected \$io[5]: $io[5]" if $io[5];
284 $self->wq_do('query_prepare', \@io, $lei);
285 fcntl($startq, 1031, 4096) if $^O eq 'linux'; # F_SETPIPE_SZ
287 $io[1] = $zpipe->[1] if $zpipe;
289 start_query($self, \@io, $lei);
292 # for the $lei->atfork_child_wq PIPE handler:
293 while ($done->{sock}) { $done->event_step }
297 sub ipc_atfork_prepare {
299 # (0: done_wr, 1: stdout|mbox, 2: stderr,
300 # 3: sock, 4: $l2m->{-wq_s1}, 5: $startq)
301 $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&= <&=]);
302 $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
305 sub prepare_external {
306 my ($self, $loc, $boost) = @_; # n.b. already ordered by boost
307 if (ref $loc) { # already a URI, or PublicInbox::Inbox-like object
308 return push(@{$self->{remotes}}, $loc) if $loc->can('scheme');
309 } elsif ($loc =~ m!\Ahttps?://!) {
311 return push(@{$self->{remotes}}, URI->new($loc));
312 } elsif (-f "$loc/ei.lock") {
313 require PublicInbox::ExtSearch;
314 $loc = PublicInbox::ExtSearch->new($loc);
315 } elsif (-f "$loc/inbox.lock" || -d "$loc/public-inbox") {
316 require PublicInbox::Inbox; # v2, v1
317 $loc = bless { inboxdir => $loc }, 'PublicInbox::Inbox';
319 warn "W: ignoring $loc, unable to determine type\n";
322 push @{$self->{locals}}, $loc;