- last if $sync->{quit};
- }
-}
-
-sub _reindex_check_stale ($$$) {
- my ($self, $sync, $ibx) = @_;
- my $min = 0;
- my $pr = $sync->{-opt}->{-progress};
- my $fetching;
- my $ekey = $ibx->eidx_key;
- local $sync->{-regen_fmt} =
- "$ekey checking stale/missing %u/".$ibx->over->max."\n";
- ${$sync->{nr}} = 0;
- do {
- if (checkpoint_due($sync)) {
- reindex_checkpoint($self, $sync); # release lock
- }
- # now, check if there's stale xrefs
- my $iter = $self->{oidx}->dbh->prepare_cached(<<'', undef, 1);
-SELECT docid,xnum,oidbin FROM xref3 WHERE ibx_id = ? AND docid > ?
-ORDER BY docid,xnum ASC LIMIT 10000
-
- $iter->execute($ibx->{-ibx_id}, $min);
- $fetching = undef;
-
- while (my ($docid, $xnum, $oidbin) = $iter->fetchrow_array) {
- return if $sync->{quit};
- ${$sync->{nr}} = $xnum;
-
- $fetching = $min = $docid;
- my $smsg = $ibx->over->get_art($xnum);
- my $err;
- if (!$smsg) {
- $err = 'stale';
- } elsif (pack('H*', $smsg->{blob}) ne $oidbin) {
- $err = "mismatch (!= $smsg->{blob})";
- } else {
- next; # likely, all good
- }
- # current_info already has eidx_key
- my $oidhex = unpack('H*', $oidbin);
- warn "$xnum:$oidhex (#$docid): $err\n";
- my $del = $self->{oidx}->dbh->prepare_cached(<<'');
-DELETE FROM xref3 WHERE ibx_id = ? AND xnum = ? AND oidbin = ?
-
- $del->bind_param(1, $ibx->{-ibx_id});
- $del->bind_param(2, $xnum);
- $del->bind_param(3, $oidbin, SQL_BLOB);
- $del->execute;
-
- # get_xref3 over-fetches, but this is a rare path:
- my $xr3 = $self->{oidx}->get_xref3($docid);
- my $idx = $self->idx_shard($docid);
- if (scalar(@$xr3) == 0) { # all gone
- $self->{oidx}->delete_by_num($docid);
- $self->{oidx}->eidxq_del($docid);
- $idx->ipc_do('xdb_remove', $docid);
- } else { # enqueue for reindex of remaining messages
- $idx->ipc_do('remove_eidx_info',
- $docid, $ibx->eidx_key);
- $self->{oidx}->eidxq_add($docid); # yes, add
+ return if $sync->{quit};
+ next unless scalar keys %x3m;
+
+ # eliminate stale/mismatched entries
+ my %mismatch = map { $_->{num} => $_->{blob} } @$msgs;
+ while (my ($k, $docids) = each %x3m) {
+ my ($xnum, $hex) = unpack('JH*', $k);
+ my $bin = pack('H*', $hex);
+ my $exp = $mismatch{$xnum};
+ my $m = defined($exp) ? "mismatch (!= $exp)" : 'stale';
+ warn("# $xnum:$hex (#@$docids): $m\n");
+ for my $i (@$docids) {
+ _unref_doc($sync, $i, $ibx, $xnum, $bin);