]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inbox: simplify v2 epoch counting
authorEric Wong <e@80x24.org>
Wed, 16 Dec 2020 23:19:04 +0000 (23:19 +0000)
committerEric Wong <e@80x24.org>
Thu, 17 Dec 2020 19:41:14 +0000 (19:41 +0000)
Perl readdir detects list context and can return an array
suitable for the grep op.  From there, we can rely on
substr to remove the ".git" suffix and integerize the value
to save a few bytes before letting List::Util::max return
the value.

This is how we detect Xapian shards nowadays, too, and
we'll also use defined-or (//) to simplify the return
value there.

We'll also simplify InboxWritable->git_dir_latest,
remove some callers, and consider removing it entirely.

lib/PublicInbox/ExtSearchIdx.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/Search.pm
lib/PublicInbox/V2Writable.pm
lib/PublicInbox/Xapcmd.pm

index f492734a69da4c2210db9f7ed5a9b430481e9898..3764612cf149c9281c9b8e3b21603f3c0e859486 100644 (file)
@@ -290,9 +290,7 @@ sub _sync_inbox ($$$) {
        my $v = $ibx->version;
        my $ekey = $ibx->eidx_key;
        if ($v == 2) {
-               my $epoch_max;
-               defined($ibx->git_dir_latest(\$epoch_max)) or return;
-               $sync->{epoch_max} = $epoch_max;
+               $sync->{epoch_max} = $ibx->max_git_epoch // return;
                sync_prepare($self, $sync); # or return # TODO: once MiscIdx is stable
        } elsif ($v == 1) {
                my $uv = $ibx->uidvalidity;
index bd1de0a004000b96121946fd863b777c37f4d1ee..8a3a01940a008473bebab3d307608f1d432762f8 100644 (file)
@@ -4,10 +4,10 @@
 # Represents a public-inbox (which may have multiple mailing addresses)
 package PublicInbox::Inbox;
 use strict;
-use warnings;
 use PublicInbox::Git;
 use PublicInbox::MID qw(mid2path);
 use PublicInbox::Eml;
+use List::Util qw(max);
 
 # Long-running "git-cat-file --batch" processes won't notice
 # unlinked packs, so we need to restart those processes occasionally.
@@ -155,19 +155,15 @@ sub max_git_epoch {
        my ($self) = @_;
        return if $self->version < 2;
        my $cur = $self->{-max_git_epoch};
-       my $changed = git($self)->alternates_changed;
-       if (!defined($cur) || $changed) {
+       my $changed;
+       if (!defined($cur) || ($changed = git($self)->alternates_changed)) {
                git_cleanup($self) if $changed;
                my $gits = "$self->{inboxdir}/git";
                if (opendir my $dh, $gits) {
-                       my $max = -1;
-                       while (defined(my $git_dir = readdir($dh))) {
-                               $git_dir =~ m!\A([0-9]+)\.git\z! or next;
-                               $max = $1 if $1 > $max;
-                       }
-                       $cur = $self->{-max_git_epoch} = $max if $max >= 0;
-               } else {
-                       warn "opendir $gits failed: $!\n";
+                       my $max = max(map {
+                               substr($_, 0, -4) + 0; # drop ".git" suffix
+                       } grep(/\A[0-9]+\.git\z/, readdir($dh))) // return;
+                       $cur = $self->{-max_git_epoch} = $max;
                }
        }
        $cur;
index bdfae2f888b7e9ed07f9a2425173ab16395dde7d..48d2267f3bfacb2cdd29049aec1d680103169850 100644 (file)
@@ -303,22 +303,11 @@ sub warn_ignore_cb {
 # v2+ only
 sub git_dir_n { "$_[0]->{inboxdir}/git/$_[1].git" }
 
-# v2+ only
+# v2+ only, XXX: maybe we can just rely on ->max_git_epoch and remove
 sub git_dir_latest {
        my ($self, $max) = @_;
-       $$max = -1;
-       my $pfx = "$self->{inboxdir}/git";
-       return unless -d $pfx;
-       my $latest;
-       opendir my $dh, $pfx or die "opendir $pfx: $!\n";
-       while (defined(my $git_dir = readdir($dh))) {
-               $git_dir =~ m!\A([0-9]+)\.git\z! or next;
-               if ($1 > $$max) {
-                       $$max = $1;
-                       $latest = "$pfx/$git_dir";
-               }
-       }
-       $latest;
+       defined($$max = $self->max_git_epoch) ?
+               "$self->{inboxdir}/git/$$max.git" : undef;
 }
 
 1;
index 803914b02f596d45b6d03087180b4afb14f3f9b3..b1d38fb901b2f0fcce07513bd559be71b812fa60 100644 (file)
@@ -197,8 +197,7 @@ sub xdb_sharded {
 
        # We need numeric sorting so shard[0] is first for reading
        # Xapian metadata, if needed
-       my $last = max(grep(/\A[0-9]+\z/, readdir($dh)));
-       return if !defined($last);
+       my $last = max(grep(/\A[0-9]+\z/, readdir($dh))) // return;
        my (@xdb, $slow_phrase);
        for (0..$last) {
                my $shard_dir = "$self->{xpfx}/$_";
index 992305c5dda66d012050103b11e1674c1763b581..7b8b5abf7a87140c67a3ffcbfeea893dbee610af 100644 (file)
@@ -128,12 +128,9 @@ sub init_inbox {
        }
        $self->idx_init;
        $self->{mm}->skip_artnum($skip_artnum) if defined $skip_artnum;
-       my $epoch_max = -1;
-       $self->{ibx}->git_dir_latest(\$epoch_max);
-       if (defined $skip_epoch && $epoch_max == -1) {
-               $epoch_max = $skip_epoch;
-       }
-       $self->git_init($epoch_max >= 0 ? $epoch_max : 0);
+       my $max = $self->{ibx}->max_git_epoch;
+       $max = $skip_epoch if (defined($skip_epoch) && !defined($max));
+       $self->git_init($max // 0);
        $self->done;
 }
 
@@ -336,12 +333,7 @@ sub _replace_oids ($$$) {
        my $ibx = $self->{ibx};
        my $pfx = "$ibx->{inboxdir}/git";
        my $rewrites = []; # epoch => commit
-       my $max = $self->{epoch_max};
-
-       unless (defined($max)) {
-               defined(my $latest = $ibx->git_dir_latest(\$max)) or return;
-               $self->{epoch_max} = $max;
-       }
+       my $max = $self->{epoch_max} //= $ibx->max_git_epoch // return;
 
        foreach my $i (0..$max) {
                my $git_dir = "$pfx/$i.git";
index 4332943cd62008f8da2179dae3eff44895f5a8cc..4f77ef25333abe91eacb256f0cf7d41f15f1f031 100644 (file)
@@ -109,8 +109,7 @@ sub prepare_reindex ($$$) {
                        $opt->{reindex}->{from} = $lc;
                }
        } else { # v2
-               my $max;
-               $ibx->git_dir_latest(\$max) or return;
+               my $max = $ibx->max_git_epoch // return;
                my $from = $opt->{reindex}->{from};
                my $mm = $ibx->mm;
                my $v = PublicInbox::Search::SCHEMA_VERSION();