]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
searchidxpart: s/barrier/remote_barrier/
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index e673c2522db79c31104d03c389130c5fa7b321db..261f9d91a0c7e7849eb21d22b6d48c6ffaf5b4ed 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;
@@ -57,10 +57,11 @@ 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),
        };
-       bless $self, $class
+       bless $self, $class;
 }
 
 # returns undef on duplicate or spam
@@ -146,19 +147,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);
                }
@@ -184,6 +185,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);
 
@@ -199,25 +202,58 @@ sub idx_init {
 }
 
 sub remove {
-       my ($self, $mime, $msg) = @_;
-       my $existing = $self->lookup_content($mime) or return;
-
-       # don't touch ghosts or already junked messages
-       return unless $existing->type eq 'mail';
-
-       # always write removals to the current (latest) git repo since
-       # we process chronologically
+       my ($self, $mime, $cmt_msg) = @_;
+       $self->barrier;
+       $self->idx_init;
        my $im = $self->importer;
-       my ($cmt, undef) = $im->remove($mime, $msg);
-       $cmt = $im->get_mark($cmt);
-       $self->unindex_msg($existing, $cmt);
+       my $ibx = $self->{-inbox};
+       my $srch = $ibx->search;
+       my $cid = content_id($mime);
+       my $skel = $self->{skel};
+       my $parts = $self->{idx_parts};
+       my $mm = $skel->{mm};
+       my $removed;
+       my $mids = mids($mime->header_obj);
+       foreach my $mid (@$mids) {
+               $srch->reopen->each_smsg_by_mid($mid, sub {
+                       my ($smsg) = @_;
+                       $smsg->load_expand;
+                       my $msg = $ibx->msg_by_smsg($smsg);
+                       if (!defined($msg)) {
+                               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);
+                               # $removed should only be set once assuming
+                               # no bugs in our deduplication code:
+                               $removed = $smsg;
+                               $removed->{mime} = $cur;
+                               $im->remove(\$orig, $cmt_msg);
+                               $orig = undef;
+                               $removed->num; # memoize this for callers
+
+                               my $oid = $smsg->{blob};
+                               foreach my $idx (@$parts, $skel) {
+                                       $idx->remote_remove($oid, $mid);
+                               }
+                       }
+                       1; # continue
+               });
+               $self->barrier;
+       }
+       $removed;
 }
 
 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 {
@@ -232,13 +268,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};
@@ -249,7 +280,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
 
@@ -369,7 +400,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} = undef;
        $im->{path_type} = 'v2';
        $self->{im} = $im;
 }