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;
# 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.
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;
# 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;
# 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}/$_";
}
$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;
}
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";
$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();