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 $lei->{ovv}->ovv_atexit_child($lei);
132 sub query_mset { # non-parallel for non-"--thread" users
133 my ($self, $lei, $srcs) = @_;
134 my %sig = $lei->atfork_child_wq($self);
135 local @SIG{keys %sig} = values %sig;
136 my $mo = { %{$lei->{mset_opt}} };
138 $self->attach_external($_) for @$srcs;
139 my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei, $self);
140 my $dedupe = $lei->{dedupe} // die 'BUG: {dedupe} missing';
141 $dedupe->prepare_dedupe;
143 $mset = $self->mset($mo->{qstr}, $mo);
144 for my $it ($mset->items) {
145 my $smsg = smsg_for($self, $it) or next;
146 next if $dedupe->is_smsg_dup($smsg);
147 $each_smsg->($smsg, $it);
149 } while (_mset_more($mset, $mo));
150 $lei->{ovv}->ovv_atexit_child($lei);
156 my $tmp = File::Temp->newdir('lei_xsrch_git-XXXXXXXX', TMPDIR => 1);
157 for my $ibx (@{$self->{shard2ibx} // []}) {
158 my $d = File::Spec->canonpath($ibx->git->{git_dir});
159 $seen{$d} //= push @dirs, "$d/objects\n"
161 my $git_dir = $tmp->dirname;
162 PublicInbox::Import::init_bare($git_dir);
163 my $f = "$git_dir/objects/info/alternates";
164 open my $alt, '>', $f or die "open($f): $!";
165 print $alt @dirs or die "print $f: $!";
166 close $alt or die "close $f: $!";
167 my $git = PublicInbox::Git->new($git_dir);
172 sub query_done { # EOF callback
174 $lei->{ovv}->ovv_end($lei);
178 sub start_query { # always runs in main (lei-daemon) process
179 my ($self, $io, $lei, $srcs) = @_;
180 if (my $l2m = $lei->{l2m}) {
181 $lei->{1} = $io->[1];
182 $l2m->post_augment($lei);
183 $io->[1] = delete $lei->{1};
185 my $remotes = $self->{remotes} // [];
186 if ($lei->{opt}->{thread}) {
187 $lei->{-parallel} = scalar(@$remotes) + scalar(@$srcs) - 1;
188 for my $ibxish (@$srcs) {
189 $self->wq_do('query_thread_mset', $io, $lei, $ibxish);
192 $lei->{-parallel} = scalar(@$remotes);
193 $self->wq_do('query_mset', $io, $lei, $srcs);
196 for my $rmt (@$remotes) {
197 $self->wq_do('query_thread_mbox', $io, $lei, $rmt);
199 close $io->[0]; # qry_status_wr
203 sub query_prepare { # wq_do
204 my ($self, $lei) = @_;
205 my %sig = $lei->atfork_child_wq($self);
206 local @SIG{keys %sig} = values %sig;
207 if (my $l2m = $lei->{l2m}) {
208 eval { $l2m->do_augment($lei) };
209 return $lei->fail($@) if $@;
211 # trigger PublicInbox::OpPipe->event_step
212 my $qry_status_wr = $lei->{0} or
213 return $lei->fail('BUG: qry_status_wr missing');
214 $qry_status_wr->autoflush(1);
215 print $qry_status_wr '.' or # this should never fail...
216 return $lei->fail("BUG? print qry_status_wr: $!");
220 my ($self, $lei_orig, $srcs) = @_;
221 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
223 pipe(my $qry_status_rd, $io[0]) or die "pipe $!";
225 $lei_orig->{lxs} = $self;
226 $lei_orig->event_step_init; # wait for shutdowns
227 my $op_map = { '' => [ \&query_done, $lei_orig ] };
228 my $in_loop = exists $lei_orig->{sock};
229 my $opp = PublicInbox::OpPipe->new($qry_status_rd, $op_map, $in_loop);
230 if (my $l2m = $lei->{l2m}) {
231 $l2m->pre_augment($lei_orig); # may redirect $lei->{1} for mbox
232 $io[1] = $lei_orig->{1};
233 $op_map->{'.'} = [ \&start_query, $self, \@io, $lei, $srcs ];
234 $self->wq_do('query_prepare', \@io, $lei);
235 $opp->event_step if !$in_loop;
237 start_query($self, \@io, $lei, $srcs);
240 my @pids = $self->wq_close;
241 # for the $lei->atfork_child_wq PIPE handler:
242 $op_map->{'!'} = [ \&CORE::kill, 'TERM', @pids ];
244 my $ipc_worker_reap = $self->can('ipc_worker_reap');
245 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
249 sub ipc_atfork_prepare {
251 $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
252 $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC