]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei: fork + FD cleanup
[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 use PublicInbox::Search qw(get_pct);
12 use Sys::Syslog qw(syslog);
13
14 sub new {
15         my ($class) = @_;
16         PublicInbox::Search::load_xapian();
17         bless {
18                 qp_flags => $PublicInbox::Search::QP_FLAGS |
19                                 PublicInbox::Search::FLAG_PURE_NOT(),
20         }, $class
21 }
22
23 sub attach_external {
24         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
25
26         if (!$ibxish->can('over') || !$ibxish->over) {
27                 return push(@{$self->{remotes}}, $ibxish)
28         }
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");
34
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);
41
42                 my $prev = {};
43                 for my $old_ibxish (@{$self->{shard2ibx}}) {
44                         next if $prev == $old_ibxish;
45                         $prev = $old_ibxish;
46                         my @shards = $old_ibxish->search->xdb_shards_flat;
47                         push @{$self->{shards_flat}}, @shards;
48                 }
49                 my $nr = scalar(@{$self->{shards_flat}});
50                 $nr == $expect or die
51                         "BUG: reloaded $nr shards, expected $expect"
52         }
53         push @{$self->{shards_flat}}, @shards;
54         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
55 }
56
57 # returns a list of local inboxes (or count in scalar context)
58 sub locals {
59         my %uniq = map {; "$_" => $_ } @{$_[0]->{shard2ibx} // []};
60         values %uniq;
61 }
62
63 # called by PublicInbox::Search::xdb
64 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
65
66 # like over->get_art
67 sub smsg_for {
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;
76         $smsg;
77 }
78
79 sub recent {
80         my ($self, $qstr, $opt) = @_;
81         $opt //= {};
82         $opt->{relevance} //= -2;
83         $self->mset($qstr //= 'bytes:1..', $opt);
84 }
85
86 sub over {}
87
88 sub _mset_more ($$) {
89         my ($mset, $mo) = @_;
90         my $size = $mset->size;
91         $size && (($mo->{offset} += $size) < ($mo->{limit} // 10000));
92 }
93
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";
101                 return;
102         }
103         my $mo = { %{$lei->{mset_opt}} };
104         my $mset;
105         do {
106                 $mset = $srch->mset($mo->{qstr}, $mo);
107                 my $ids = $srch->mset_to_artnums($mset, $mo);
108                 my $ctx = { ids => $ids };
109                 my $i = 0;
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;
117                                 }
118                                 print { $self->{1} } Dumper($smsg);
119                                 # $self->out($buf .= $ORS);
120                                 # $emit_cb->($smsg);
121                         }
122                         @{$ctx->{xids}} = ();
123                 }
124         } while (_mset_more($mset, $mo));
125 }
126
127 sub query_mset { # non-parallel for non-"--thread" users
128         my ($self, $lei, $srcs) = @_;
129         my $mo = { %{$lei->{mset_opt}} };
130         my $mset;
131         local %SIG = (%SIG, $lei->atfork_child_wq($self));
132         $self->attach_external($_) for @$srcs;
133         do {
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);
139                         use Data::Dumper;
140                         print { $self->{1} } Dumper($smsg);
141                         # $self->out($buf .= $ORS) if defined $buf;
142                         #$emit_cb->($smsg);
143                 }
144         } while (_mset_more($mset, $mo));
145 }
146
147 sub do_query {
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);
155                 }
156         } else {
157                 $self->wq_do('query_mset', \@io, $lei, $srcs);
158         }
159         # TODO
160         for my $rmt (@{$self->{remotes} // []}) {
161                 $self->wq_do('query_thread_mbox', \@io, $lei, $rmt);
162         }
163 }
164
165 sub ipc_atfork_child {
166         my ($self) = @_;
167         $SIG{__WARN__} = sub { syslog('warning', "@_") };
168         $self->SUPER::ipc_atfork_child; # PublicInbox::IPC
169 }
170
171 1;