From 26e0fe73de93f451ae5575f7625fec8ae020673c Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Tue, 13 Apr 2021 10:54:44 +0000
Subject: [PATCH] lei_query: rearrange internals to capture query early

To support saved search, we need the query string available to
us before we setup LeiDedupe via (LeiOverview || LeiToMail).
---
 lib/PublicInbox/LeiQuery.pm | 55 +++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 26 deletions(-)

diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm
index 9174bea8..224eba69 100644
--- a/lib/PublicInbox/LeiQuery.pm
+++ b/lib/PublicInbox/LeiQuery.pm
@@ -14,11 +14,39 @@ sub prep_ext { # externals_each callback
 
 sub _start_query {
 	my ($self) = @_;
+	PublicInbox::LeiOverview->new($self) or return;
+	my $opt = $self->{opt};
+	my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
+	if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
+		return $self->fail("`$xj' search jobs must be >= 1");
+	}
+	my $lxs = $self->{lxs};
+	$xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
+	my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
+	$xj = $nproc if $xj > $nproc;
+	$lxs->{-wq_nr_workers} = $xj;
+	if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
+		return $self->fail("`$mj' writer jobs must be >= 1");
+	}
+	my $l2m = $self->{l2m};
+	if ($l2m && ($opt->{'import-remote'} //= 1) |
+				# we use \1 (a ref) to distinguish between
+				# user-supplied and default value
+				(($opt->{'import-before'} //= \1) ? 1 : 0)) {
+		$self->_lei_store(1)->write_prepare($self);
+	}
+	$l2m and $l2m->{-wq_nr_workers} = $mj // do {
+		$mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
+		$mj <= 0 ? 1 : $mj;
+	};
+
+	# descending docid order is cheapest, MUA controls sorting order
+	$self->{mset_opt}->{relevance} //= -2 if $l2m || $opt->{threads};
 	if ($self->{net}) {
 		require PublicInbox::LeiAuth;
 		$self->{auth} = PublicInbox::LeiAuth->new
 	}
-	$self->{lxs}->do_query($self);
+	$lxs->do_query($self);
 }
 
 sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
@@ -87,32 +115,9 @@ sub lei_q {
 	my ($self, @argv) = @_;
 	require PublicInbox::LeiOverview;
 	PublicInbox::Config->json; # preload before forking
-	PublicInbox::LeiOverview->new($self) or return;
 	my $lxs = lxs_prepare($self) or return;
 	$self->ale->refresh_externals($lxs);
 	my $opt = $self->{opt};
-	my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
-	if (defined($xj) && $xj ne '' && $xj !~ /\A[1-9][0-9]*\z/) {
-		return $self->fail("`$xj' search jobs must be >= 1");
-	}
-	$xj ||= $lxs->concurrency($opt); # allow: "--jobs ,$WRITER_ONLY"
-	my $nproc = $lxs->detect_nproc // 1; # don't memoize, schedtool(1) exists
-	$xj = $nproc if $xj > $nproc;
-	$lxs->{-wq_nr_workers} = $xj;
-	if (defined($mj) && $mj !~ /\A[1-9][0-9]*\z/) {
-		return $self->fail("`$mj' writer jobs must be >= 1");
-	}
-	if ($self->{l2m} && ($opt->{'import-remote'} //= 1) |
-				# we use \1 (a ref) to distinguish between
-				# user-supplied and default value
-				(($opt->{'import-before'} //= \1) ? 1 : 0)) {
-		$self->_lei_store(1)->write_prepare($self);
-	}
-	$self->{l2m} and $self->{l2m}->{-wq_nr_workers} = $mj // do {
-		$mj = POSIX::lround($nproc * 3 / 4); # keep some CPU for git
-		$mj <= 0 ? 1 : $mj;
-	};
-
 	my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
 	$mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
 	$mset_opt{limit} //= 10000;
@@ -127,8 +132,6 @@ sub lei_q {
 			die "unrecognized --sort=$sort\n";
 		}
 	}
-	# descending docid order is cheapest, MUA controls sorting order
-	$mset_opt{relevance} //= -2 if $self->{l2m} || $opt->{threads};
 	$self->{mset_opt} = \%mset_opt;
 
 	if ($opt->{stdin}) {
-- 
2.50.0