]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei: move external vivification to xsearch
[public-inbox.git] / lib / PublicInbox / LeiQuery.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::DS qw(dwaitpid);
9
10 # the main "lei q SEARCH_TERMS" method
11 sub lei_q {
12         my ($self, @argv) = @_;
13         require PublicInbox::LeiXSearch;
14         require PublicInbox::LeiOverview;
15         PublicInbox::Config->json; # preload before forking
16         my $opt = $self->{opt};
17         my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
18         # any number of LeiXSearch || LeiSearch || Inbox
19         if ($opt->{'local'} //= 1) { # --local is enabled by default
20                 my $sto = $self->_lei_store(1);
21                 $lxs->prepare_external($sto->search);
22         }
23
24         # --external is enabled by default, but allow --no-external
25         if ($opt->{external} //= 1) {
26                 my $cb = $lxs->can('prepare_external');
27                 $self->_externals_each($cb, $lxs);
28         }
29         my $xj = $opt->{thread} ? $lxs->locals : ($lxs->remotes + 1);
30         my $ovv = PublicInbox::LeiOverview->new($self) or return;
31         $self->atfork_prepare_wq($lxs);
32         $lxs->wq_workers_start('lei_xsearch', $xj, $self->oldset);
33         delete $lxs->{-ipc_atfork_child_close};
34         if (my $l2m = $self->{l2m}) {
35                 my $mj = 4; # TODO: configurable
36                 $self->atfork_prepare_wq($l2m);
37                 $l2m->wq_workers_start('lei2mail', $mj, $self->oldset);
38                 delete $l2m->{-ipc_atfork_child_close};
39         }
40
41         # no forking workers after this
42
43         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
44         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
45         $mset_opt{qstr} = join(' ', map {;
46                 # Consider spaces in argv to be for phrase search in Xapian.
47                 # In other words, the users should need only care about
48                 # normal shell quotes and not have to learn Xapian quoting.
49                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
50         } @argv);
51         if (defined(my $sort = $opt->{'sort'})) {
52                 if ($sort eq 'relevance') {
53                         $mset_opt{relevance} = 1;
54                 } elsif ($sort eq 'docid') {
55                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
56                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
57                         # the default
58                 } else {
59                         die "unrecognized --sort=$sort\n";
60                 }
61         }
62         # descending docid order
63         $mset_opt{relevance} //= -2 if $opt->{thread};
64         $self->{mset_opt} = \%mset_opt;
65         $ovv->ovv_begin($self);
66         $lxs->do_query($self);
67 }
68
69 1;