1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
8 use PublicInbox::DS qw(dwaitpid);
10 sub _vivify_external { # _externals_each callback
12 if (-f "$dir/ei.lock") {
13 require PublicInbox::ExtSearch;
14 push @$src, PublicInbox::ExtSearch->new($dir);
15 } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") { # v2, v1
16 require PublicInbox::Inbox;
17 push @$src, bless { inboxdir => $dir }, 'PublicInbox::Inbox';
19 warn "W: ignoring $dir, unable to determine type\n";
23 # the main "lei q SEARCH_TERMS" method
25 my ($self, @argv) = @_;
26 my $opt = $self->{opt};
28 # --local is enabled by default
29 # src: LeiXSearch || LeiSearch || Inbox
31 require PublicInbox::LeiXSearch;
32 require PublicInbox::LeiOverview;
33 PublicInbox::Config->json;
34 my $lxs = PublicInbox::LeiXSearch->new;
36 # --external is enabled by default, but allow --no-external
37 if ($opt->{external} // 1) {
38 $self->_externals_each(\&_vivify_external, \@srcs);
40 my $j = $opt->{jobs} // (scalar(@srcs) > 3 ? 3 : scalar(@srcs));
41 $j = 1 if !$opt->{thread};
42 $j++ if $opt->{'local'}; # for sto->search below
43 $self->atfork_prepare_wq($lxs);
44 $lxs->wq_workers_start('lei_xsearch', $j, $self->oldset)
45 // $lxs->wq_workers($j);
47 # no forking workers after this
48 my $ovv = PublicInbox::LeiOverview->new($self) or return;
49 my $sto = $self->_lei_store(1);
50 unshift(@srcs, $sto->search) if $opt->{'local'};
51 my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
52 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
53 $mset_opt{qstr} = join(' ', map {;
54 # Consider spaces in argv to be for phrase search in Xapian.
55 # In other words, the users should need only care about
56 # normal shell quotes and not have to learn Xapian quoting.
57 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
59 if (defined(my $sort = $opt->{'sort'})) {
60 if ($sort eq 'relevance') {
61 $mset_opt{relevance} = 1;
62 } elsif ($sort eq 'docid') {
63 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
64 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
67 die "unrecognized --sort=$sort\n";
70 # descending docid order
71 $mset_opt{relevance} //= -2 if $opt->{thread};
72 $self->{mset_opt} = \%mset_opt;
73 $ovv->ovv_begin($self);
74 $lxs->do_query($self, \@srcs);