+sub apply_boost ($$) {
+ my ($req, $smsg) = @_;
+ my $id2pos = $req->{id2pos}; # index in ibx_sorted
+ my $xr3 = $req->{self}->{oidx}->get_xref3($smsg->{num}, 1);
+ @$xr3 = sort {
+ $id2pos->{$a->[0]} <=> $id2pos->{$b->[0]}
+ ||
+ $a->[1] <=> $b->[1] # break ties with {xnum}
+ } @$xr3;
+ my $new_smsg = $req->{new_smsg};
+ return if $xr3->[0]->[2] ne pack('H*', $new_smsg->{blob}); # loser
+
+ # replace the old smsg with the more boosted one
+ $new_smsg->{num} = $smsg->{num};
+ $new_smsg->populate($req->{eml}, $req);
+ $req->{self}->{oidx}->add_overview($req->{eml}, $new_smsg);
+}
+
+sub remove_doc ($$) {
+ my ($self, $docid) = @_;
+ $self->{oidx}->delete_by_num($docid);
+ $self->{oidx}->eidxq_del($docid);
+ $self->idx_shard($docid)->ipc_do('xdb_remove', $docid);
+}
+
+sub _unref_doc ($$$$$;$) {
+ my ($sync, $docid, $ibx, $xnum, $oidbin, $eml) = @_;
+ my $smsg;
+ if (ref($docid)) {
+ $smsg = $docid;
+ $docid = $smsg->{num};
+ }
+ my $s = 'DELETE FROM xref3 WHERE oidbin = ?';
+ $s .= ' AND ibx_id = ?' if defined($ibx);
+ $s .= ' AND xnum = ?' if defined($xnum);
+ my $del = $sync->{self}->{oidx}->dbh->prepare_cached($s);
+ my $col = 0;
+ $del->bind_param(++$col, $oidbin, SQL_BLOB);
+ $del->bind_param(++$col, $ibx->{-ibx_id}) if $ibx;
+ $del->bind_param(++$col, $xnum) if defined($xnum);
+ $del->execute;
+ my $xr3 = $sync->{self}->{oidx}->get_xref3($docid);
+ if (scalar(@$xr3) == 0) { # all gone
+ remove_doc($sync->{self}, $docid);
+ } else { # enqueue for reindex of remaining messages
+ if ($ibx) {
+ my $ekey = $ibx->{-gc_eidx_key} // $ibx->eidx_key;
+ my $idx = $sync->{self}->idx_shard($docid);
+ $idx->ipc_do('remove_eidx_info', $docid, $ekey, $eml);
+ } # else: we can't remove_eidx_info in reindex-only path
+
+ # replace invalidated blob ASAP with something which should be
+ # readable since we may commit the transaction on checkpoint.
+ # eidxq processing will re-apply boost
+ $smsg //= $sync->{self}->{oidx}->get_art($docid);
+ my $hex = unpack('H*', $oidbin);
+ if ($smsg && $smsg->{blob} eq $hex) {
+ $xr3->[0] =~ /:([a-f0-9]{40,}+)\z/ or
+ die "BUG: xref $xr3->[0] has no OID";
+ $sync->{self}->{oidx}->update_blob($smsg, $1);
+ }
+ # yes, add, we'll need to re-apply boost
+ $sync->{self}->{oidx}->eidxq_add($docid);
+ }
+ @$xr3
+}
+