]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiSavedSearch.pm
get rid of unnecessary bytes::length usage
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.pm
index af864a50aadeb319dd08b4a548642b05337e87a0..fd51fe38681612c6f9f07a7aec888a3700e1d160 100644 (file)
@@ -6,6 +6,7 @@ package PublicInbox::LeiSavedSearch;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::Lock);
+use PublicInbox::Git;
 use PublicInbox::OverIdx;
 use PublicInbox::LeiSearch;
 use PublicInbox::Config;
@@ -13,7 +14,7 @@ use PublicInbox::Spawn qw(run_die);
 use PublicInbox::ContentHash qw(git_sha);
 use PublicInbox::MID qw(mids_for_index);
 use Digest::SHA qw(sha256_hex);
-my $LOCAL_PFX = qr!\A(?:maildir|mh|mbox.+|mmdf):!i; # TODO: put in LeiToMail?
+my $LOCAL_PFX = qr!\A(?:maildir|mh|mbox.+|mmdf|v2):!i; # TODO: put in LeiToMail?
 
 # move this to PublicInbox::Config if other things use it:
 my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
@@ -39,8 +40,6 @@ sub lss_dir_for ($$;$) {
        } else {
                # can't use Cwd::abs_path since dirname($$dstref) may not exist
                $$dstref = $lei->rel2abs($$dstref);
-               # Maildirs have trailing '/' internally
-               $$dstref .= '/' if -d $$dstref;
                $$dstref =~ tr!/!/!s;
                @n = ($$dstref =~ m{([^/]+)/*\z}); # basename
        }
@@ -116,9 +115,16 @@ sub up { # updating existing saved search via "lei up"
 sub new { # new saved search "lei q --save"
        my ($cls, $lei) = @_;
        my $self = bless { ale => $lei->ale }, $cls;
+       require File::Path;
        my $dst = $lei->{ovv}->{dst};
+
+       # canonicalize away relative paths into the config
+       if ($lei->{ovv}->{fmt} eq 'maildir' &&
+                       $dst =~ m!(?:/*|\A)\.\.(?:/*|\z)! && !-d $dst) {
+               File::Path::make_path($dst);
+               $lei->{ovv}->{dst} = $dst = $lei->abs_path($dst);
+       }
        my $dir = lss_dir_for($lei, \$dst);
-       require File::Path;
        File::Path::make_path($dir); # raises on error
        $self->{-cfg} = {};
        my $f = $self->{'-f'} = "$dir/lei.saved-search";
@@ -136,7 +142,7 @@ sub new { # new saved search "lei q --save"
        print $fh <<EOM;
 ; to refresh with new results, run: lei up $sq_dst
 [lei]
-       $q
+$q
 [lei "q"]
        output = $dst
 EOM
@@ -169,23 +175,24 @@ sub cfg_set { # called by LeiXSearch
 sub is_dup {
        my ($self, $eml, $smsg) = @_;
        my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
-       my $blob = $smsg ? $smsg->{blob} : undef;
-       my $lk = $self->lock_for_scope_fast;
-       return 1 if $blob && $oidx->blob_exists($blob);
+       my $lk;
        if ($self->{-dedupe_mid}) {
+               $lk //= $self->lock_for_scope_fast;
                for my $mid (@{mids_for_index($eml)}) {
                        my ($id, $prv);
                        return 1 if $oidx->next_by_mid($mid, \$id, \$prv);
                }
        }
+       my $blob = $smsg ? $smsg->{blob} : git_sha(1, $eml)->hexdigest;
+       $lk //= $self->lock_for_scope_fast;
+       return 1 if $oidx->blob_exists($blob);
        if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
                for my $docid (values %$xoids) {
                        $oidx->add_xref3($docid, -1, $blob, '.');
                }
                $oidx->commit_lazy;
                if ($self->{-dedupe_oid}) {
-                       $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
-                       exists $xoids->{$smsg->{blob}} ? 1 : undef;
+                       exists $xoids->{$blob} ? 1 : undef;
                } else {
                        1;
                }
@@ -196,11 +203,11 @@ sub is_dup {
                        $smsg->{bytes} = 0;
                        $smsg->populate($eml);
                }
+               $smsg->{blob} //= $blob;
                $oidx->begin_lazy;
                $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
-               $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
                $oidx->add_overview($eml, $smsg);
-               $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
+               $oidx->add_xref3($smsg->{num}, -1, $blob, '.');
                $oidx->commit_lazy;
                undef;
        }
@@ -224,16 +231,28 @@ sub prepare_dedupe {
 
 sub over { $_[0]->{oidx} } # for xoids_for
 
-sub git { $_[0]->{ale}->git }
+# don't use ale->git directly since is_dup is called inside
+# ale->git->cat_async callbacks
+sub git { $_[0]->{git} //= PublicInbox::Git->new($_[0]->{ale}->git->{git_dir}) }
 
 sub pause_dedupe {
        my ($self) = @_;
-       $self->{ale}->git->cleanup;
+       git($self)->cleanup;
        my $lockfh = delete $self->{lockfh}; # from lock_for_scope_fast;
        my $oidx = delete($self->{oidx}) // return;
        $oidx->commit_lazy;
 }
 
+sub reset_dedupe {
+       my ($self) = @_;
+       prepare_dedupe($self);
+       my $lk = $self->lock_for_scope_fast;
+       for my $t (qw(xref3 over id2num)) {
+               $self->{oidx}->{dbh}->do("DELETE FROM $t");
+       }
+       pause_dedupe($self);
+}
+
 sub mm { undef }
 
 sub altid_map { {} }
@@ -256,6 +275,70 @@ sub output2lssdir {
        undef;
 }
 
+sub edit_begin {
+       my ($self, $lei) = @_;
+       if (ref($self->{-cfg}->{'lei.q.output'})) {
+               delete $self->{-cfg}->{'lei.q.output'}; # invalid
+               $lei->err(<<EOM);
+$self->{-f} has multiple values of lei.q.output
+please remove redundant ones
+EOM
+       }
+       $lei->{-lss_for_edit} = $self;
+}
+
+sub edit_done {
+       my ($self, $lei) = @_;
+       my $cfg = PublicInbox::Config->git_config_dump($self->{'-f'});
+       my $new_out = $cfg->{'lei.q.output'} // '';
+       return $lei->fail(<<EOM) if ref $new_out;
+$self->{-f} has multiple values of lei.q.output
+please edit again
+EOM
+       return $lei->fail(<<EOM) if $new_out eq '';
+$self->{-f} needs lei.q.output
+please edit again
+EOM
+       my $old_out = $self->{-cfg}->{'lei.q.output'} // '';
+       return if $old_out eq $new_out;
+       my $old_path = $old_out;
+       my $new_path = $new_out;
+       s!$LOCAL_PFX!! for ($old_path, $new_path);
+       my $dir_old = lss_dir_for($lei, \$old_path, 1);
+       my $dir_new = lss_dir_for($lei, \$new_path);
+       return if $dir_new eq $dir_old; # no change, likely
+
+       ($old_out =~ m!\Av2:!i || $new_out =~ m!\Av2:!) and
+               return $lei->fail(<<EOM);
+conversions from/to v2 inboxes not supported at this time
+EOM
+
+       return $lei->fail(<<EOM) if -e $dir_new;
+lei.q.output changed from `$old_out' to `$new_out'
+However, $dir_new exists
+EOM
+       # start the conversion asynchronously
+       my $old_sq = PublicInbox::Config::squote_maybe($old_out);
+       my $new_sq = PublicInbox::Config::squote_maybe($new_out);
+       $lei->puts("lei.q.output changed from $old_sq to $new_sq");
+       $lei->qerr("# lei convert $old_sq -o $new_sq");
+       my $v = !$lei->{opt}->{quiet};
+       $lei->{opt} = { output => $new_out, verbose => $v };
+       require PublicInbox::LeiConvert;
+       PublicInbox::LeiConvert::lei_convert($lei, $old_out);
+
+       $lei->fail(<<EOM) if -e $dir_old && !rename($dir_old, $dir_new);
+E: rename($dir_old, $dir_new) error: $!
+EOM
+}
+
+# cf. LeiDedupe->has_entries
+sub has_entries {
+       my $oidx = $_[0]->{oidx} // die 'BUG: no {oidx}';
+       my @n = $oidx->{dbh}->selectrow_array('SELECT num FROM over LIMIT 1');
+       scalar(@n) ? 1 : undef;
+}
+
 no warnings 'once';
 *nntp_url = \&cloneurl;
 *base_url = \&PublicInbox::Inbox::base_url;