]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei: q: lock stdout on overview output
[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
12 sub new {
13         my ($class) = @_;
14         PublicInbox::Search::load_xapian();
15         bless {
16                 qp_flags => $PublicInbox::Search::QP_FLAGS |
17                                 PublicInbox::Search::FLAG_PURE_NOT(),
18         }, $class
19 }
20
21 sub attach_external {
22         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
23
24         if (!$ibxish->can('over') || !$ibxish->over) {
25                 return push(@{$self->{remotes}}, $ibxish)
26         }
27         my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
28         my $srch = $ibxish->search or
29                 return warn("$desc not indexed for Xapian\n");
30         my @shards = $srch->xdb_shards_flat or
31                 return warn("$desc has no Xapian shardsXapian\n");
32
33         if (delete $self->{xdb}) { # XXX: do we need this?
34                 # clobber existing {xdb} if amending
35                 my $expect = delete $self->{nshard};
36                 my $shards = delete $self->{shards_flat};
37                 scalar(@$shards) == $expect or die
38                         "BUG: {nshard}$expect != shards=".scalar(@$shards);
39
40                 my $prev = {};
41                 for my $old_ibxish (@{$self->{shard2ibx}}) {
42                         next if $prev == $old_ibxish;
43                         $prev = $old_ibxish;
44                         my @shards = $old_ibxish->search->xdb_shards_flat;
45                         push @{$self->{shards_flat}}, @shards;
46                 }
47                 my $nr = scalar(@{$self->{shards_flat}});
48                 $nr == $expect or die
49                         "BUG: reloaded $nr shards, expected $expect"
50         }
51         push @{$self->{shards_flat}}, @shards;
52         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
53 }
54
55 # returns a list of local inboxes (or count in scalar context)
56 sub locals {
57         my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
58         values %uniq;
59 }
60
61 # called by PublicInbox::Search::xdb
62 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
63
64 # like over->get_art
65 sub smsg_for {
66         my ($self, $mitem) = @_;
67         # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
68         my $nshard = $self->{nshard};
69         my $docid = $mitem->get_docid;
70         my $shard = ($docid - 1) % $nshard;
71         my $num = int(($docid - 1) / $nshard) + 1;
72         my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
73         $smsg->{docid} = $docid;
74         $smsg;
75 }
76
77 sub recent {
78         my ($self, $qstr, $opt) = @_;
79         $opt //= {};
80         $opt->{relevance} //= -2;
81         $self->mset($qstr //= 'bytes:1..', $opt);
82 }
83
84 sub over {}
85
86 sub _mset_more ($$) {
87         my ($mset, $mo) = @_;
88         my $size = $mset->size;
89         $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
90 }
91
92 sub query_thread_mset { # for --thread
93         my ($self, $lei, $ibxish) = @_;
94         my %sig = $lei->atfork_child_wq($self);
95         local @SIG{keys %sig} = values %sig;
96
97         my ($srch, $over) = ($ibxish->search, $ibxish->over);
98         unless ($srch && $over) {
99                 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
100                 warn "$desc not indexed by Xapian\n";
101                 return;
102         }
103         my $mo = { %{$lei->{mset_opt}} };
104         my $mset;
105         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
106         my $dd = $lei->{dd};
107         $dd->prepare_dedupe;
108         do {
109                 $mset = $srch->mset($mo->{qstr}, $mo);
110                 my $ids = $srch->mset_to_artnums($mset, $mo);
111                 my $ctx = { ids => $ids };
112                 my $i = 0;
113                 my %n2item = map { ($ids->[$i++], $_) } $mset->items;
114                 while ($over->expand_thread($ctx)) {
115                         for my $n (@{$ctx->{xids}}) {
116                                 my $smsg = $over->get_art($n) or next;
117                                 next if $dd->is_smsg_dup($smsg);
118                                 my $mitem = delete $n2item{$smsg->{num}};
119                                 $each_smsg->($smsg, $mitem);
120                                 # $self->out($buf .= $ORS);
121                                 # $emit_cb->($smsg);
122                         }
123                         @{$ctx->{xids}} = ();
124                 }
125         } while (_mset_more($mset, $mo));
126         $lei->{ovv}->ovv_atexit_child($lei);
127 }
128
129 sub query_mset { # non-parallel for non-"--thread" users
130         my ($self, $lei, $srcs) = @_;
131         my %sig = $lei->atfork_child_wq($self);
132         local @SIG{keys %sig} = values %sig;
133         my $mo = { %{$lei->{mset_opt}} };
134         my $mset;
135         $self->attach_external($_) for @$srcs;
136         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
137         my $dd = $lei->{dd};
138         $dd->prepare_dedupe;
139         do {
140                 $mset = $self->mset($mo->{qstr}, $mo);
141                 for my $it ($mset->items) {
142                         my $smsg = smsg_for($self, $it) or next;
143                         next if $dd->is_smsg_dup($smsg);
144                         $each_smsg->($smsg, $it);
145                         # $self->out($buf .= $ORS) if defined $buf;
146                         #$emit_cb->($smsg);
147                 }
148         } while (_mset_more($mset, $mo));
149         $lei->{ovv}->ovv_atexit_child($lei);
150 }
151
152 sub query_done { # PublicInbox::EOFpipe callback
153         my ($lei) = @_;
154         $lei->{ovv}->ovv_end($lei);
155         $lei->dclose;
156 }
157
158 sub do_query {
159         my ($self, $lei_orig, $srcs) = @_;
160         my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
161         my $remotes = $self->{remotes} // [];
162         pipe(my ($eof_wait, $qry_done)) or die "pipe $!";
163         $io[0] = $qry_done; # don't need stdin
164
165         if ($lei->{opt}->{thread}) {
166                 $lei->{-parallel} = scalar(@$remotes) + scalar(@$srcs) - 1;
167                 for my $ibxish (@$srcs) {
168                         $self->wq_do('query_thread_mset', \@io, $lei, $ibxish);
169                 }
170         } else {
171                 $lei->{-parallel} = scalar(@$remotes);
172                 $self->wq_do('query_mset', \@io, $lei, $srcs);
173         }
174         # TODO
175         for my $rmt (@$remotes) {
176                 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
177         }
178         @io = ();
179         close $qry_done; # fully closed when children are done
180
181         # query_done will run when query_*mset close $qry_done
182         if ($lei_orig->{sock}) { # watch for client premature exit
183                 require PublicInbox::EOFpipe;
184                 PublicInbox::EOFpipe->new($eof_wait, \&query_done, $lei_orig);
185                 $lei_orig->{lxs} = $self;
186                 $lei_orig->event_step_init;
187         } else {
188                 $self->wq_close;
189                 read($eof_wait, my $buf, 1); # wait for close($lei->{0})
190                 query_done($lei_orig); # may SIGPIPE
191         }
192 }
193
194 sub ipc_atfork_prepare {
195         my ($self) = @_;
196         $self->wq_set_recv_modes(qw[+<&= >&= >&= +<&=]);
197         $self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
198 }
199
200 1;