]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
avoid calling waitpid from children in DESTROY
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
1 # Copyright (C) 2020 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);
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         if (!$ibxish->can('over')) {
24                 push @{$self->{remotes}}, $ibxish
25         }
26         if (delete $self->{xdb}) { # XXX: do we need this?
27                 # clobber existing {xdb} if amending
28                 my $expect = delete $self->{nshard};
29                 my $shards = delete $self->{shards_flat};
30                 scalar(@$shards) == $expect or die
31                         "BUG: {nshard}$expect != shards=".scalar(@$shards);
32
33                 my $prev = {};
34                 for my $old_ibxish (@{$self->{shard2ibx}}) {
35                         next if $prev == $old_ibxish;
36                         $prev = $old_ibxish;
37                         my @shards = $old_ibxish->search->xdb_shards_flat;
38                         push @{$self->{shards_flat}}, @shards;
39                 }
40                 my $nr = scalar(@{$self->{shards_flat}});
41                 $nr == $expect or die
42                         "BUG: reloaded $nr shards, expected $expect"
43         }
44         my @shards = $ibxish->search->xdb_shards_flat;
45         push @{$self->{shards_flat}}, @shards;
46         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
47 }
48
49 # called by PublicInbox::Search::xdb
50 sub xdb_shards_flat { @{$_[0]->{shards_flat}} }
51
52 # like over->get_art
53 sub smsg_for {
54         my ($self, $mitem) = @_;
55         # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
56         my $nshard = $self->{nshard};
57         my $docid = $mitem->get_docid;
58         my $shard = ($docid - 1) % $nshard;
59         my $num = int(($docid - 1) / $nshard) + 1;
60         my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
61         $smsg->{docid} = $docid;
62         $smsg;
63 }
64
65 sub recent {
66         my ($self, $qstr, $opt) = @_;
67         $opt //= {};
68         $opt->{relevance} //= -2;
69         $self->mset($qstr //= 'bytes:1..', $opt);
70 }
71
72 1;