]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei: query: ensure pager exit is instantaneous
[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::MID qw($MID_EXTRACT);
9 use POSIX qw(strftime);
10 use PublicInbox::Address qw(pairs);
11 use PublicInbox::DS qw(dwaitpid);
12
13 sub _iso8601 ($) { strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($_[0])) }
14
15 # prepares an smsg for JSON
16 sub _smsg_unbless ($) {
17         my ($smsg) = @_;
18
19         delete @$smsg{qw(lines bytes)};
20         $smsg->{rcvd} = _iso8601(delete $smsg->{ts}); # JMAP receivedAt
21         $smsg->{dt} = _iso8601(delete $smsg->{ds}); # JMAP UTCDate
22
23         if (my $r = delete $smsg->{references}) {
24                 $smsg->{references} = [
25                                 map { "<$_>" } ($r =~ m/$MID_EXTRACT/go) ];
26         }
27         if (my $m = delete($smsg->{mid})) {
28                 $smsg->{'m'} = "<$m>";
29         }
30         # XXX breaking to/cc, into structured arrays or tables which
31         # distinguish "$phrase <$address>" causes pretty printing JSON
32         # to take up too much vertical space.  I can't get either
33         # Cpanel::JSON::XS or JSON::XS or jq(1) only indent when
34         # wrapping is necessary, rather than blindly indenting and
35         # adding vertical space everywhere.
36         for my $f (qw(from to cc)) {
37                 my $v = delete $smsg->{$f} or next;
38                 $smsg->{substr($f, 0, 1)} = $v;
39         }
40         $smsg->{'s'} = delete $smsg->{subject};
41         # can we be bothered to parse From/To/Cc into arrays?
42         scalar { %$smsg }; # unbless
43 }
44
45 sub _vivify_external { # _externals_each callback
46         my ($src, $dir) = @_;
47         if (-f "$dir/ei.lock") {
48                 require PublicInbox::ExtSearch;
49                 push @$src, PublicInbox::ExtSearch->new($dir);
50         } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") { # v2, v1
51                 require PublicInbox::Inbox;
52                 push @$src, bless { inboxdir => $dir }, 'PublicInbox::Inbox';
53         } else {
54                 warn "W: ignoring $dir, unable to determine type\n";
55         }
56 }
57
58 # the main "lei q SEARCH_TERMS" method
59 sub lei_q {
60         my ($self, @argv) = @_;
61         my $sto = $self->_lei_store(1);
62         my $cfg = $self->_lei_cfg(1);
63         my $opt = $self->{opt};
64         require PublicInbox::LeiDedupe;
65         my $dd = PublicInbox::LeiDedupe->new($self);
66
67         # --local is enabled by default
68         # src: LeiXSearch || LeiSearch || Inbox
69         my @srcs = $opt->{'local'} ? ($sto->search) : ();
70         require PublicInbox::LeiXSearch;
71         my $lxs = PublicInbox::LeiXSearch->new;
72
73         # --external is enabled by default, but allow --no-external
74         if ($opt->{external} // 1) {
75                 $self->_externals_each(\&_vivify_external, \@srcs);
76         }
77         my $j = $opt->{jobs} // scalar(@srcs) > 4 ? 4 : scalar(@srcs);
78         $j = 1 if !$opt->{thread};
79         if ($self->{pid}) {
80                 $lxs->wq_workers_start('lei_xsearch', $j, $self->oldset)
81                         // $self->wq_workers($j);
82         }
83         my $out = $opt->{output} // '-';
84         $out = 'json:/dev/stdout' if $out eq '-';
85         my $isatty = -t $self->{1};
86         # no forking workers after this
87         my $pid_old12 = $self->start_pager if $isatty;
88         my $json = substr($out, 0, 5) eq 'json:' ?
89                 ref(PublicInbox::Config->json)->new : undef;
90         if ($json) {
91                 if ($opt->{pretty} //= $isatty) {
92                         $json->pretty(1)->space_before(0);
93                         $json->indent_length($opt->{indent} // 2);
94                 }
95                 $json->utf8; # avoid Wide character in print warnings
96                 $json->ascii(1) if $opt->{ascii}; # for "\uXXXX"
97                 $json->canonical;
98         }
99
100         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
101         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
102         $mset_opt{qstr} = join(' ', map {;
103                 # Consider spaces in argv to be for phrase search in Xapian.
104                 # In other words, the users should need only care about
105                 # normal shell quotes and not have to learn Xapian quoting.
106                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
107         } @argv);
108         if (defined(my $sort = $opt->{'sort'})) {
109                 if ($sort eq 'relevance') {
110                         $mset_opt{relevance} = 1;
111                 } elsif ($sort eq 'docid') {
112                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
113                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
114                         # the default
115                 } else {
116                         die "unrecognized --sort=$sort\n";
117                 }
118         }
119         # $self->out($json->encode(\%mset_opt));
120         # descending docid order
121         $mset_opt{relevance} //= -2 if $opt->{thread};
122         # my $wcb = PublicInbox::LeiToMail->write_cb($out, $self);
123         $self->{mset_opt} = \%mset_opt;
124         $lxs->do_query($self, \@srcs);
125         if ($pid_old12) {
126                 $self->{$_} = $pid_old12->[$_] for (1, 2);
127                 dwaitpid($pid_old12->[0], undef, $self->{sock});
128         }
129 }
130
131 1;