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
19 PublicInbox::Search::load_xapian();
21 qp_flags => $PublicInbox::Search::QP_FLAGS |
22 PublicInbox::Search::FLAG_PURE_NOT(),
27 my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
29 if (!$ibxish->can('over') || !$ibxish->over) {
30 return push(@{$self->{remotes}}, $ibxish)
32 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
33 my $srch = $ibxish->search or
34 return warn("$desc not indexed for Xapian\n");
35 my @shards = $srch->xdb_shards_flat or
36 return warn("$desc has no Xapian shardsXapian\n");
38 if (delete $self->{xdb}) { # XXX: do we need this?
39 # clobber existing {xdb} if amending
40 my $expect = delete $self->{nshard};
41 my $shards = delete $self->{shards_flat};
42 scalar(@$shards) == $expect or die
43 "BUG: {nshard}$expect != shards=".scalar(@$shards);
46 for my $old_ibxish (@{$self->{shard2ibx}}) {
47 next if $prev == $old_ibxish;
49 my @shards = $old_ibxish->search->xdb_shards_flat;
50 push @{$self->{shards_flat}}, @shards;
52 my $nr = scalar(@{$self->{shards_flat}});
54 "BUG: reloaded $nr shards, expected $expect"
56 push @{$self->{shards_flat}}, @shards;
57 push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
60 # returns a list of local inboxes (or count in scalar context)
62 my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
66 # called by PublicInbox::Search::xdb
67 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
71 my ($self, $mitem) = @_;
72 # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
73 my $nshard = $self->{nshard};
74 my $docid = $mitem->get_docid;
75 my $shard = ($docid - 1) % $nshard;
76 my $num = int(($docid - 1) / $nshard) + 1;
77 my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
78 $smsg->{docid} = $docid;
83 my ($self, $qstr, $opt) = @_;
85 $opt->{relevance} //= -2;
86 $self->mset($qstr //= 'bytes:1..', $opt);
93 my $size = $mset->size;
94 $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
97 sub query_thread_mset { # for --thread
98 my ($self, $lei, $ibxish) = @_;
99 my %sig = $lei->atfork_child_wq($self);
100 local @SIG{keys %sig} = values %sig;
102 my ($srch, $over) = ($ibxish->search, $ibxish->over);
103 unless ($srch && $over) {
104 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
105 warn "$desc not indexed by Xapian\n";
108 my $mo = { %{$lei->{mset_opt}} };
110 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $ibxish);
111 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
112 $dedupe->prepare_dedupe;
114 $mset = $srch->mset($mo->{qstr}, $mo);
115 my $ids = $srch->mset_to_artnums($mset, $mo);
116 my $ctx = { ids => $ids };
118 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
119 while ($over->expand_thread($ctx)) {
120 for my $n (@{$ctx->{xids}}) {
121 my $smsg = $over->get_art($n) or next;
122 next if $dedupe->is_smsg_dup($smsg);
123 my $mitem = delete $n2item{$smsg->{num}};
124 $each_smsg->($smsg, $mitem);
126 @{$ctx->{xids}} = ();
128 } while (_mset_more($mset, $mo));
129 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
130 $lei->{ovv}->ovv_atexit_child($lei);
133 sub query_mset { # non-parallel for non-"--thread" users
134 my ($self, $lei, $srcs) = @_;
135 my %sig = $lei->atfork_child_wq($self);
136 local @SIG{keys %sig} = values %sig;
137 my $mo = { %{$lei->{mset_opt}} };
139 $self->attach_external($_) for @$srcs;
140 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $self);
141 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
142 $dedupe->prepare_dedupe;
144 $mset = $self->mset($mo->{qstr}, $mo);
145 for my $it ($mset->items) {
146 my $smsg = smsg_for($self, $it) or next;
147 next if $dedupe->is_smsg_dup($smsg);
148 $each_smsg->($smsg, $it);
150 } while (_mset_more($mset, $mo));
151 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
152 $lei->{ovv}->ovv_atexit_child($lei);
158 my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
159 for my $ibx (@{$self->{shard2ibx} // []}) {
160 my $d = File::Spec->canonpath($ibx->git->{git_dir});
161 $seen{$d} //= push @dirs, "$d/objects\n"
163 my $git_dir = $tmp->dirname;
164 PublicInbox::Import::init_bare($git_dir);
165 my $f = "$git_dir/objects/info/alternates";
166 open my $alt, '>', $f or die "open($f): $!";
167 print $alt @dirs or die "print $f: $!";
168 close $alt or die "close $f: $!";
169 my $git = PublicInbox::Git->new($git_dir);
174 sub query_done { # EOF callback
175 my ($self, $lei) = @_;
176 my $l2m = delete $lei->{l2m};
177 if (my $pids = delete $self->{l2m_pids}) {
178 my $ipc_worker_reap = $self->can('ipc_worker_reap');
179 dwaitpid($_, $ipc_worker_reap, $l2m) for @$pids;
181 $lei->{ovv}->ovv_end($lei);
182 $lei->start_mua if $l2m && !$l2m->lock_free;
186 sub start_query { # always runs in main (lei-daemon) process
187 my ($self, $io, $lei, $srcs) = @_;
188 if (my $l2m = $lei->{l2m}) {
189 $lei->{1} = $io->[1];
190 $l2m->post_augment($lei);
191 $io->[1] = delete $lei->{1};
192 $lei->start_mua($io->[3]) if $l2m->lock_free;
194 my $remotes = $self->{remotes} // [];
195 if ($lei->{opt}->{thread}) {
196 for my $ibxish (@$srcs) {
197 $self->wq_do('query_thread_mset', $io, $lei, $ibxish);
200 $self->wq_do('query_mset', $io, $lei, $srcs);
203 for my $rmt (@$remotes) {
204 $self->wq_do('query_thread_mbox', $io, $lei, $rmt);
206 close $io->[0]; # qry_status_wr
210 sub query_prepare { # wq_do
211 my ($self, $lei) = @_;
212 my %sig = $lei->atfork_child_wq($self);
213 local @SIG{keys %sig} = values %sig;
214 if (my $l2m = $lei->{l2m}) {
215 eval { $l2m->do_augment($lei) };
216 return $lei->fail($@) if $@;
218 # trigger PublicInbox::OpPipe->event_step
219 my $qry_status_wr = $lei->{0} or
220 return $lei->fail('BUG: qry_status_wr missing');
221 $qry_status_wr->autoflush(1);
222 print $qry_status_wr '.' or # this should never fail...
223 return $lei->fail("BUG? print qry_status_wr: $!");
227 my ($self, $lei_orig, $srcs) = @_;
228 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
230 pipe(my $qry_status_rd, $io[0]) or die "pipe $!";
232 $lei_orig->event_step_init; # wait for shutdowns
233 my $op_map = { '' => [ \&query_done, $self, $lei_orig ] };
234 my $in_loop = exists $lei_orig->{sock};
235 my $opp = PublicInbox::OpPipe->new($qry_status_rd, $op_map, $in_loop);
236 my $l2m = $lei->{l2m};
238 $l2m->pre_augment($lei_orig); # may redirect $lei->{1} for mbox
239 $io[1] = $lei_orig->{1};
240 $op_map->{'.'} = [ \&start_query, $self, \@io, $lei, $srcs ];
241 $self->wq_do('query_prepare', \@io, $lei);
242 $opp->event_step if !$in_loop;
244 start_query($self, \@io, $lei, $srcs);
247 my @pids = $self->wq_close;
248 # for the $lei->atfork_child_wq PIPE handler:
249 $op_map->{'!'} = [ \&CORE::kill, 'TERM', @pids ];
251 my $ipc_worker_reap = $self->can('ipc_worker_reap');
252 if (my $l2m_pids = delete $self->{l2m_pids}) {
253 dwaitpid($_, $ipc_worker_reap, $l2m) for @$l2m_pids;
255 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
259 sub ipc_atfork_prepare {
261 # (qry_status_wr, stdout|mbox, stderr, 3: sock, 4: $l2m->{-wq_s1})
262 $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&=]);
263 $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC