]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
v2/ui: get nntpd and init tests running on v2
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index cf19c7610cb775f1cb09d5c985442046996ce52a..81c7a4d16c0189902d87fbc052ac4d26e57b1c04 100644 (file)
@@ -7,12 +7,10 @@ use strict;
 use warnings;
 use Fcntl qw(:flock :DEFAULT);
 use PublicInbox::SearchIdxPart;
-use PublicInbox::SearchIdxThread;
+use PublicInbox::SearchIdxSkeleton;
 use PublicInbox::MIME;
 use PublicInbox::Git;
 use PublicInbox::Import;
-use Email::MIME::ContentType;
-$Email::MIME::ContentType::STRICT_PARAMS = 0;
 
 # an estimate of the post-packed size to the raw uncompressed size
 my $PACKING_FACTOR = 0.4;
@@ -63,7 +61,7 @@ sub add {
        my ($len, $msgref) = @{$im->{last_object}};
 
        $self->idx_init;
-       my $num = $self->{all}->index_mm($mime);
+       my $num = $self->{skel}->index_mm($mime, 1);
        my $nparts = $self->{partitions};
        my $part = $num % $nparts;
        my $idx = $self->idx_part($part);
@@ -81,20 +79,23 @@ sub idx_part {
        $self->{idx_parts}->[$part];
 }
 
+# idempotent
 sub idx_init {
        my ($self) = @_;
        return if $self->{idx_parts};
-       # first time initialization:
-       my $all = $self->{all} =
-               PublicInbox::SearchIdxThread->new($self->{-inbox});
+
+       # first time initialization, first we create the skeleton pipe:
+       my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
 
        # need to create all parts before initializing msgmap FD
        my $max = $self->{partitions} - 1;
        my $idx = $self->{idx_parts} = [];
        for my $i (0..$max) {
-               push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
+               push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $skel);
        }
-       $all->_msgmap_init->{dbh}->begin_work;
+
+       # Now that all subprocesses are up, we can open the FD for SQLite:
+       $skel->_msgmap_init->{dbh}->begin_work;
 }
 
 sub remove {
@@ -129,25 +130,26 @@ sub checkpoint {
 sub searchidx_checkpoint {
        my ($self, $more) = @_;
 
-       # order matters, we can only close {all} after all partitions
-       # are done because the partitions also write to {all}
-
+       # order matters, we can only close {skel} after all partitions
+       # are done because the partitions also write to {skel}
        if (my $parts = $self->{idx_parts}) {
                foreach my $idx (@$parts) {
-                       $idx->remote_commit;
+                       $idx->remote_commit; # propagates commit to skel
                        $idx->remote_close unless $more;
                }
                delete $self->{idx_parts} unless $more;
        }
 
-       if (my $all = $self->{all}) {
-               $all->{mm}->{dbh}->commit;
+       if (my $skel = $self->{skel}) {
+               my $dbh = $skel->{mm}->{dbh};
+               $dbh->commit;
                if ($more) {
-                       $all->{mm}->{dbh}->begin_work;
+                       $dbh->begin_work;
+               } else {
+                       $skel->remote_commit; # XXX should be unnecessary...
+                       $skel->remote_close;
+                       delete $self->{skel};
                }
-               $all->remote_commit;
-               $all->remote_close unless $more;
-               delete $self->{all} unless $more;
        }
        $self->{transact_bytes} = 0;
 }
@@ -242,4 +244,14 @@ sub lookup_content {
        undef # TODO
 }
 
+sub atfork_child {
+       my ($self) = @_;
+       if (my $parts = $self->{idx_parts}) {
+               $_->atfork_child foreach @$parts;
+       }
+       if (my $im = $self->{im}) {
+               $im->atfork_child;
+       }
+}
+
 1;