]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
import: enable locking under v2
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 656f0693a261f7dd92800557fd2f9c1ae30e2409..bdde76bbbf54702007d5f27a8c0e05777d67d06c 100644 (file)
@@ -26,10 +26,13 @@ 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";
                }
@@ -57,6 +60,7 @@ sub new {
                xap_ro => undef,
                partitions => $nparts,
                transact_bytes => 0,
+               lock_path => "$dir/inbox.lock",
                # limit each repo to 1GB or so
                rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
        };
@@ -146,19 +150,19 @@ sub num_for_harder {
 
        my $hdr = $mime->header_obj;
        my $dig = content_digest($mime);
-       $$mid0 = $dig->clone->hexdigest . '@localhost';
+       $$mid0 = PublicInbox::Import::digest2mid($dig);
        my $num = $self->{skel}->{mm}->mid_insert($$mid0);
        unless (defined $num) {
                # it's hard to spoof the last Received: header
                my @recvd = $hdr->header_raw('Received');
                $dig->add("Received: $_") foreach (@recvd);
-               $$mid0 = $dig->clone->hexdigest . '@localhost';
+               $$mid0 = PublicInbox::Import::digest2mid($dig);
                $num = $self->{skel}->{mm}->mid_insert($$mid0);
 
                # fall back to a random Message-ID and give up determinism:
                until (defined($num)) {
                        $dig->add(rand);
-                       $$mid0 = $dig->clone->hexdigest . '@localhost';
+                       $$mid0 = PublicInbox::Import::digest2mid($dig);
                        warn "using random Message-ID <$$mid0> as fallback\n";
                        $num = $self->{skel}->{mm}->mid_insert($$mid0);
                }
@@ -220,6 +224,7 @@ sub remove {
                                warn "broken smsg for $mid\n";
                                return 1; # continue
                        }
+                       my $orig = $$msg;
                        my $cur = PublicInbox::MIME->new($msg);
                        if (content_id($cur) eq $cid) {
                                $mm->num_delete($smsg->num);
@@ -227,7 +232,8 @@ sub remove {
                                # no bugs in our deduplication code:
                                $removed = $smsg;
                                $removed->{mime} = $cur;
-                               $im->remove($cur, $cmt_msg);
+                               $im->remove(\$orig, $cmt_msg);
+                               $orig = undef;
                                $removed->num; # memoize this for callers
 
                                my $oid = $smsg->{blob};
@@ -261,13 +267,8 @@ sub checkpoint {
 sub barrier {
        my ($self) = @_;
 
-       # For safety, we ensure git checkpoint is complete before because
-       # the data in git is still more important than what is in Xapian.
-       # Performance may be gained by delaying ->progress call but we
-       # lose safety
        if (my $im = $self->{im}) {
-               $im->checkpoint;
-               $im->progress('checkpoint');
+               $im->barrier;
        }
        my $skel = $self->{skel};
        my $parts = $self->{idx_parts};
@@ -398,7 +399,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->{ssoma_lock} = 0;
+       $im->{lock_path} = $self->{lock_path};
        $im->{path_type} = 'v2';
        $self->{im} = $im;
 }