about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-01-26 01:17:42 +0000
committerEric Wong <e@yhbt.net>2020-01-27 02:58:06 +0000
commit227a1d886672767e37cc86a3432952c14eb8a143 (patch)
treedd4890655f88583a16952c751cf9f4e9e731d224
parentfcfa0d639da338c470f07942da71f78fa65354a1 (diff)
downloadpublic-inbox-227a1d886672767e37cc86a3432952c14eb8a143.tar.gz
inbox: add ->version method
This allows us to simplify version checking by avoiding
"//" or "||" operators sprinkled around.
-rw-r--r--lib/PublicInbox/Admin.pm5
-rw-r--r--lib/PublicInbox/AltId.pm2
-rw-r--r--lib/PublicInbox/Feed.pm2
-rw-r--r--lib/PublicInbox/Inbox.pm11
-rw-r--r--lib/PublicInbox/InboxWritable.pm9
-rw-r--r--lib/PublicInbox/Search.pm2
-rw-r--r--lib/PublicInbox/SearchIdx.pm2
-rw-r--r--lib/PublicInbox/Xapcmd.pm5
-rwxr-xr-xscript/public-inbox-convert2
-rwxr-xr-xscripts/import_slrnspool2
10 files changed, 20 insertions, 22 deletions
diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm
index 1f1b133d..2d3e0281 100644
--- a/lib/PublicInbox/Admin.pm
+++ b/lib/PublicInbox/Admin.pm
@@ -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 {
diff --git a/lib/PublicInbox/AltId.pm b/lib/PublicInbox/AltId.pm
index 6b03d603..5add1ea2 100644
--- a/lib/PublicInbox/AltId.pm
+++ b/lib/PublicInbox/AltId.pm
@@ -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";
diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index cbf25d46..0bd458c9 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -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";
         }
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index 07e8b5b7..b76d4e5a 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -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);
diff --git a/lib/PublicInbox/InboxWritable.pm b/lib/PublicInbox/InboxWritable.pm
index 228e786c..5b2aeed3 100644
--- a/lib/PublicInbox/InboxWritable.pm
+++ b/lib/PublicInbox/InboxWritable.pm
@@ -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;
                 }
 
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 5c9dccb5..5e820594 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -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");
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index cb554912..4e951bbe 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -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) {
diff --git a/lib/PublicInbox/Xapcmd.pm b/lib/PublicInbox/Xapcmd.pm
index de2ef5c6..19c6ff07 100644
--- a/lib/PublicInbox/Xapcmd.pm
+++ b/lib/PublicInbox/Xapcmd.pm
@@ -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";
diff --git a/script/public-inbox-convert b/script/public-inbox-convert
index 633c4cf8..56a810eb 100755
--- a/script/public-inbox-convert
+++ b/script/public-inbox-convert
@@ -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 };
diff --git a/scripts/import_slrnspool b/scripts/import_slrnspool
index 1dccb8dd..b913cf32 100755
--- a/scripts/import_slrnspool
+++ b/scripts/import_slrnspool
@@ -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