From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 2B6F120382 for ; Thu, 23 May 2019 09:37:05 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 02/26] v1writable: retire in favor of InboxWritable Date: Thu, 23 May 2019 09:36:40 +0000 Message-Id: <20190523093704.18367-3-e@80x24.org> In-Reply-To: <20190523093704.18367-1-e@80x24.org> References: <20190523093704.18367-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: In retrospect, introducing V1Writable was unnecessary and InboxWritable->importer is in a better position to abstract away differences between v1 and v2 writers. So teach InboxWritable to initialize inboxes and get rid of V1Writable. --- MANIFEST | 1 - lib/PublicInbox/InboxWritable.pm | 35 ++++++++++++++++++++++++-------- lib/PublicInbox/V1Writable.pm | 34 ------------------------------- lib/PublicInbox/V2Writable.pm | 6 +++--- script/public-inbox-init | 13 +++--------- t/cgi.t | 4 ++-- t/indexlevels-mirror.t | 5 ++--- t/init.t | 4 ++-- t/nntpd.t | 15 +++----------- t/v2mirror.t | 1 + 10 files changed, 43 insertions(+), 75 deletions(-) delete mode 100644 lib/PublicInbox/V1Writable.pm diff --git a/MANIFEST b/MANIFEST index 2c356c6..2b101fa 100644 --- a/MANIFEST +++ b/MANIFEST @@ -126,7 +126,6 @@ lib/PublicInbox/SpawnPP.pm lib/PublicInbox/Syscall.pm lib/PublicInbox/Unsubscribe.pm lib/PublicInbox/UserContent.pm -lib/PublicInbox/V1Writable.pm lib/PublicInbox/V2Writable.pm lib/PublicInbox/View.pm lib/PublicInbox/ViewDiff.pm diff --git a/lib/PublicInbox/InboxWritable.pm b/lib/PublicInbox/InboxWritable.pm index 2f1ca6f..116f423 100644 --- a/lib/PublicInbox/InboxWritable.pm +++ b/lib/PublicInbox/InboxWritable.pm @@ -19,25 +19,44 @@ use constant { }; sub new { - my ($class, $ibx) = @_; - bless $ibx, $class; + my ($class, $ibx, $creat_opt) = @_; + my $self = bless $ibx, $class; + + # TODO: maybe stop supporting this + if ($creat_opt) { # for { nproc => $N } + $self->{-creat_opt} = $creat_opt; + init_inbox($self) if ($self->{version} || 1) == 1; + } + $self; +} + +sub init_inbox { + my ($self, $partitions, $skip_epoch, $skip_artnum) = @_; + # TODO: honor skip_artnum + my $v = $self->{version} || 1; + if ($v == 1) { + my $dir = $self->{mainrepo} or die "no mainrepo in inbox\n"; + PublicInbox::Import::init_bare($dir); + } else { + my $v2w = importer($self); + $v2w->init_inbox($partitions, $skip_epoch, $skip_artnum); + } } sub importer { my ($self, $parallel) = @_; - $self->{-importer} ||= eval { + $self->{-importer} ||= do { my $v = $self->{version} || 1; if ($v == 2) { eval { require PublicInbox::V2Writable }; die "v2 not supported: $@\n" if $@; - my $v2w = PublicInbox::V2Writable->new($self); + my $opt = $self->{-creat_opt}; + my $v2w = PublicInbox::V2Writable->new($self, $opt); $v2w->{parallel} = $parallel; $v2w; } elsif ($v == 1) { - my $git = $self->git; - my $name = $self->{name}; - my $addr = $self->{-primary_address}; - PublicInbox::Import->new($git, $name, $addr, $self); + my @arg = (undef, undef, undef, $self); + PublicInbox::Import->new(@arg); } else { $! = 78; # EX_CONFIG 5.3.5 local configuration error die "unsupported inbox version: $v\n"; diff --git a/lib/PublicInbox/V1Writable.pm b/lib/PublicInbox/V1Writable.pm deleted file mode 100644 index 6ca5db4..0000000 --- a/lib/PublicInbox/V1Writable.pm +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (C) 2019 all contributors -# License: AGPL-3.0+ - -# This interface wraps PublicInbox::Import and makes it closer -# to V2Writable -# Used to write to V1 inboxes (see L). -package PublicInbox::V1Writable; -use strict; -use warnings; -use base qw(PublicInbox::Import); -use PublicInbox::InboxWritable; - -sub new { - my ($class, $ibx, $creat) = @_; - my $dir = $ibx->{mainrepo} or die "no mainrepo in inbox\n"; - unless (-d $dir) { - if ($creat) { - PublicInbox::Import::init_bare($dir); - } else { - die "$dir does not exist\n"; - } - } - $ibx = PublicInbox::InboxWritable->new($ibx); - $class->SUPER::new(undef, undef, undef, $ibx); -} - -sub init_inbox { - my ($self, $partitions, $skip_epoch, $skip_artnum) = @_; - # TODO: honor skip_artnum - my $dir = $self->{-inbox}->{mainrepo} or die "no mainrepo in inbox\n"; - PublicInbox::Import::init_bare($dir); -} - -1; diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index afcac4d..c476cb3 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -94,13 +94,13 @@ sub new { } sub init_inbox { - my ($self, $parallel, $skip) = @_; + my ($self, $parallel, $skip_epoch) = @_; $self->{parallel} = $parallel; $self->idx_init; my $epoch_max = -1; git_dir_latest($self, \$epoch_max); - if (defined $skip && $epoch_max == -1) { - $epoch_max = $skip; + if (defined $skip_epoch && $epoch_max == -1) { + $epoch_max = $skip_epoch; } $self->git_init($epoch_max >= 0 ? $epoch_max : 0); $self->done; diff --git a/script/public-inbox-init b/script/public-inbox-init index 2cc704c..5724c52 100755 --- a/script/public-inbox-init +++ b/script/public-inbox-init @@ -10,7 +10,7 @@ use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/; use PublicInbox::Admin; PublicInbox::Admin::require_or_die('-base'); require PublicInbox::Config; -require PublicInbox::Inbox; +require PublicInbox::InboxWritable; use File::Temp qw/tempfile/; use File::Basename qw/dirname/; use File::Path qw/mkpath/; @@ -116,15 +116,8 @@ my $ibx = PublicInbox::Inbox->new({ indexlevel => $indexlevel, }); -if ($version >= 2) { - require PublicInbox::V2Writable; - PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0, $skip); -} elsif ($version == 1) { - require PublicInbox::V1Writable; - PublicInbox::V1Writable->new($ibx, 1)->init_inbox(0, $skip); -} else { - die "Unsupported -V/--version: $version\n"; -} +my $creat_opt = {}; +PublicInbox::InboxWritable->new($ibx, $creat_opt)->init_inbox(0, $skip); # needed for git prior to v2.1.0 umask(0077) if defined $perm; diff --git a/t/cgi.t b/t/cgi.t index d3172bf..81130df 100644 --- a/t/cgi.t +++ b/t/cgi.t @@ -41,11 +41,11 @@ my $cfgpfx = "publicinbox.test"; use_ok 'PublicInbox::Git'; use_ok 'PublicInbox::Import'; use_ok 'PublicInbox::Inbox'; -use_ok 'PublicInbox::V1Writable'; +use_ok 'PublicInbox::InboxWritable'; use_ok 'PublicInbox::Config'; my $cfg = PublicInbox::Config->new($pi_config); my $ibx = $cfg->lookup_name('test'); -my $im = PublicInbox::V1Writable->new($ibx); +my $im = PublicInbox::InboxWritable->new($ibx)->importer; { local $ENV{HOME} = $home; diff --git a/t/indexlevels-mirror.t b/t/indexlevels-mirror.t index 3dd4323..d124c75 100644 --- a/t/indexlevels-mirror.t +++ b/t/indexlevels-mirror.t @@ -5,6 +5,7 @@ use warnings; use Test::More; use PublicInbox::MIME; use PublicInbox::Inbox; +use PublicInbox::InboxWritable; use File::Temp qw/tempdir/; require './t/common.perl'; require_git(2.6); @@ -38,9 +39,7 @@ sub import_index_incremental { -primary_address => 'test@example.com', indexlevel => $level, }); - my $cls = "PublicInbox::V${v}Writable"; - use_ok $cls; - my $im = $cls->new($ibx, {nproc=>1}); + my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer; $mime->header_set('Message-ID', ''); ok($im->add($mime), 'first message added'); $im->done; diff --git a/t/init.t b/t/init.t index 86b4eb5..79dcad1 100644 --- a/t/init.t +++ b/t/init.t @@ -88,7 +88,7 @@ SKIP: { qw(http://example.com/skip1 skip1@example.com)); is(system(@cmd), 0, "--skip 1"); my $gits = [ glob("$tmpdir/skip1/git/*.git") ]; - is_deeply(["$tmpdir/skip1/git/1.git"], $gits, 'skip OK'); + is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK'); } @@ -96,7 +96,7 @@ SKIP: { qw(http://example.com/skip2 skip2@example.com)); is(system(@cmd), 0, "--skip 2"); my $gits = [ glob("$tmpdir/skip2/git/*.git") ]; - is_deeply(["$tmpdir/skip2/git/2.git"], $gits, 'skipping 2 works, too'); + is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too'); } done_testing(); diff --git a/t/nntpd.t b/t/nntpd.t index c7ea319..aa62ff6 100644 --- a/t/nntpd.t +++ b/t/nntpd.t @@ -9,6 +9,7 @@ foreach my $mod (qw(DBD::SQLite)) { } require PublicInbox::SearchIdx; require PublicInbox::Msgmap; +require PublicInbox::InboxWritable; use Email::Simple; use IO::Socket; use Socket qw(IPPROTO_TCP TCP_NODELAY); @@ -30,9 +31,6 @@ my $group = 'test-nntpd'; my $addr = $group . '@example.com'; my $nntpd = 'blib/script/public-inbox-nntpd'; my $init = 'blib/script/public-inbox-init'; -use_ok 'PublicInbox::Import'; -use_ok 'PublicInbox::Inbox'; -use_ok 'PublicInbox::Git'; SKIP: { skip "git 2.6+ required for V2Writable", 1 if $version == 1; use_ok 'PublicInbox::V2Writable'; @@ -68,15 +66,8 @@ $ibx = PublicInbox::Inbox->new($ibx); 0, 'enabled newsgroup'); my $len; - my $im; - if ($version == 2) { - $im = PublicInbox::V2Writable->new($ibx); - } elsif ($version == 1) { - use_ok 'PublicInbox::V1Writable'; - $im = PublicInbox::V1Writable->new($ibx); - } else { - die "unsupported version: $version"; - } + $ibx = PublicInbox::InboxWritable->new($ibx); + my $im = $ibx->importer; # ensure successful message delivery { diff --git a/t/v2mirror.t b/t/v2mirror.t index 441e36d..fe05ec4 100644 --- a/t/v2mirror.t +++ b/t/v2mirror.t @@ -17,6 +17,7 @@ use File::Temp qw/tempdir/; use IO::Socket; use POSIX qw(dup2); use_ok 'PublicInbox::V2Writable'; +use PublicInbox::InboxWritable; use PublicInbox::MIME; use PublicInbox::Config; # FIXME: too much setup -- EW