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