]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei_query: remove unnecessary V2Writable require
[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 POSIX ();
9
10 sub prep_ext { # externals_each callback
11         my ($lxs, $exclude, $loc) = @_;
12         $lxs->prepare_external($loc) unless $exclude->{$loc};
13 }
14
15 sub _start_query {
16         my ($self) = @_;
17         if ($self->{net}) {
18                 require PublicInbox::LeiAuth;
19                 $self->{auth} = PublicInbox::LeiAuth->new
20         }
21         $self->{lxs}->do_query($self);
22 }
23
24 sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
25         my ($self) = @_; # $_[1] = $rbuf
26         if (defined($_[1])) {
27                 $_[1] eq '' and return eval {
28                         $self->{lse}->query_approxidate($self->{lse}->git,
29                                                 $self->{mset_opt}->{qstr});
30                         _start_query($self);
31                 };
32                 $self->{mset_opt}->{qstr} .= $_[1];
33         } else {
34                 $self->fail("error reading stdin: $!");
35         }
36 }
37
38 sub lxs_prepare {
39         my ($self) = @_;
40         require PublicInbox::LeiXSearch;
41         # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
42         my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
43         my $opt = $self->{opt};
44         my @only = @{$opt->{only} // []};
45         # --local is enabled by default unless --only is used
46         # we'll allow "--only $LOCATION --local"
47         my $sto = $self->_lei_store(1); # FIXME: should not create
48         $self->{lse} = $sto->search;
49         if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
50                 $lxs->prepare_external($self->{lse});
51         }
52         if (@only) {
53                 for my $loc (@only) {
54                         my @loc = $self->get_externals($loc) or return;
55                         $lxs->prepare_external($_) for @loc;
56                 }
57         } else {
58                 my (@ilocals, @iremotes);
59                 for my $loc (@{$opt->{include} // []}) {
60                         my @loc = $self->get_externals($loc) or return;
61                         $lxs->prepare_external($_) for @loc;
62                         @ilocals = @{$lxs->{locals} // []};
63                         @iremotes = @{$lxs->{remotes} // []};
64                 }
65                 # --external is enabled by default, but allow --no-external
66                 if ($opt->{external} //= 1) {
67                         my %x;
68                         for my $loc (@{$opt->{exclude} // []}) {
69                                 my @l = $self->get_externals($loc, 1) or return;
70                                 $x{$_} = 1 for @l;
71                         }
72                         my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
73                         $opt->{remote} //= !($lxs->locals - $opt->{'local'});
74                         if ($opt->{'local'}) {
75                                 $lxs->{remotes} = \@iremotes if !$opt->{remote};
76                         } else {
77                                 $lxs->{locals} = \@ilocals;
78                         }
79                 }
80         }
81         ($lxs->locals || $lxs->remotes) ? ($self->{lxs} = $lxs) :
82                 $self->fail('no local or remote inboxes to search');
83 }
84
85 # the main "lei q SEARCH_TERMS" method
86 sub lei_q {
87         my ($self, @argv) = @_;
88         require PublicInbox::LeiOverview;
89         PublicInbox::Config->json; # preload before forking
90         PublicInbox::LeiOverview->new($self) or return;
91         my $lxs = lxs_prepare($self) or return;
92         $self->ale->refresh_externals($lxs);
93         my $opt = $self->{opt};
94         my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
95         if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
96                 return $self->fail("`$xj' search jobs must be >= 1");
97         }
98         $xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
99         my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
100         $xj = $nproc if $xj > $nproc;
101         $lxs->{-wq_nr_workers} = $xj;
102         if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
103                 return $self->fail("`$mj' writer jobs must be >= 1");
104         }
105         if ($self->{l2m} && ($opt->{'import-remote'} //= 1) |
106                                 # we use \1 (a ref) to distinguish between
107                                 # user-supplied and default value
108                                 (($opt->{'import-before'} //= \1) ? 1 : 0)) {
109                 $self->_lei_store(1)->write_prepare($self);
110         }
111         $self->{l2m} and $self->{l2m}->{-wq_nr_workers} = $mj // do {
112                 $mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
113                 $mj <= 0 ? 1 : $mj;
114         };
115
116         my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
117         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
118         $mset_opt{limit} //= 10000;
119         if (defined(my $sort = $opt->{'sort'})) {
120                 if ($sort eq 'relevance') {
121                         $mset_opt{relevance} = 1;
122                 } elsif ($sort eq 'docid') {
123                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
124                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
125                         # the default
126                 } else {
127                         die "unrecognized --sort=$sort\n";
128                 }
129         }
130         # descending docid order is cheapest, MUA controls sorting order
131         $mset_opt{relevance} //= -2 if $self->{l2m} || $opt->{threads};
132         $self->{mset_opt} = \%mset_opt;
133
134         if ($opt->{stdin}) {
135                 return $self->fail(<<'') if @argv;
136 no query allowed on command-line with --stdin
137
138                 require PublicInbox::InputPipe;
139                 PublicInbox::InputPipe::consume($self->{0}, \&qstr_add, $self);
140                 return;
141         }
142         $mset_opt{qstr} =
143                 $self->{lse}->query_argv_to_string($self->{lse}->git, \@argv);
144         _start_query($self);
145 }
146
147 # shell completion helper called by lei__complete
148 sub _complete_q {
149         my ($self, @argv) = @_;
150         my $ext = qr/\A(?:-I|(?:--(?:include|exclude|only)))\z/;
151         my @cur;
152         while (@argv) {
153                 if ($argv[-1] =~ $ext) {
154                         my @c = $self->_complete_forget_external(@cur);
155                         # try basename match:
156                         if (scalar(@cur) == 1 && index($cur[0], '/') < 0) {
157                                 my $all = $self->externals_each;
158                                 my %bn;
159                                 for my $loc (keys %$all) {
160                                         my $bn = (split(m!/!, $loc))[-1];
161                                         ++$bn{$bn};
162                                 }
163                                 push @c, grep {
164                                         $bn{$_} == 1 && /\A\Q$cur[0]/
165                                 } keys %bn;
166                         }
167                         return @c if @c;
168                 }
169                 unshift(@cur, pop @argv);
170         }
171         ();
172 }
173
174 # Stuff we may pass through to curl (as of 7.64.0), see curl manpage for
175 # details, so most options which make sense for HTTP/HTTPS (including proxy
176 # support for Tor and other methods of getting past weird networks).
177 # Most of these are untested by us, some may not make sense for our use case
178 # and typos below are likely.
179 # n.b. some short options (-$NUMBER) are not supported since they conflict
180 # with other "lei q" switches.
181 # FIXME: Getopt::Long doesn't easily let us support support options with
182 # '.' in them (e.g. --http1.1)
183 # TODO: should we depend on "-c http.*" options for things which have
184 # analogues in git(1)? that would reduce likelyhood of conflicts with
185 # our other CLI options
186 # Note: some names are renamed to avoid potential conflicts,
187 # see %lei2mail in lib/PublicInbox/LeiCurl.pm
188 sub curl_opt { qw(
189         curl-config=s@
190         abstract-unix-socket=s anyauth basic cacert=s capath=s
191         cert-status cert-type cert=s ciphers=s
192         connect-timeout=s connect-to=s cookie-jar=s cookie=s crlfile=s
193         digest disable dns-interface=s dns-ipv4-addr=s dns-ipv6-addr=s
194         dns-servers=s doh-url=s egd-file=s engine=s false-start
195         happy-eyeballs-timeout-ms=s haproxy-protocol header=s@
196         http2-prior-knowledge http2 insecure
197         interface=s ipv4 ipv6 junk-session-cookies
198         key-type=s key=s limit-rate=s local-port=s location-trusted location
199         max-redirs=i max-time=s negotiate netrc-file=s netrc-optional netrc
200         no-alpn no-buffer no-npn no-sessionid noproxy=s ntlm-wb ntlm
201         pass=s pinnedpubkey=s post301 post302 post303 preproxy=s
202         proxy-anyauth proxy-basic proxy-cacert=s proxy-capath=s
203         proxy-cert-type=s proxy-cert=s proxy-ciphers=s proxy-crlfile=s
204         proxy-digest proxy-header=s@ proxy-insecure
205         proxy-key-type=s proxy-key proxy-negotiate proxy-ntlm proxy-pass=s
206         proxy-pinnedpubkey=s proxy-service-name=s proxy-ssl-allow-beast
207         proxy-tls13-ciphers=s proxy-tlsauthtype=s proxy-tlspassword=s
208         proxy-tlsuser=s proxy-tlsv1 proxy-user=s proxy=s
209         proxytunnel=s pubkey=s random-file=s referer=s resolve=s
210         retry-connrefused retry-delay=s retry-max-time=s retry=i
211         sasl-ir service-name=s socks4=s socks4a=s socks5-basic
212         socks5-gssapi-service-name=s socks5-gssapi socks5-hostname=s socks5=s
213         speed-limit speed-type ssl-allow-beast sslv2 sslv3
214         suppress-connect-headers tcp-fastopen tls-max=s
215         tls13-ciphers=s tlsauthtype=s tlspassword=s tlsuser=s
216         tlsv1 trace-ascii=s trace-time trace=s
217         unix-socket=s user-agent=s user=s
218 )
219 }
220
221 1;