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, $v2ibx) = @_;
11 my $self = $class->SUPER::new($v2ibx, 1, 'all');
17 pipe($r, $w) or die "pipe failed: $!\n";
21 defined $pid or die "fork failed: $!\n";
24 eval { thread_worker_loop($self, $r) };
25 die "thread worker died: $@\n" if $@;
34 # lock on only exists in parent, not in worker
35 my $l = $self->{lock_path} = $self->xdir . '/thread.lock';
36 open my $fh, '>>', $l or die "failed to create $l: $!\n";
40 sub thread_worker_loop {
43 my $xdb = $self->_xdb_acquire;
44 $xdb->begin_transaction;
46 while (my $line = $r->getline) {
47 if ($line eq "commit\n") {
48 $xdb->commit_transaction if $txn;
50 } elsif ($line eq "close\n") {
54 read($r, $msg, $line) or die "read failed: $!\n";
55 $msg = thaw($msg); # should raise on error
56 defined $msg or die "failed to thaw buffer\n";
58 $xdb->begin_transaction;
61 eval { $self->thread_msg_real(@$msg) };
62 warn "failed to index message <$msg->[0]>: $@\n" if $@;
67 # called by a partition worker
69 my ($self, $mid, $ts, $xpath, $doc_data) = @_;
72 my $str = freeze([ $mid, $ts, $xpath, $doc_data ]);
73 my $len = length($str) . "\n";
75 # multiple processes write to the same pipe, so use flock
77 print $w $len, $str or $err = $!;
80 die "print failed: $err\n" if $err;
84 my ($self, $mid, $ts, $xpath, $doc_data) = @_;
85 my $smsg = $self->lookup_message($mid);
86 my ($old_tid, $doc_id);
88 # convert a ghost to a regular message
89 # it will also clobber any existing regular message
90 $doc_id = $smsg->{doc_id};
91 $old_tid = $smsg->thread_id;
93 $smsg = PublicInbox::SearchMsg->new(undef);
96 my $doc = $smsg->{doc};
97 $doc->add_term('XPATH' . $xpath) if defined $xpath;
98 $doc->add_term('XMID' . $mid);
99 $doc->set_data($doc_data);
101 my @refs = ($smsg->references =~ /<([^>]+)>/g);
102 $self->link_message($smsg, \@refs, $old_tid);
103 my $db = $self->{xdb};
104 if (defined $doc_id) {
105 $db->replace_document($doc_id, $doc);
107 $doc_id = $db->add_document($doc);