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);
14 PublicInbox::Search::load_xapian();
16 qp_flags => $PublicInbox::Search::QP_FLAGS |
17 PublicInbox::Search::FLAG_PURE_NOT(),
22 my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
24 if (!$ibxish->can('over') || !$ibxish->over) {
25 return push(@{$self->{remotes}}, $ibxish)
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");
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);
41 for my $old_ibxish (@{$self->{shard2ibx}}) {
42 next if $prev == $old_ibxish;
44 my @shards = $old_ibxish->search->xdb_shards_flat;
45 push @{$self->{shards_flat}}, @shards;
47 my $nr = scalar(@{$self->{shards_flat}});
49 "BUG: reloaded $nr shards, expected $expect"
51 push @{$self->{shards_flat}}, @shards;
52 push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
55 # returns a list of local inboxes (or count in scalar context)
57 my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
61 # called by PublicInbox::Search::xdb
62 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
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;
78 my ($self, $qstr, $opt) = @_;
80 $opt->{relevance} //= -2;
81 $self->mset($qstr //= 'bytes:1..', $opt);