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);
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 ($srch, $over) = ($ibxish->search, $ibxish->over);
96 unless ($srch && $over) {
97 my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
98 warn "$desc not indexed by Xapian\n";
101 local %SIG = (%SIG, $lei->atfork_child_wq($self));
102 my $mo = { %{$lei->{mset_opt}} };
105 $mset = $srch->mset($mo->{qstr}, $mo);
106 my $ids = $srch->mset_to_artnums($mset, $mo);
107 my $ctx = { ids => $ids };
109 my %n2p = map { ($ids->[$i++], get_pct($_)) } $mset->items;
110 while ($over->expand_thread($ctx)) {
111 for my $n (@{$ctx->{xids}}) {
112 my $smsg = $over->get_art($n) or next;
113 # next if $dd->is_smsg_dup($smsg); TODO
114 if (my $p = delete $n2p{$smsg->{num}}) {
115 $smsg->{relevance} = $p;
117 print { $self->{1} } Dumper($smsg);
118 # $self->out($buf .= $ORS);
121 @{$ctx->{xids}} = ();
123 } while (_mset_more($mset, $mo));
126 sub query_mset { # non-parallel for non-"--thread" users
127 my ($self, $lei, $srcs) = @_;
128 my $mo = { %{$lei->{mset_opt}} };
130 local %SIG = (%SIG, $lei->atfork_child_wq($self));
131 $self->attach_external($_) for @$srcs;
133 $mset = $self->mset($mo->{qstr}, $mo);
134 for my $it ($mset->items) {
135 my $smsg = smsg_for($self, $it) or next;
136 # next if $dd->is_smsg_dup($smsg);
137 $smsg->{relevance} = get_pct($it);
139 print { $self->{1} } Dumper($smsg);
140 # $self->out($buf .= $ORS) if defined $buf;
143 } while (_mset_more($mset, $mo));
147 my ($self, $lei_orig, $srcs) = @_;
148 my ($lei, @io) = $lei_orig->atfork_prepare_wq($self);
149 $io[1]->autoflush(1);
150 $io[2]->autoflush(1);
151 if ($lei->{opt}->{thread}) {
152 for my $ibxish (@$srcs) {
153 $self->wq_do('query_thread_mset', @io, $lei, $ibxish);
156 $self->wq_do('query_mset', @io, $lei, $srcs);
159 for my $rmt (@{$self->{remotes} // []}) {
160 $self->wq_do('query_thread_mbox', @io, $lei, $rmt);