]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
7ca01454f04c7d65bc421582d1143758032ef350
[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 sub _vivify_external { # _externals_each callback
11         my ($src, $dir) = @_;
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';
18         } else {
19                 warn "W: ignoring $dir, unable to determine type\n";
20         }
21 }
22
23 # the main "lei q SEARCH_TERMS" method
24 sub lei_q {
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);
31
32         # --local is enabled by default
33         # src: LeiXSearch || LeiSearch || Inbox
34         my @srcs;
35         require PublicInbox::LeiXSearch;
36         require PublicInbox::LeiOverview;
37         my $lxs = PublicInbox::LeiXSearch->new;
38
39         # --external is enabled by default, but allow --no-external
40         if ($opt->{external} // 1) {
41                 $self->_externals_each(\&_vivify_external, \@srcs);
42         }
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         if ($self->{sock}) {
47                 $self->atfork_prepare_wq($lxs);
48                 $lxs->wq_workers_start('lei_xsearch', $j, $self->oldset)
49                         // $lxs->wq_workers($j);
50         }
51         unshift(@srcs, $sto->search) if $opt->{'local'};
52         # no forking workers after this
53         require PublicInbox::LeiOverview;
54         $self->{ovv} = PublicInbox::LeiOverview->new($self);
55         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
56         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
57         $mset_opt{qstr} = join(' ', map {;
58                 # Consider spaces in argv to be for phrase search in Xapian.
59                 # In other words, the users should need only care about
60                 # normal shell quotes and not have to learn Xapian quoting.
61                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
62         } @argv);
63         if (defined(my $sort = $opt->{'sort'})) {
64                 if ($sort eq 'relevance') {
65                         $mset_opt{relevance} = 1;
66                 } elsif ($sort eq 'docid') {
67                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
68                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
69                         # the default
70                 } else {
71                         die "unrecognized --sort=$sort\n";
72                 }
73         }
74         # $self->out($json->encode(\%mset_opt));
75         # descending docid order
76         $mset_opt{relevance} //= -2 if $opt->{thread};
77         # my $wcb = PublicInbox::LeiToMail->write_cb($out, $self);
78         $self->{mset_opt} = \%mset_opt;
79         $self->{ovv}->ovv_begin($self);
80         pipe(my ($eof_wait, $qry_done)) or die "pipe $!";
81         require PublicInbox::EOFpipe;
82         my $eof = PublicInbox::EOFpipe->new($eof_wait, \&query_done, $self);
83         $lxs->do_query($self, $qry_done, \@srcs);
84         $eof->event_step unless $self->{sock};
85 }
86
87 sub query_done { # PublicInbox::EOFpipe callback
88         my ($self) = @_;
89         $self->{ovv}->ovv_end($self);
90 }
91
92 1;