1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::SearchIdxPart;
6 use base qw(PublicInbox::SearchIdx);
9 my ($class, $v2writable, $part, $threader) = @_;
10 my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
11 $self->{threader} = $threader;
13 pipe($r, $w) or die "pipe failed: $!\n";
17 defined $pid or die "fork failed: $!\n";
19 $v2writable->atfork_child;
23 # F_SETPIPE_SZ = 1031 on Linux; increasing the pipe size here
24 # speeds V2Writable batch imports across 8 cores by nearly 20%
25 fcntl($r, 1031, 1048576) if $^O eq 'linux';
27 eval { partition_worker_loop($self, $r, $part) };
28 die "worker $part died: $@\n" if $@;
29 die "unexpected MM $self->{mm}" if $self->{mm};
38 sub partition_worker_loop ($$$) {
39 my ($self, $r, $part) = @_;
40 $0 = "pi-v2-partition[$part]";
41 my $xdb = $self->_xdb_acquire;
42 $xdb->begin_transaction;
44 while (my $line = $r->getline) {
45 if ($line eq "commit\n") {
46 $xdb->commit_transaction if $txn;
48 } elsif ($line eq "close\n") {
53 my ($len, $artnum, $object_id) = split(/ /, $line);
54 $xdb ||= $self->_xdb_acquire;
56 $xdb->begin_transaction;
59 my $n = read($r, my $msg, $len) or die "read: $!\n";
60 $n == $len or die "short read: $n != $len\n";
61 my $mime = PublicInbox::MIME->new(\$msg);
62 $artnum = int($artnum);
63 $self->add_message($mime, $n, $artnum, $object_id);
66 warn "$$ still in transaction\n" if $txn;
67 warn "$$ xdb active\n" if $xdb;
70 # called by V2Writable
72 my ($self, $len, $msgref, $artnum, $object_id) = @_;
74 print $w "$len $artnum $object_id\n", $$msgref or die
75 "failed to write partition $!\n";
76 $w->flush or die "failed to flush: $!\n";
80 close $_[0]->{w} or die "failed to close write pipe: $!\n";