]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei: q: results output to Maildir and mbox* working
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
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>
3
4 # Combine any combination of PublicInbox::Search,
5 # PublicInbox::ExtSearch, and PublicInbox::LeiSearch objects
6 # into one Xapian DB
7 package PublicInbox::LeiXSearch;
8 use strict;
9 use v5.10.1;
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
15 use File::Spec ();
16
17 sub new {
18         my ($class) = @_;
19         PublicInbox::Search::load_xapian();
20         bless {
21                 qp_flags => $PublicInbox::Search::QP_FLAGS |
22                                 PublicInbox::Search::FLAG_PURE_NOT(),
23         }, $class
24 }
25
26 sub attach_external {
27         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
28
29         if (!$ibxish->can('over') || !$ibxish->over) {
30                 return push(@{$self->{remotes}}, $ibxish)
31         }
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");
37
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);
44
45                 my $prev = {};
46                 for my $old_ibxish (@{$self->{shard2ibx}}) {
47                         next if $prev == $old_ibxish;
48                         $prev = $old_ibxish;
49                         my @shards = $old_ibxish->search->xdb_shards_flat;
50                         push @{$self->{shards_flat}}, @shards;
51                 }
52                 my $nr = scalar(@{$self->{shards_flat}});
53                 $nr == $expect or die
54                         "BUG: reloaded $nr shards, expected $expect"
55         }
56         push @{$self->{shards_flat}}, @shards;
57         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
58 }
59
60 # returns a list of local inboxes (or count in scalar context)
61 sub locals {
62         my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
63         values %uniq;
64 }
65
66 # called by PublicInbox::Search::xdb
67 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
68
69 # like over->get_art
70 sub smsg_for {
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;
79         $smsg;
80 }
81
82 sub recent {
83         my ($self, $qstr, $opt) = @_;
84         $opt //= {};
85         $opt->{relevance} //= -2;
86         $self->mset($qstr //= 'bytes:1..', $opt);
87 }
88
89 sub over {}
90
91 sub _mset_more ($$) {
92         my ($mset, $mo) = @_;
93         my $size = $mset->size;
94         $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
95 }
96
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;
101
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";
106                 return;
107         }
108         my $mo = { %{$lei->{mset_opt}} };
109         my $mset;
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;
113         do {
114                 $mset = $srch->mset($mo->{qstr}, $mo);
115                 my $ids = $srch->mset_to_artnums($mset, $mo);
116                 my $ctx = { ids => $ids };
117                 my $i = 0;
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);
125                         }
126                         @{$ctx->{xids}} = ();
127                 }
128         } while (_mset_more($mset, $mo));
129         $lei->{ovv}->ovv_atexit_child($lei);
130 }
131
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}} };
137         my $mset;
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;
142         do {
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);
148                 }
149         } while (_mset_more($mset, $mo));
150         $lei->{ovv}->ovv_atexit_child($lei);
151 }
152
153 sub git {
154         my ($self) = @_;
155         my (%seen, @dirs);
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"
160         }
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);
168         $git->{-tmp} = $tmp;
169         $git;
170 }
171
172 sub query_done { # EOF callback
173         my ($lei) = @_;
174         $lei->{ovv}->ovv_end($lei);
175         $lei->dclose;
176 }
177
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};
184         }
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);
190                 }
191         } else {
192                 $lei->{-parallel} = scalar(@$remotes);
193                 $self->wq_do('query_mset', $io, $lei, $srcs);
194         }
195         # TODO
196         for my $rmt (@$remotes) {
197                 $self->wq_do('query_thread_mbox', $io, $lei, $rmt);
198         }
199         close $io->[0]; # qry_status_wr
200         @$io = ();
201 }
202
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 $@;
210         }
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: $!");
217 }
218
219 sub do_query {
220         my ($self, $lei_orig, $srcs) = @_;
221         my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
222         $io[0] = undef;
223         pipe(my $qry_status_rd, $io[0]) or die "pipe $!";
224
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;
236         } else {
237                 start_query($self, \@io, $lei, $srcs);
238         }
239         unless ($in_loop) {
240                 my @pids = $self->wq_close;
241                 # for the $lei->atfork_child_wq PIPE handler:
242                 $op_map->{'!'} = [ \&CORE::kill, 'TERM', @pids ];
243                 $opp->event_step;
244                 my $ipc_worker_reap = $self->can('ipc_worker_reap');
245                 dwaitpid($_, $ipc_worker_reap, $self) for @pids;
246         }
247 }
248
249 sub ipc_atfork_prepare {
250         my ($self) = @_;
251         $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
252         $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
253 }
254
255 1;