]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiSavedSearch.pm
lei q: update messages to reflect --save default
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.pm
index 8ceaaf018ec628a6cba2132304304797ce733f4f..637456e41e9a728475148265cea17fd1150abcfa 100644 (file)
@@ -29,31 +29,23 @@ sub BOOL_FIELDS () {
        qw(external local remote import-remote import-before threads)
 }
 
-sub cfg_dump ($$) {
-       my ($lei, $f) = @_;
-       my $ret = eval { PublicInbox::Config->git_config_dump($f, $lei->{2}) };
-       return $ret if !$@;
-       $lei->err($@);
-       undef;
-}
-
 sub lss_dir_for ($$;$) {
        my ($lei, $dstref, $on_fs) = @_;
-       my @n;
+       my $pfx;
        if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
                require PublicInbox::URIimap;
                my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
                $$dstref = $$uri;
-               @n = ($uri->mailbox);
+               $pfx = $uri->mailbox;
        } else {
                # can't use Cwd::abs_path since dirname($$dstref) may not exist
                $$dstref = $lei->rel2abs($$dstref);
                $$dstref =~ tr!/!/!s;
-               @n = ($$dstref =~ m{([^/]+)/*\z}); # basename
+               $pfx = $$dstref;
        }
-       push @n, sha256_hex($$dstref);
+       ($pfx) = ($pfx =~ m{([^/]+)/*\z}); # basename
        my $lss_dir = $lei->share_path . '/saved-searches/';
-       my $d = $lss_dir . join('-', @n);
+       my $d = "$lss_dir$pfx-".sha256_hex($$dstref);
 
        # fall-back to looking up by st_ino + st_dev in case we're in
        # a symlinked or bind-mounted path
@@ -61,10 +53,10 @@ sub lss_dir_for ($$;$) {
                my @cur = stat(_);
                my $want = pack('dd', @cur[1,0]); # st_ino + st_dev
                my ($c, $o, @st);
-               for my $g ("$n[0]-*", '*') {
+               for my $g ("$pfx-*", '*') {
                        my @maybe = glob("$lss_dir$g/lei.saved-search");
                        for my $f (@maybe) {
-                               $c = cfg_dump($lei, $f) // next;
+                               $c = $lei->cfg_dump($f) // next;
                                $o = $c->{'lei.q.output'} // next;
                                $o =~ s!$LOCAL_PFX!! or next;
                                @st = stat($o) or next;
@@ -88,7 +80,7 @@ sub list {
                print $fh "\tpath = ", cquote_val($p), "\n";
        }
        close $fh or die "close $f: $!";
-       my $cfg = cfg_dump($lei, $f);
+       my $cfg = $lei->cfg_dump($f);
        unlink($f);
        my $out = $cfg ? $cfg->get_all('lei.q.output') : [];
        map {;
@@ -102,7 +94,7 @@ sub translate_dedupe ($$$) {
        $dd //= 'content';
        return 1 if $dd eq 'content'; # the default
        return $self->{"-dedupe_$dd"} = 1 if ($dd eq 'oid' || $dd eq 'mid');
-       $lei->fail("--dedupe=$dd unsupported with --save");
+       $lei->fail("--dedupe=$dd requires --no-save");
 }
 
 sub up { # updating existing saved search via "lei up"
@@ -111,9 +103,9 @@ sub up { # updating existing saved search via "lei up"
        my $self = bless { ale => $lei->ale }, $cls;
        my $dir = $dst;
        output2lssdir($self, $lei, \$dir, \$f) or
-               return $lei->fail("--save was not used with $dst cwd=".
+               return $lei->fail("--no-save was used with $dst cwd=".
                                        $lei->rel2abs('.'));
-       $self->{-cfg} = cfg_dump($lei, $f) // return $lei->fail;
+       $self->{-cfg} = $lei->cfg_dump($f) // return $lei->fail;
        $self->{-ovf} = "$dir/over.sqlite3";
        $self->{'-f'} = $f;
        $self->{lock_path} = "$self->{-f}.flock";
@@ -149,6 +141,7 @@ sub new { # new saved search "lei q --save"
        $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
        print $fh <<EOM;
 ; to refresh with new results, run: lei up $sq_dst
+; `maxuid' and `lastresult' lines are maintained by "lei up" for optimization
 [lei]
 $q
 [lei "q"]
@@ -275,7 +268,7 @@ sub output2lssdir {
        my $dir = lss_dir_for($lei, \$dst, 1);
        my $f = "$dir/lei.saved-search";
        if (-f $f && -r _) {
-               $self->{-cfg} = cfg_dump($lei, $f) // return;
+               $self->{-cfg} = $lei->cfg_dump($f) // return;
                $$dir_ref = $dir;
                $$fn_ref = $f;
                return 1;