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 # $startq will EOF when query_prepare is done augmenting and allow
98 # query_mset and query_thread_mset to proceed.
102 read($startq, my $query_prepare_done, 1);
105 sub query_thread_mset { # for --thread
106 my ($self, $lei, $ibxish) = @_;
107 my $startq = delete $self->{5};
108 my %sig = $lei->atfork_child_wq($self);
109 local @SIG{keys %sig} = values %sig;
111 my ($srch, $over) = ($ibxish->search, $ibxish->over);
112 unless ($srch && $over) {
113 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
114 warn "$desc not indexed by Xapian\n";
117 my $mo = { %{$lei->{mset_opt}} };
119 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $ibxish);
120 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
121 $dedupe->prepare_dedupe;
123 $mset = $srch->mset($mo->{qstr}, $mo);
124 my $ids = $srch->mset_to_artnums($mset, $mo);
125 my $ctx = { ids => $ids };
127 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
128 while ($over->expand_thread($ctx)) {
129 for my $n (@{$ctx->{xids}}) {
130 my $smsg = $over->get_art($n) or next;
131 wait_startq($startq) if $startq;
132 next if $dedupe->is_smsg_dup($smsg);
133 my $mitem = delete $n2item{$smsg->{num}};
134 $each_smsg->($smsg, $mitem);
136 @{$ctx->{xids}} = ();
138 } while (_mset_more($mset, $mo));
139 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
140 $lei->{ovv}->ovv_atexit_child($lei);
143 sub query_mset { # non-parallel for non-"--thread" users
144 my ($self, $lei, $srcs) = @_;
145 my $startq = delete $self->{5};
146 my %sig = $lei->atfork_child_wq($self);
147 local @SIG{keys %sig} = values %sig;
148 my $mo = { %{$lei->{mset_opt}} };
150 $self->attach_external($_) for @$srcs;
151 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $self);
152 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
153 $dedupe->prepare_dedupe;
155 $mset = $self->mset($mo->{qstr}, $mo);
156 for my $it ($mset->items) {
157 my $smsg = smsg_for($self, $it) or next;
158 wait_startq($startq) if $startq;
159 next if $dedupe->is_smsg_dup($smsg);
160 $each_smsg->($smsg, $it);
162 } while (_mset_more($mset, $mo));
163 undef $each_smsg; # drops @io for l2m->{each_smsg_done}
164 $lei->{ovv}->ovv_atexit_child($lei);
170 my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
171 for my $ibx (@{$self->{shard2ibx} // []}) {
172 my $d = File::Spec->canonpath($ibx->git->{git_dir});
173 $seen{$d} //= push @dirs, "$d/objects\n"
175 my $git_dir = $tmp->dirname;
176 PublicInbox::Import::init_bare($git_dir);
177 my $f = "$git_dir/objects/info/alternates";
178 open my $alt, '>', $f or die "open($f): $!";
179 print $alt @dirs or die "print $f: $!";
180 close $alt or die "close $f: $!";
181 my $git = PublicInbox::Git->new($git_dir);
186 sub query_done { # EOF callback
187 my ($self, $lei) = @_;
188 my $l2m = delete $lei->{l2m};
189 if (my $pids = delete $self->{l2m_pids}) {
190 my $ipc_worker_reap = $self->can('ipc_worker_reap');
191 dwaitpid($_, $ipc_worker_reap, $l2m) for @$pids;
193 $lei->{ovv}->ovv_end($lei);
194 $lei->start_mua if $l2m && !$l2m->lock_free;
198 sub start_query { # always runs in main (lei-daemon) process
199 my ($self, $io, $lei, $srcs) = @_;
200 if (my $l2m = $lei->{l2m}) {
201 $lei->{1} = $io->[1];
202 $l2m->post_augment($lei);
203 $io->[1] = delete $lei->{1};
204 $lei->start_mua($io->[3]) if $l2m->lock_free;
206 my $remotes = $self->{remotes} // [];
207 if ($lei->{opt}->{thread}) {
208 for my $ibxish (@$srcs) {
209 $self->wq_do('query_thread_mset', $io, $lei, $ibxish);
212 $self->wq_do('query_mset', $io, $lei, $srcs);
215 for my $rmt (@$remotes) {
216 $self->wq_do('query_thread_mbox', $io, $lei, $rmt);
218 close $io->[0]; # qry_status_wr
222 sub query_prepare { # for wq_do,
223 my ($self, $lei) = @_;
224 my %sig = $lei->atfork_child_wq($self);
225 local @SIG{keys %sig} = values %sig;
226 eval { $lei->{l2m}->do_augment($lei) };
227 $lei->fail($@) if $@;
231 my ($self, $lei_orig, $srcs) = @_;
232 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
234 pipe(my $done, $io[0]) or die "pipe $!";
236 $lei_orig->event_step_init; # wait for shutdowns
237 my $done_op = { '' => [ \&query_done, $self, $lei_orig ] };
238 my $in_loop = exists $lei_orig->{sock};
239 $done = PublicInbox::OpPipe->new($done, $done_op, $in_loop);
240 my $l2m = $lei->{l2m};
242 $l2m->pre_augment($lei_orig); # may redirect $lei->{1} for mbox
243 $io[1] = $lei_orig->{1};
244 my @l2m_io = (undef, @io[1..$#io]);
245 pipe(my $startq, $l2m_io[0]) or die "pipe: $!";
246 $self->wq_do('query_prepare', \@l2m_io, $lei);
247 $io[4] //= *STDERR{GLOB};
248 die "BUG: unexpected \$io[5]: $io[5]" if $io[5];
249 fcntl($startq, 1031, 4096) if $^O eq 'linux'; # F_SETPIPE_SZ
252 start_query($self, \@io, $lei, $srcs);
254 my @pids = $self->wq_close;
255 # for the $lei->atfork_child_wq PIPE handler:
256 $done_op->{'!'} = [ \&CORE::kill, 'TERM', @pids ];
258 my $ipc_worker_reap = $self->can('ipc_worker_reap');
259 if (my $l2m_pids = delete $self->{l2m_pids}) {
260 dwaitpid($_, $ipc_worker_reap, $l2m) for @$l2m_pids;
262 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
266 sub ipc_atfork_prepare {
268 # (0: qry_status_wr, 1: stdout|mbox, 2: stderr,
269 # 3: sock, 4: $l2m->{-wq_s1}, 5: $startq)
270 $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&= <&=]);
271 $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC