user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 1/3] inbox: add ->version method
Date: Sun, 26 Jan 2020 01:17:42 +0000
Message-ID: <20200126011744.6278-2-e@yhbt.net> (raw)
In-Reply-To: <20200126011744.6278-1-e@yhbt.net>

This allows us to simplify version checking by avoiding
"//" or "||" operators sprinkled around.
---
 lib/PublicInbox/Admin.pm         |  5 ++---
 lib/PublicInbox/AltId.pm         |  2 +-
 lib/PublicInbox/Feed.pm          |  2 +-
 lib/PublicInbox/Inbox.pm         | 11 ++++++-----
 lib/PublicInbox/InboxWritable.pm |  9 ++++-----
 lib/PublicInbox/Search.pm        |  2 +-
 lib/PublicInbox/SearchIdx.pm     |  2 +-
 lib/PublicInbox/Xapcmd.pm        |  5 ++---
 script/public-inbox-convert      |  2 +-
 scripts/import_slrnspool         |  2 +-
 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

  reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-26  1:17 [PATCH 0/3] writing/admin code cleanups lock fix Eric Wong
2020-01-26  1:17 ` Eric Wong [this message]
2020-01-26  1:17 ` [PATCH 2/3] search: {version} => {ibx_ver} Eric Wong
2020-01-26  1:17 ` [PATCH 3/3] xapcmd: increase scope of lock Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200126011744.6278-2-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git