]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inbox: add ->version method
authorEric Wong <e@yhbt.net>
Sun, 26 Jan 2020 01:17:42 +0000 (01:17 +0000)
committerEric Wong <e@yhbt.net>
Mon, 27 Jan 2020 02:58:06 +0000 (02:58 +0000)
This allows us to simplify version checking by avoiding
"//" or "||" operators sprinkled around.

lib/PublicInbox/Admin.pm
lib/PublicInbox/AltId.pm
lib/PublicInbox/Feed.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/Search.pm
lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/Xapcmd.pm
script/public-inbox-convert
scripts/import_slrnspool

index 1f1b133d40b4379f27fcdc8bdc28bc845b5e6c9f..2d3e0281f4d71dac716b5720b255f60577075a61 100644 (file)
@@ -84,7 +84,6 @@ sub resolve_inboxes ($;$$) {
        if ($cfg) {
                $cfg->each_inbox(sub {
                        my ($ibx) = @_;
-                       $ibx->{version} ||= 1;
                        my $path = abs_path($ibx->{inboxdir});
                        if (defined($path)) {
                                $dir2ibx{$path} = $ibx;
@@ -97,7 +96,7 @@ EOF
        }
        if ($opt->{all}) {
                my @all = values %dir2ibx;
-               @all = grep { $_->{version} >= $min_ver } @all;
+               @all = grep { $_->version >= $min_ver } @all;
                push @ibxs, @all;
        } else { # directories specified on the command-line
                my $i = 0;
@@ -189,7 +188,7 @@ invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
 sub index_inbox {
        my ($ibx, $im, $opt) = @_;
        my $jobs = delete $opt->{jobs} if $opt;
-       if (ref($ibx) && ($ibx->{version} || 1) == 2) {
+       if (ref($ibx) && $ibx->version == 2) {
                eval { require PublicInbox::V2Writable };
                die "v2 requirements not met: $@\n" if $@;
                my $v2w = $im // eval { $ibx->importer(0) } || eval {
index 6b03d603e78efdb3caf61bd1baeb69911eb75238..5add1ea255dd198440e47ad7e28c07aaaa9bb098 100644 (file)
@@ -30,7 +30,7 @@ sub new {
        } split(/[&;]/, $query);
        my $f = $params{file} or die "file: required for $type spec $spec\n";
        unless (index($f, '/') == 0) {
-               if (($ibx->{version} || 1) == 1) {
+               if ($ibx->version == 1) {
                        $f = "$ibx->{inboxdir}/public-inbox/$f";
                } else {
                        $f = "$ibx->{inboxdir}/$f";
index cbf25d46cf9359052b926ca69a9277a6787085d7..0bd458c94e2bdd05564f386136d0ddacbfaca83f 100644 (file)
@@ -85,7 +85,7 @@ sub recent_msgs {
        my $ibx = $ctx->{-inbox};
        my $max = $ibx->{feedmax};
        my $qp = $ctx->{qp};
-       my $v = $ibx->{version} || 1;
+       my $v = $ibx->version;
        if ($v > 2) {
                die "BUG: unsupported inbox version: $v\n";
        }
index 07e8b5b754071de5c8264d9e95034469e45b1128..b76d4e5abb017383f831af4a97c73903e7cfa80a 100644 (file)
@@ -125,9 +125,11 @@ sub new {
        bless $opts, $class;
 }
 
+sub version { $_[0]->{version} // 1 }
+
 sub git_epoch {
        my ($self, $epoch) = @_;
-       ($self->{version} || 1) == 2 or return;
+       $self->version == 2 or return;
        $self->{"$epoch.git"} ||= eval {
                my $git_dir = "$self->{inboxdir}/git/$epoch.git";
                my $g = PublicInbox::Git->new($git_dir);
@@ -141,7 +143,7 @@ sub git {
        my ($self) = @_;
        $self->{git} ||= eval {
                my $git_dir = $self->{inboxdir};
-               $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
+               $git_dir .= '/all.git' if $self->version == 2;
                my $g = PublicInbox::Git->new($git_dir);
                $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
                _cleanup_later($self);
@@ -151,8 +153,7 @@ sub git {
 
 sub max_git_epoch {
        my ($self) = @_;
-       my $v = $self->{version};
-       return unless defined($v) && $v == 2;
+       return if $self->version < 2;
        my $cur = $self->{-max_git_epoch};
        my $changed = git($self)->alternates_changed;
        if (!defined($cur) || $changed) {
@@ -178,7 +179,7 @@ sub mm {
                require PublicInbox::Msgmap;
                _cleanup_later($self);
                my $dir = $self->{inboxdir};
-               if (($self->{version} || 1) >= 2) {
+               if ($self->version >= 2) {
                        PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
                } else {
                        PublicInbox::Msgmap->new($dir);
index 228e786ce7ecc07946cb74769a615d608e7b72ff..5b2aeed34a1647fbf408800f7e34e12f87f47661 100644 (file)
@@ -24,7 +24,7 @@ sub new {
        # TODO: maybe stop supporting this
        if ($creat_opt) { # for { nproc => $N }
                $self->{-creat_opt} = $creat_opt;
-               init_inbox($self) if ($self->{version} || 1) == 1;
+               init_inbox($self) if $self->version == 1;
        }
        $self;
 }
@@ -39,8 +39,7 @@ sub assert_usable_dir {
 sub init_inbox {
        my ($self, $shards, $skip_epoch, $skip_artnum) = @_;
        # TODO: honor skip_artnum
-       my $v = $self->{version} || 1;
-       if ($v == 1) {
+       if ($self->version == 1) {
                my $dir = assert_usable_dir($self);
                PublicInbox::Import::init_bare($dir);
        } else {
@@ -51,7 +50,7 @@ sub init_inbox {
 
 sub importer {
        my ($self, $parallel) = @_;
-       my $v = $self->{version} || 1;
+       my $v = $self->version;
        if ($v == 2) {
                eval { require PublicInbox::V2Writable };
                die "v2 not supported: $@\n" if $@;
@@ -75,7 +74,7 @@ sub filter {
                # v2 keeps msgmap open, which causes conflicts for filters
                # such as PublicInbox::Filter::RubyLang which overload msgmap
                # for a predictable serial number.
-               if ($im && ($self->{version} || 1) >= 2 && $self->{altid}) {
+               if ($im && $self->version >= 2 && $self->{altid}) {
                        $im->done;
                }
 
index 5c9dccb53af327282d10df960f7f112801986e58..5e820594c3777b322b2a36997cb08d50c95e9403 100644 (file)
@@ -198,7 +198,7 @@ sub new {
        my $self = bless {
                inboxdir => $ibx->{inboxdir},
                altid => $ibx->{altid},
-               version => $ibx->{version} // 1,
+               version => $ibx->version,
        }, $class;
        my $dir = xdir($self, 1);
        $self->{over_ro} = PublicInbox::Over->new("$dir/over.sqlite3");
index cb5549125c4c0fdc0f5ab2b50e55e081ecf692e2..4e951bbedc7449d899a683b8ce6f965f3d291e5e 100644 (file)
@@ -34,7 +34,7 @@ sub new {
        ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
        my $levels = qr/\A(?:full|medium|basic)\z/;
        my $inboxdir = $ibx->{inboxdir};
-       my $version = $ibx->{version} || 1;
+       my $version = $ibx->version;
        my $indexlevel = 'full';
        my $altid = $ibx->{altid};
        if ($altid) {
index de2ef5c6ad814dae358611f97ef6fe9ba955c9fb..19c6ff077d21911f003888edd2cc74a0b99f16e5 100644 (file)
@@ -97,7 +97,7 @@ sub runnable_or_die ($) {
 
 sub prepare_reindex ($$$) {
        my ($ibx, $im, $reindex) = @_;
-       if ($ibx->{version} == 1) {
+       if ($ibx->version == 1) {
                my $dir = $ibx->search->xdir(1);
                my $xdb = $PublicInbox::Search::X{Database}->new($dir);
                if (my $lc = $xdb->get_metadata('last_commit')) {
@@ -173,7 +173,6 @@ sub run {
        -d $old or die "$old does not exist\n";
 
        my $tmp = {};
-       my $v = $ibx->{version} ||= 1;
        my @q;
        my $reshard = $opt->{reshard};
        if (defined $reshard && $reshard <= 0) {
@@ -185,7 +184,7 @@ sub run {
 
        # we want temporary directories to be as deep as possible,
        # so v2 shards can keep "xap$SCHEMA_VERSION" on a separate FS.
-       if ($v == 1) {
+       if ($ibx->version == 1) {
                if (defined $reshard) {
                        warn
 "--reshard=$reshard ignored for v1 $ibx->{inboxdir}\n";
index 633c4cf8d9906e9b760e5feba1a24b0d9d66be49..56a810eb054ffc1ea41585eab1052dabb0b7bc12 100755 (executable)
@@ -41,7 +41,7 @@ unless ($old) {
        $old = PublicInbox::Inbox->new($old);
 }
 $old = PublicInbox::InboxWritable->new($old);
-if (($old->{version} || 1) >= 2) {
+if ($old->version >= 2) {
        die "Only conversion from v1 inboxes is supported\n";
 }
 my $new = { %$old };
index 1dccb8dd51c10793f06eca441eaf02156e15f427..b913cf3212d17de382ce91ba761b0c9025a58529 100755 (executable)
@@ -26,7 +26,7 @@ my $config = PublicInbox::Config->new;
 my $ibx = $config->lookup($recipient);
 my $git = $ibx->git;
 my $im;
-if (($ibx->{version} || 1) == 2) {
+if ($ibx->version == 2) {
        require PublicInbox::V2Writable;
        $im = PublicInbox::V2Writable->new($ibx);
        $im->{parallel} = 0; # pointless to be parallel for a single message