+ $doc->add_boolean_term('G' . $tid);
+ $tid;
+}
+
+sub link_and_save {
+ my ($self, $doc, $mids, $refs, $num, $xpath) = @_;
+ my $db = $self->{xdb};
+ my $old_tid;
+ my $doc_id;
+ $doc->add_boolean_term('XNUM' . $num) if defined $num;
+ $doc->add_boolean_term('XPATH' . $xpath) if defined $xpath;
+ $doc->add_boolean_term('Q' . $_) foreach @$mids;
+
+ $self->{skel} and die "Should not have read-only skel here\n";;
+ foreach my $mid (@$mids) {
+ my $vivified = 0;
+ $self->each_smsg_by_mid($mid, sub {
+ my ($cur) = @_;
+ my $type = $cur->type;
+ my $cur_tid = $cur->thread_id;
+ $old_tid = $cur_tid unless defined $old_tid;
+ if ($type eq 'mail') {
+ # do not break existing mail messages,
+ # just merge the threads
+ merge_threads($self, $old_tid, $cur_tid);
+ return 1;
+ }
+ if ($type ne 'ghost') {
+ die "<$mid> has a bad type: $type\n";
+ }
+ my $tid = link_doc($self, $doc, $refs, $old_tid);
+ $old_tid = $tid unless defined $old_tid;
+ $doc_id = $cur->{doc_id};
+ $self->{xdb}->replace_document($doc_id, $doc);
+ ++$vivified;
+ 1;
+ });
+ $vivified > 1 and warn
+ "BUG: vivified multiple ($vivified) ghosts for $mid\n";
+ }
+ # not really important, but we return any vivified ghost docid, here:
+ return $doc_id if defined $doc_id;
+ link_doc($self, $doc, $refs, $old_tid);
+ $self->{xdb}->add_document($doc);