]> Sergey Matveev's repositories - public-inbox.git/commitdiff
v1writable: retire in favor of InboxWritable
authorEric Wong <e@80x24.org>
Thu, 23 May 2019 09:36:40 +0000 (09:36 +0000)
committerEric Wong <e@80x24.org>
Thu, 23 May 2019 17:43:50 +0000 (17:43 +0000)
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
lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/V1Writable.pm [deleted file]
lib/PublicInbox/V2Writable.pm
script/public-inbox-init
t/cgi.t
t/indexlevels-mirror.t
t/init.t
t/nntpd.t
t/v2mirror.t

index 2c356c620bc0f5727195d7a992c1cba7a7a2211a..2b101fa3ccbcefcd6a80a1575b5e4950195d9c50 100644 (file)
--- 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
index 2f1ca6f0ab2460638eef994c495706ad1a9b3286..116f423b74e6f4c5659a756cae7279dbc500b4c2 100644 (file)
@@ -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 (file)
index 6ca5db4..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
-# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
-
-# This interface wraps PublicInbox::Import and makes it closer
-# to V2Writable
-# Used to write to V1 inboxes (see L<public-inbox-v1-format(5)>).
-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;
index afcac4d2db863103f5e92dfd82bbbeb5301279a5..c476cb39992a6581616030fcd7133b26f44d959a 100644 (file)
@@ -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;
index 2cc704ce78f09970a34c80c67d52caf1abfecd46..5724c52ac90e1c46474adb227df9a4e9936a1abb 100755 (executable)
@@ -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 d3172bf74cbd1820e2ae2104ac95c712fd3ae328..81130df5bc418b97f2a1ceff8cf1e7a128e2d348 100644 (file)
--- 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;
index 3dd432382672344bb4458acb0c2ef95c03160fbd..d124c7558ff790e49008e63d02d011e17c543db4 100644 (file)
@@ -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', '<m@1>');
        ok($im->add($mime), 'first message added');
        $im->done;
index 86b4eb5c72e67b74a3474b1aee2030e3d3b71cec..79dcad1b6340d543a3f97c86278d6539a87ccd34 100644 (file)
--- 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();
index c7ea3197c20f3808fc037e5144ee5aad2fd89c5f..aa62ff6036b86648ec9303f20d5cfee07b1fe784 100644 (file)
--- 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
        {
index 441e36d75e16d503dec7f49262fde80ef5755cdf..fe05ec4d2c30916a6a01d424571e944080f7ca0f 100644 (file)
@@ -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