]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchIdxPart.pm
v2: parallelize Xapian indexing
[public-inbox.git] / lib / PublicInbox / SearchIdxPart.pm
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;
4 use strict;
5 use warnings;
6 use base qw(PublicInbox::SearchIdx);
7
8 sub new {
9         my ($class, $v2writable, $part, $threader) = @_;
10         my $self = $class->SUPER::new($v2writable->{-inbox}, 1, $part);
11         $self->{threader} = $threader;
12         my ($r, $w);
13         pipe($r, $w) or die "pipe failed: $!\n";
14         my $pid = fork;
15         defined $pid or die "fork failed: $!\n";
16         if ($pid == 0) {
17                 foreach my $other (@{$v2writable->{idx_parts}}) {
18                         my $other_w = $other->{w} or next;
19                         close $other_w or die "close other failed: $!\n";
20                 }
21                 $v2writable = undef;
22                 close $w;
23                 eval { partition_worker_loop($self, $r) };
24                 die "worker $part died: $@\n" if $@;
25                 die "unexpected MM $self->{mm}" if $self->{mm};
26                 exit;
27         }
28         $self->{pid} = $pid;
29         $self->{w} = $w;
30         close $r;
31         $self;
32 }
33
34 sub partition_worker_loop ($$) {
35         my ($self, $r) = @_;
36         my $xdb = $self->_xdb_acquire;
37         $xdb->begin_transaction;
38         my $txn = 1;
39         while (my $line = $r->getline) {
40                 if ($line eq "commit\n") {
41                         $xdb->commit_transaction if $txn;
42                         $txn = undef;
43                 } elsif ($line eq "close\n") {
44                         $self->_xdb_release;
45                         $xdb = $txn = undef;
46                 } else {
47                         my ($len, $artnum, $object_id) = split(/ /, $line);
48                         $xdb ||= $self->_xdb_acquire;
49                         if (!$txn) {
50                                 $xdb->begin_transaction;
51                                 $txn = 1;
52                         }
53                         my $n = read($r, my $msg, $len) or die "read: $!\n";
54                         $n == $len or die "short read: $n != $len\n";
55                         my $mime = PublicInbox::MIME->new(\$msg);
56                         $self->index_blob($mime, $len, $artnum, $object_id);
57                 }
58         }
59         warn "$$ still in transaction\n" if $txn;
60         warn "$$ xdb active\n" if $xdb;
61 }
62
63 # called by V2Writable
64 sub index_raw {
65         my ($self, $len, $msgref, $artnum, $object_id) = @_;
66         print { $self->{w} } "$len $artnum $object_id\n", $$msgref or die
67                 "failed to write partition $!\n";
68 }
69
70 1;