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);
15 PublicInbox::Search::load_xapian();
17 qp_flags => $PublicInbox::Search::QP_FLAGS |
18 PublicInbox::Search::FLAG_PURE_NOT(),
23 my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
25 if (!$ibxish->can('over') || !$ibxish->over) {
26 return push(@{$self->{remotes}}, $ibxish)
28 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
29 my $srch = $ibxish->search or
30 return warn("$desc not indexed for Xapian\n");
31 my @shards = $srch->xdb_shards_flat or
32 return warn("$desc has no Xapian shardsXapian\n");
34 if (delete $self->{xdb}) { # XXX: do we need this?
35 # clobber existing {xdb} if amending
36 my $expect = delete $self->{nshard};
37 my $shards = delete $self->{shards_flat};
38 scalar(@$shards) == $expect or die
39 "BUG: {nshard}$expect != shards=".scalar(@$shards);
42 for my $old_ibxish (@{$self->{shard2ibx}}) {
43 next if $prev == $old_ibxish;
45 my @shards = $old_ibxish->search->xdb_shards_flat;
46 push @{$self->{shards_flat}}, @shards;
48 my $nr = scalar(@{$self->{shards_flat}});
50 "BUG: reloaded $nr shards, expected $expect"
52 push @{$self->{shards_flat}}, @shards;
53 push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
56 # returns a list of local inboxes (or count in scalar context)
58 my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
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 $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
74 $smsg->{docid} = $docid;
79 my ($self, $qstr, $opt) = @_;
81 $opt->{relevance} //= -2;
82 $self->mset($qstr //= 'bytes:1..', $opt);
89 my $size = $mset->size;
90 $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
93 sub query_thread_mset { # for --thread
94 my ($self, $lei, $ibxish) = @_;
95 my %sig = $lei->atfork_child_wq($self);
96 local @SIG{keys %sig} = values %sig;
98 my ($srch, $over) = ($ibxish->search, $ibxish->over);
99 unless ($srch && $over) {
100 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
101 warn "$desc not indexed by Xapian\n";
104 my $mo = { %{$lei->{mset_opt}} };
106 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
110 $mset = $srch->mset($mo->{qstr}, $mo);
111 my $ids = $srch->mset_to_artnums($mset, $mo);
112 my $ctx = { ids => $ids };
114 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
115 while ($over->expand_thread($ctx)) {
116 for my $n (@{$ctx->{xids}}) {
117 my $smsg = $over->get_art($n) or next;
118 next if $dd->is_smsg_dup($smsg);
119 my $mitem = delete $n2item{$smsg->{num}};
120 $each_smsg->($smsg, $mitem);
122 @{$ctx->{xids}} = ();
124 } while (_mset_more($mset, $mo));
125 $lei->{ovv}->ovv_atexit_child($lei);
128 sub query_mset { # non-parallel for non-"--thread" users
129 my ($self, $lei, $srcs) = @_;
130 my %sig = $lei->atfork_child_wq($self);
131 local @SIG{keys %sig} = values %sig;
132 my $mo = { %{$lei->{mset_opt}} };
134 $self->attach_external($_) for @$srcs;
135 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
139 $mset = $self->mset($mo->{qstr}, $mo);
140 for my $it ($mset->items) {
141 my $smsg = smsg_for($self, $it) or next;
142 next if $dd->is_smsg_dup($smsg);
143 $each_smsg->($smsg, $it);
145 } while (_mset_more($mset, $mo));
146 $lei->{ovv}->ovv_atexit_child($lei);
149 sub query_done { # PublicInbox::EOFpipe callback
151 $lei->{ovv}->ovv_end($lei);
156 my ($self, $lei_orig, $srcs) = @_;
157 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
158 my $remotes = $self->{remotes} // [];
159 pipe(my ($eof_wait, $qry_done)) or die "pipe $!";
160 $io[0] = $qry_done; # don't need stdin
162 if ($lei->{opt}->{thread}) {
163 $lei->{-parallel} = scalar(@$remotes) + scalar(@$srcs) - 1;
164 for my $ibxish (@$srcs) {
165 $self->wq_do('query_thread_mset', \@io, $lei, $ibxish);
168 $lei->{-parallel} = scalar(@$remotes);
169 $self->wq_do('query_mset', \@io, $lei, $srcs);
172 for my $rmt (@$remotes) {
173 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
176 close $qry_done; # fully closed when children are done
178 # query_done will run when query_*mset close $qry_done
179 if ($lei_orig->{sock}) { # watch for client premature exit
180 require PublicInbox::EOFpipe;
181 PublicInbox::EOFpipe->new($eof_wait, \&query_done, $lei_orig);
182 $lei_orig->{lxs} = $self;
183 $lei_orig->event_step_init;
185 my @pids = $self->wq_close;
186 # wait for close($lei->{0})
187 if (read($eof_wait, my $buf, 1)) {
188 # if we get a SIGPIPE from one, kill the rest
189 kill('TERM', @pids) if $buf eq '!';
191 my $ipc_worker_reap = $self->can('ipc_worker_reap');
192 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
193 query_done($lei_orig); # may SIGPIPE
197 sub ipc_atfork_prepare {
199 $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
200 $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC