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::Search qw(get_pct);
12 use Sys::Syslog qw(syslog);
16 PublicInbox::Search::load_xapian();
18 qp_flags => $PublicInbox::Search::QP_FLAGS |
19 PublicInbox::Search::FLAG_PURE_NOT(),
24 my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
26 if (!$ibxish->can('over') || !$ibxish->over) {
27 return push(@{$self->{remotes}}, $ibxish)
29 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
30 my $srch = $ibxish->search or
31 return warn("$desc not indexed for Xapian\n");
32 my @shards = $srch->xdb_shards_flat or
33 return warn("$desc has no Xapian shardsXapian\n");
35 if (delete $self->{xdb}) { # XXX: do we need this?
36 # clobber existing {xdb} if amending
37 my $expect = delete $self->{nshard};
38 my $shards = delete $self->{shards_flat};
39 scalar(@$shards) == $expect or die
40 "BUG: {nshard}$expect != shards=".scalar(@$shards);
43 for my $old_ibxish (@{$self->{shard2ibx}}) {
44 next if $prev == $old_ibxish;
46 my @shards = $old_ibxish->search->xdb_shards_flat;
47 push @{$self->{shards_flat}}, @shards;
49 my $nr = scalar(@{$self->{shards_flat}});
51 "BUG: reloaded $nr shards, expected $expect"
53 push @{$self->{shards_flat}}, @shards;
54 push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
57 # returns a list of local inboxes (or count in scalar context)
59 my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
63 # called by PublicInbox::Search::xdb
64 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
68 my ($self, $mitem) = @_;
69 # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
70 my $nshard = $self->{nshard};
71 my $docid = $mitem->get_docid;
72 my $shard = ($docid - 1) % $nshard;
73 my $num = int(($docid - 1) / $nshard) + 1;
74 my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
75 $smsg->{docid} = $docid;
80 my ($self, $qstr, $opt) = @_;
82 $opt->{relevance} //= -2;
83 $self->mset($qstr //= 'bytes:1..', $opt);
90 my $size = $mset->size;
91 $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
94 sub query_thread_mset { # for --thread
95 my ($self, $lei, $ibxish) = @_;
96 local %SIG = (%SIG, $lei->atfork_child_wq($self));
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";
103 my $mo = { %{$lei->{mset_opt}} };
106 $mset = $srch->mset($mo->{qstr}, $mo);
107 my $ids = $srch->mset_to_artnums($mset, $mo);
108 my $ctx = { ids => $ids };
110 my %n2p = map { ($ids->[$i++], get_pct($_)) } $mset->items;
111 while ($over->expand_thread($ctx)) {
112 for my $n (@{$ctx->{xids}}) {
113 my $smsg = $over->get_art($n) or next;
114 # next if $dd->is_smsg_dup($smsg); TODO
115 if (my $p = delete $n2p{$smsg->{num}}) {
116 $smsg->{relevance} = $p;
118 print { $self->{1} } Dumper($smsg);
119 # $self->out($buf .= $ORS);
122 @{$ctx->{xids}} = ();
124 } while (_mset_more($mset, $mo));
127 sub query_mset { # non-parallel for non-"--thread" users
128 my ($self, $lei, $srcs) = @_;
129 my $mo = { %{$lei->{mset_opt}} };
131 local %SIG = (%SIG, $lei->atfork_child_wq($self));
132 $self->attach_external($_) for @$srcs;
134 $mset = $self->mset($mo->{qstr}, $mo);
135 for my $it ($mset->items) {
136 my $smsg = smsg_for($self, $it) or next;
137 # next if $dd->is_smsg_dup($smsg);
138 $smsg->{relevance} = get_pct($it);
140 print { $self->{1} } Dumper($smsg);
141 # $self->out($buf .= $ORS) if defined $buf;
144 } while (_mset_more($mset, $mo));
148 my ($self, $lei_orig, $srcs) = @_;
149 my ($lei, @io) = $lei_orig->atfork_parent_wq($self);
150 $io[1]->autoflush(1);
151 $io[2]->autoflush(1);
152 if ($lei->{opt}->{thread}) {
153 for my $ibxish (@$srcs) {
154 $self->wq_do('query_thread_mset', \@io, $lei, $ibxish);
157 $self->wq_do('query_mset', \@io, $lei, $srcs);
160 for my $rmt (@{$self->{remotes} // []}) {
161 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
165 sub ipc_atfork_child {
167 $SIG{__WARN__} = sub { syslog('warning', "@_") };
168 $self->SUPER::ipc_atfork_child; # PublicInbox::IPC