]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
Merge remote-tracking branch 'origin/danga-bundle'
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 0a304aae241a6b390592b30b3f21ddfeb608de9b..87e8f3eb581a97076b5fd22ea5af917bb623306e 100644 (file)
@@ -2,6 +2,7 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # This interface wraps and mimics PublicInbox::Import
+# Used to write to V2 inboxes (see L<public-inbox-v2-format(5)>).
 package PublicInbox::V2Writable;
 use strict;
 use warnings;
@@ -71,6 +72,7 @@ sub new {
                im => undef, #  PublicInbox::Import
                parallel => 1,
                transact_bytes => 0,
+               current_info => '',
                xpfx => $xpfx,
                over => PublicInbox::OverIdx->new("$xpfx/over.sqlite3", 1),
                lock_path => "$dir/inbox.lock",
@@ -243,6 +245,9 @@ sub idx_init {
 
        if ($self->{parallel}) {
                pipe(my ($r, $w)) or die "pipe failed: $!";
+               # pipe for barrier notifications doesn't need to be big,
+               # 1031: F_SETPIPE_SZ
+               fcntl($w, 1031, 4096) if $^O eq 'linux';
                $self->{bnote} = [ $r, $w ];
                $w->autoflush(1);
        }
@@ -281,10 +286,19 @@ sub purge_oids {
        $self->done;
        my $pfx = "$self->{-inbox}->{mainrepo}/git";
        my $purges = [];
-       foreach my $i (0..$self->{epoch_max}) {
-               my $git = PublicInbox::Git->new("$pfx/$i.git");
+       my $max = $self->{epoch_max};
+
+       unless (defined($max)) {
+               defined(my $latest = git_dir_latest($self, \$max)) or return;
+               $self->{epoch_max} = $max;
+       }
+       foreach my $i (0..$max) {
+               my $git_dir = "$pfx/$i.git";
+               -d $git_dir or next;
+               my $git = PublicInbox::Git->new($git_dir);
                my $im = $self->import_init($git, 0, 1);
                $purges->[$i] = $im->purge_oids($purge);
+               $im->done;
        }
        $purges;
 }
@@ -386,7 +400,7 @@ sub purge {
        my ($self, $mime) = @_;
        my $purges = $self->{-inbox}->with_umask(sub {
                remove_internal($self, $mime, undef, {});
-       });
+       }) or return;
        $self->idx_init if @$purges; # ->done is called on purges
        for my $i (0..$#$purges) {
                defined(my $cmt = $purges->[$i]) or next;
@@ -493,16 +507,15 @@ sub done {
        delete $self->{bnote};
        $self->{transact_bytes} = 0;
        $self->lock_release if $parts;
+       $self->{-inbox}->git->cleanup;
 }
 
-sub git_init {
+sub fill_alternates ($$) {
        my ($self, $epoch) = @_;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
-       my $git_dir = "$pfx/$epoch.git";
-       my @cmd = (qw(git init --bare -q), $git_dir);
-       PublicInbox::Import::run_die(\@cmd);
 
+       my $pfx = "$self->{-inbox}->{mainrepo}/git";
        my $all = "$self->{-inbox}->{mainrepo}/all.git";
+       my @cmd;
        unless (-d $all) {
                @cmd = (qw(git init --bare -q), $all);
                PublicInbox::Import::run_die(\@cmd);
@@ -510,8 +523,7 @@ sub git_init {
                                'repack.writeBitmaps', 'true');
                PublicInbox::Import::run_die(\@cmd);
        }
-
-       @cmd = (qw/git config/, "--file=$git_dir/config",
+       @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
                        'include.path', '../../all.git/config');
        PublicInbox::Import::run_die(\@cmd);
 
@@ -526,12 +538,20 @@ sub git_init {
                my $dir = "../../git/$i.git/objects";
                push @add, $dir if !$alts{$dir} && -d "$pfx/$i.git";
        }
-       return $git_dir unless @add;
+       return unless @add;
        open my $fh, '>>', $alt or die "open >> $alt: $!\n";
        foreach my $dir (@add) {
                print $fh "$dir\n" or die "print >> $alt: $!\n";
        }
        close $fh or die "close $alt: $!\n";
+}
+
+sub git_init {
+       my ($self, $epoch) = @_;
+       my $git_dir = "$self->{-inbox}->{mainrepo}/git/$epoch.git";
+       my @cmd = (qw(git init --bare -q), $git_dir);
+       PublicInbox::Import::run_die(\@cmd);
+       fill_alternates($self, $epoch);
        $git_dir
 }
 
@@ -574,7 +594,9 @@ sub importer {
        if (defined $latest) {
                my $git = PublicInbox::Git->new($latest);
                my $packed_bytes = $git->packed_bytes;
-               if ($packed_bytes >= $self->{rotate_bytes}) {
+               my $unpacked_bytes = $packed_bytes / $PACKING_FACTOR;
+
+               if ($unpacked_bytes >= $self->{rotate_bytes}) {
                        $epoch = $max + 1;
                } else {
                        $self->{epoch_max} = $max;
@@ -920,6 +942,7 @@ sub index_sync {
                my $git_dir = git_dir_n($self, $i);
                die "already reindexing!\n" if delete $self->{reindex_pipe};
                -d $git_dir or next; # missing parts are fine
+               fill_alternates($self, $i);
                my $git = PublicInbox::Git->new($git_dir);
                my $unindex = delete $self->{"unindex-range.$i"};
                $self->unindex($opts, $git, $unindex) if $unindex;
@@ -927,8 +950,10 @@ sub index_sync {
                my $fh = $self->{reindex_pipe} = $git->popen(@cmd, $range);
                my $cmt;
                while (<$fh>) {
+                       chomp;
+                       $self->{current_info} = "$i.git $_";
                        if (/\A$x40$/o && !defined($cmt)) {
-                               chomp($cmt = $_);
+                               $cmt = $_;
                        } elsif (/\A:\d{6} 100644 $x40 ($x40) [AM]\tm$/o) {
                                $self->reindex_oid($mm_tmp, $D, $git, $1,
                                                $regen, $reindex);