]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
v2writable: allow disabling parallelization
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index bdde76bbbf54702007d5f27a8c0e05777d67d06c..8e1363ea732895da043cb5080502d65afdbc66b1 100644 (file)
@@ -5,7 +5,7 @@
 package PublicInbox::V2Writable;
 use strict;
 use warnings;
-use Fcntl qw(:flock :DEFAULT);
+use base qw(PublicInbox::Lock);
 use PublicInbox::SearchIdxPart;
 use PublicInbox::SearchIdxSkeleton;
 use PublicInbox::MIME;
@@ -26,13 +26,10 @@ sub nproc () {
 sub new {
        my ($class, $v2ibx, $creat) = @_;
        my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
-       my $lock_path = "$dir/inbox.lock";
        unless (-d $dir) {
                if ($creat) {
                        require File::Path;
                        File::Path::mkpath($dir);
-                       open my $fh, '>>', $lock_path or
-                               die "failed to open $lock_path: $!\n";
                } else {
                        die "$dir does not exist\n";
                }
@@ -59,12 +56,13 @@ sub new {
                xap_rw => undef, # PublicInbox::V2SearchIdx
                xap_ro => undef,
                partitions => $nparts,
+               parallel => 1,
                transact_bytes => 0,
                lock_path => "$dir/inbox.lock",
                # limit each repo to 1GB or so
                rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
        };
-       bless $self, $class
+       bless $self, $class;
 }
 
 # returns undef on duplicate or spam
@@ -96,7 +94,7 @@ sub add {
        my $nparts = $self->{partitions};
        my $part = $num % $nparts;
        my $idx = $self->idx_part($part);
-       $idx->index_raw($len, $msgref, $num, $oid, $mid0);
+       $idx->index_raw($len, $msgref, $num, $oid, $mid0, $mime);
        my $n = $self->{transact_bytes} += $len;
        if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
                $self->checkpoint;
@@ -188,6 +186,8 @@ sub idx_init {
        # frequently activated.
        delete $ibx->{$_} foreach (qw(git mm search));
 
+       $self->lock_acquire;
+
        # first time initialization, first we create the skeleton pipe:
        my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
 
@@ -250,9 +250,11 @@ sub remove {
 
 sub done {
        my ($self) = @_;
+       my $locked = defined $self->{idx_parts};
        my $im = delete $self->{im};
        $im->done if $im; # PublicInbox::Import::done
        $self->searchidx_checkpoint(0);
+       $self->lock_release if $locked;
 }
 
 sub checkpoint {
@@ -279,7 +281,7 @@ sub barrier {
                # Now deal with Xapian
                $skel->barrier_init(scalar(@$parts));
                # each partition needs to issue a barrier command to skel:
-               $_->barrier foreach @$parts;
+               $_->remote_barrier foreach @$parts;
 
                $skel->barrier_wait; # wait for each Xapian partition
 
@@ -399,7 +401,7 @@ sub import_init {
        my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
        $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
        $im->{want_object_info} = 1;
-       $im->{lock_path} = $self->{lock_path};
+       $im->{lock_path} = undef;
        $im->{path_type} = 'v2';
        $self->{im} = $im;
 }