]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiUp.pm
lei q: improve --limit behavior and progress
[public-inbox.git] / lib / PublicInbox / LeiUp.pm
index be476427bc64f4598c0c6a2dc104d5cf9dde244f..89cf0112e9b58964d0ddaf9e363cdb3e90042d2a 100644 (file)
@@ -11,7 +11,6 @@ use PublicInbox::LeiSavedSearch;
 use PublicInbox::DS;
 use PublicInbox::PktOp;
 use PublicInbox::LeiFinmsg;
-use PublicInbox::LEI;
 my $REMOTE_RE = qr!\A(?:imap|http)s?://!i; # http(s) will be for JMAP
 
 sub up1 ($$) {
@@ -19,7 +18,6 @@ sub up1 ($$) {
        my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
        my $f = $lss->{'-f'};
        my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
-       $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
        my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
                                return $lei->fail("lei.q unset in $f");
        my $lse = $lei->{lse} // die 'BUG: {lse} missing';
@@ -28,25 +26,19 @@ sub up1 ($$) {
        } else {
                $lse->query_approxidate($lse->git, $mset_opt->{qstr} = $q);
        }
-       my $o = $lei->{opt}->{output} = $lss->{-cfg}->{'lei.q.output'} //
-               return $lei->fail("lei.q.output unset in $f");
-       ref($o) and return $lei->fail("multiple values of lei.q.output in $f");
-       if (defined(my $dd = $lss->{-cfg}->{'lei.q.dedupe'})) {
-               $lss->translate_dedupe($lei, $dd) or return;
-               $lei->{opt}->{dedupe} = $dd;
-       }
-       for my $k (qw(only include exclude)) {
+       # n.b. only a few CLI args are accepted for "up", so //= usually sets
+       for my $k ($lss->ARRAY_FIELDS) {
                my $v = $lss->{-cfg}->get_all("lei.q.$k") // next;
-               $lei->{opt}->{$k} = $v;
+               $lei->{opt}->{$k} //= $v;
        }
-       for my $k (qw(external local remote
-                       import-remote import-before threads)) {
-               my $c = "lei.q.$k";
-               my $v = $lss->{-cfg}->{$c} // next;
-               ref($v) and return $lei->fail("multiple values of $c in $f");
-               $lei->{opt}->{$k} = $v;
+       for my $k ($lss->BOOL_FIELDS, $lss->SINGLE_FIELDS) {
+               my $v = $lss->{-cfg}->get_1('lei.q', $k) // next;
+               $lei->{opt}->{$k} //= $v;
        }
-       $lei->{lss} = $lss; # for LeiOverview->new
+       my $o = $lei->{opt}->{output} // '';
+       return $lei->fail("lei.q.output unset in $f") if $o eq '';
+       $lss->translate_dedupe($lei) or return;
+       $lei->{lss} = $lss; # for LeiOverview->new and query_remote_mboxrd
        my $lxs = $lei->lxs_prepare or return;
        $lei->ale->refresh_externals($lxs, $lei);
        $lei->_start_query;
@@ -54,19 +46,22 @@ sub up1 ($$) {
 
 sub up1_redispatch {
        my ($lei, $out, $op_p) = @_;
-       my $l = bless { %$lei }, ref($lei);
-       $l->{opt} = { %{$l->{opt}} };
-       delete $l->{sock}; # do not close
-       $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
-
-       # make close($l->{1}) happy in lei->dclose
-       open my $fh, '>&', $l->{1} or return $l->child_error(0, "dup: $!");
-       local $PublicInbox::LEI::current_lei = $l;
-       $l->{1} = $fh;
-       eval {
+       my $l;
+       if (defined($lei->{opt}->{mua})) { # single output
+               $l = $lei;
+       } else { # multiple outputs
+               $l = bless { %$lei }, ref($lei);
+               $l->{opt} = { %{$l->{opt}} }; # deep copy
+               delete $l->{opt}->{all};
+               delete $l->{sock}; # do not close
+               # make close($l->{1}) happy in lei->dclose
+               open my $fh, '>&', $l->{1} or
+                       return $l->child_error(0, "dup: $!");
+               $l->{1} = $fh;
                $l->qerr("# updating $out");
-               up1($l, $out);
-       };
+       }
+       $l->{''} = $op_p; # daemon only ($l => $lei => script/lei)
+       eval { $l->dispatch('up', $out) };
        $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
 }
 
@@ -88,10 +83,9 @@ sub lei_up {
        my ($lei, @outs) = @_;
        my $opt = $lei->{opt};
        my $self = bless { -mail_sync => 1 }, __PACKAGE__;
-       $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
        if (defined(my $all = $opt->{all})) {
                return $lei->fail("--all and @outs incompatible") if @outs;
-               length($opt->{mua}//'') and return
+               defined($opt->{mua}) and return
                        $lei->fail('--all and --mua= are incompatible');
                @outs = PublicInbox::LeiSavedSearch::list($lei);
                if ($all eq 'local') {
@@ -104,12 +98,16 @@ sub lei_up {
                } else {
                        $lei->fail("only --all=$all not understood");
                }
+       } elsif ($lei->{lse}) {
+               scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
+               return up1($lei, $outs[0]);
        } else {
                $self->{remote} = [ grep(/$REMOTE_RE/, @outs) ];
                $self->{local} = [ grep(!/$REMOTE_RE/, @outs) ];
        }
+       $lei->{lse} = $lei->_lei_store(1)->write_prepare($lei)->search;
        ((@{$self->{local} // []} + @{$self->{remote} // []}) > 1 &&
-               length($opt->{mua} // '')) and return $lei->fail(<<EOM);
+               defined($opt->{mua})) and return $lei->fail(<<EOM);
 multiple outputs and --mua= are incompatible
 EOM
        if ($self->{remote}) { # setup lei->{auth}