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::SearchIdxThread;
6 use base qw(PublicInbox::SearchIdx);
7 use Storable qw(freeze thaw);
10 my ($class, $v2writable) = @_;
11 my $self = $class->SUPER::new($v2writable->{-inbox}, 1, 'all');
17 pipe($r, $w) or die "pipe failed: $!\n";
21 defined $pid or die "fork failed: $!\n";
23 $v2writable->atfork_child;
26 eval { thread_worker_loop($self, $r) };
27 die "thread worker died: $@\n" if $@;
36 # lock on only exists in parent, not in worker
37 my $l = $self->{lock_path} = $self->xdir . '/thread.lock';
38 open my $fh, '>>', $l or die "failed to create $l: $!\n";
42 sub thread_worker_loop {
45 my $xdb = $self->_xdb_acquire;
46 $xdb->begin_transaction;
48 while (my $line = $r->getline) {
49 if ($line eq "commit\n") {
50 $xdb->commit_transaction if $txn;
52 } elsif ($line eq "close\n") {
56 read($r, $msg, $line) or die "read failed: $!\n";
57 $msg = thaw($msg); # should raise on error
58 defined $msg or die "failed to thaw buffer\n";
60 $xdb->begin_transaction;
63 eval { $self->thread_msg_real(@$msg) };
64 warn "failed to index message <$msg->[0]>: $@\n" if $@;
69 # called by a partition worker
71 my ($self, $mid, $ts, $xpath, $doc_data) = @_;
74 my $str = freeze([ $mid, $ts, $xpath, $doc_data ]);
75 my $len = length($str) . "\n";
77 # multiple processes write to the same pipe, so use flock
79 print $w $len, $str or $err = $!;
82 die "print failed: $err\n" if $err;
86 my ($self, $mid, $ts, $xpath, $doc_data) = @_;
87 my $smsg = $self->lookup_message($mid);
88 my ($old_tid, $doc_id);
90 # convert a ghost to a regular message
91 # it will also clobber any existing regular message
92 $doc_id = $smsg->{doc_id};
93 $old_tid = $smsg->thread_id;
95 $smsg = PublicInbox::SearchMsg->new(undef);
98 my $doc = $smsg->{doc};
99 $doc->add_term('XPATH' . $xpath) if defined $xpath;
100 $doc->add_term('XMID' . $mid);
101 $doc->set_data($doc_data);
103 my @refs = ($smsg->references =~ /<([^>]+)>/g);
104 $self->link_message($smsg, \@refs, $old_tid);
105 my $db = $self->{xdb};
106 if (defined $doc_id) {
107 $db->replace_document($doc_id, $doc);
109 $doc_id = $db->add_document($doc);