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 $sto = $self->_lei_store(1);
27 my $cfg = $self->_lei_cfg(1);
28 my $opt = $self->{opt};
29 require PublicInbox::LeiDedupe;
30 my $dd = PublicInbox::LeiDedupe->new($self);
32 # --local is enabled by default
33 # src: LeiXSearch || LeiSearch || Inbox
35 require PublicInbox::LeiXSearch;
36 require PublicInbox::LeiOverview;
37 my $lxs = PublicInbox::LeiXSearch->new;
39 # --external is enabled by default, but allow --no-external
40 if ($opt->{external} // 1) {
41 $self->_externals_each(\&_vivify_external, \@srcs);
43 my $j = $opt->{jobs} // (scalar(@srcs) > 3 ? 3 : scalar(@srcs));
44 $j = 1 if !$opt->{thread};
45 $j++ if $opt->{'local'}; # for sto->search below
46 $self->atfork_prepare_wq($lxs);
47 $lxs->wq_workers_start('lei_xsearch', $j, $self->oldset)
48 // $lxs->wq_workers($j);
50 unshift(@srcs, $sto->search) if $opt->{'local'};
51 # no forking workers after this
52 require PublicInbox::LeiOverview;
53 $self->{ovv} = PublicInbox::LeiOverview->new($self);
54 my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
55 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
56 $mset_opt{qstr} = join(' ', map {;
57 # Consider spaces in argv to be for phrase search in Xapian.
58 # In other words, the users should need only care about
59 # normal shell quotes and not have to learn Xapian quoting.
60 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
62 if (defined(my $sort = $opt->{'sort'})) {
63 if ($sort eq 'relevance') {
64 $mset_opt{relevance} = 1;
65 } elsif ($sort eq 'docid') {
66 $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
67 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
70 die "unrecognized --sort=$sort\n";
73 # $self->out($json->encode(\%mset_opt));
74 # descending docid order
75 $mset_opt{relevance} //= -2 if $opt->{thread};
76 # my $wcb = PublicInbox::LeiToMail->write_cb($out, $self);
77 $self->{mset_opt} = \%mset_opt;
78 $self->{ovv}->ovv_begin($self);
79 $lxs->do_query($self, \@srcs);