]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/OverIdx.pm
over: ensure old, merged {tid} is really gone
[public-inbox.git] / lib / PublicInbox / OverIdx.pm
1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # for XOVER, OVER in NNTP, and feeds/homepage/threads in PSGI
5 # Unlike Msgmap, this is an _UNSTABLE_ cache which can be
6 # tweaked/updated over time and rebuilt.
7 #
8 # Ghost messages (messages which are only referenced in References/In-Reply-To)
9 # are denoted by a negative NNTP article number.
10 package PublicInbox::OverIdx;
11 use strict;
12 use v5.10.1;
13 use parent qw(PublicInbox::Over);
14 use IO::Handle;
15 use DBI qw(:sql_types); # SQL_BLOB
16 use PublicInbox::MID qw/id_compress mids_for_index references/;
17 use PublicInbox::Smsg qw(subject_normalized);
18 use Compress::Zlib qw(compress);
19 use Carp qw(croak);
20
21 sub dbh_new {
22         my ($self) = @_;
23         my $dbh = $self->SUPER::dbh_new($self->{-no_fsync} ? 2 : 1);
24
25         # 80000 pages (80MiB on SQLite <3.12.0, 320MiB on 3.12.0+)
26         # was found to be good in 2018 during the large LKML import
27         # at the time.  This ought to be configurable based on HW
28         # and inbox size; I suspect it's overkill for many inboxes.
29         $dbh->do('PRAGMA cache_size = 80000');
30
31         create_tables($dbh);
32         $dbh;
33 }
34
35 sub new {
36         my ($class, $f) = @_;
37         my $self = $class->SUPER::new($f);
38         $self->{min_tid} = 0;
39         $self;
40 }
41
42 sub get_counter ($$) {
43         my ($dbh, $key) = @_;
44         my $sth = $dbh->prepare_cached(<<'', undef, 1);
45 SELECT val FROM counter WHERE key = ? LIMIT 1
46
47         $sth->execute($key);
48         $sth->fetchrow_array;
49 }
50
51 sub adj_counter ($$$) {
52         my ($self, $key, $op) = @_;
53         my $dbh = $self->{dbh};
54         my $sth = $dbh->prepare_cached(<<"");
55 UPDATE counter SET val = val $op 1 WHERE key = ?
56
57         $sth->execute($key);
58
59         get_counter($dbh, $key);
60 }
61
62 sub next_tid { adj_counter($_[0], 'thread', '+') }
63 sub next_ghost_num { adj_counter($_[0], 'ghost', '-') }
64
65 sub id_for ($$$$$) {
66         my ($self, $tbl, $id_col, $val_col, $val) = @_;
67         my $dbh = $self->{dbh};
68         my $in = $dbh->prepare_cached(<<"")->execute($val);
69 INSERT OR IGNORE INTO $tbl ($val_col) VALUES (?)
70
71         if ($in == 0) {
72                 my $sth = $dbh->prepare_cached(<<"", undef, 1);
73 SELECT $id_col FROM $tbl WHERE $val_col = ? LIMIT 1
74
75                 $sth->execute($val);
76                 $sth->fetchrow_array;
77         } else {
78                 $dbh->last_insert_id(undef, undef, $tbl, $id_col);
79         }
80 }
81
82 sub sid {
83         my ($self, $path) = @_;
84         return unless defined $path && $path ne '';
85         id_for($self, 'subject', 'sid', 'path' => $path);
86 }
87
88 sub mid2id {
89         my ($self, $mid) = @_;
90         id_for($self, 'msgid', 'id', 'mid' => $mid);
91 }
92
93 sub delete_by_num {
94         my ($self, $num, $tid_ref) = @_;
95         my $dbh = $self->{dbh};
96         if ($tid_ref) {
97                 my $sth = $dbh->prepare_cached(<<'', undef, 1);
98 SELECT tid FROM over WHERE num = ? LIMIT 1
99
100                 $sth->execute($num);
101                 $$tid_ref = $sth->fetchrow_array; # may be undef
102         }
103         foreach (qw(over id2num)) {
104                 $dbh->prepare_cached(<<"")->execute($num);
105 DELETE FROM $_ WHERE num = ?
106
107         }
108 }
109
110 # this includes ghosts
111 sub each_by_mid {
112         my ($self, $mid, $cols, $cb, @arg) = @_;
113         my $dbh = $self->{dbh};
114
115 =over
116         I originally wanted to stuff everything into a single query:
117
118         SELECT over.* FROM over
119         LEFT JOIN id2num ON over.num = id2num.num
120         LEFT JOIN msgid ON msgid.id = id2num.id
121         WHERE msgid.mid = ? AND over.num >= ?
122         ORDER BY over.num ASC
123         LIMIT 1000
124
125         But it's faster broken out (and we're always in a
126         transaction for subroutines in this file)
127 =cut
128
129         my $sth = $dbh->prepare_cached(<<'', undef, 1);
130 SELECT id FROM msgid WHERE mid = ? LIMIT 1
131
132         $sth->execute($mid);
133         my $id = $sth->fetchrow_array;
134         defined $id or return;
135
136         push(@$cols, 'num');
137         $cols = join(',', map { $_ } @$cols);
138         my $lim = 10;
139         my $prev = get_counter($dbh, 'ghost');
140         while (1) {
141                 $sth = $dbh->prepare_cached(<<"", undef, 1);
142 SELECT num FROM id2num WHERE id = ? AND num >= ?
143 ORDER BY num ASC
144 LIMIT $lim
145
146                 $sth->execute($id, $prev);
147                 my $nums = $sth->fetchall_arrayref;
148                 my $nr = scalar(@$nums) or return;
149                 $prev = $nums->[-1]->[0];
150
151                 $sth = $dbh->prepare_cached(<<"", undef, 1);
152 SELECT $cols FROM over WHERE over.num = ? LIMIT 1
153
154                 foreach (@$nums) {
155                         $sth->execute($_->[0]);
156                         my $smsg = $sth->fetchrow_hashref;
157                         $smsg = PublicInbox::Over::load_from_row($smsg);
158                         $cb->($self, $smsg, @arg) or return;
159                 }
160                 return if $nr != $lim;
161         }
162 }
163
164 sub _resolve_mid_to_tid {
165         my ($self, $smsg, $tid) = @_;
166         my $cur_tid = $smsg->{tid};
167         if (defined $$tid) {
168                 merge_threads($self, $$tid, $cur_tid);
169         } elsif ($cur_tid > $self->{min_tid}) {
170                 $$tid = $cur_tid;
171         } else { # rethreading, queue up dead ghosts
172                 $$tid = next_tid($self);
173                 my $n = $smsg->{num};
174                 if ($n > 0) {
175                         $self->{dbh}->prepare_cached(<<'')->execute($$tid, $n);
176 UPDATE over SET tid = ? WHERE num = ?
177
178                 } elsif ($n < 0) {
179                         push(@{$self->{-ghosts_to_delete}}, $n);
180                 }
181         }
182         1;
183 }
184
185 # this will create a ghost as necessary
186 sub resolve_mid_to_tid {
187         my ($self, $mid) = @_;
188         my $tid;
189         each_by_mid($self, $mid, ['tid'], \&_resolve_mid_to_tid, \$tid);
190         if (my $del = delete $self->{-ghosts_to_delete}) {
191                 delete_by_num($self, $_) for @$del;
192         }
193         $tid // do { # create a new ghost
194                 my $id = mid2id($self, $mid);
195                 my $num = next_ghost_num($self);
196                 $num < 0 or die "ghost num is non-negative: $num\n";
197                 $tid = next_tid($self);
198                 my $dbh = $self->{dbh};
199                 $dbh->prepare_cached(<<'')->execute($num, $tid);
200 INSERT INTO over (num, tid) VALUES (?,?)
201
202                 $dbh->prepare_cached(<<'')->execute($id, $num);
203 INSERT INTO id2num (id, num) VALUES (?,?)
204
205                 $tid;
206         };
207 }
208
209 sub merge_threads {
210         my ($self, $winner_tid, $loser_tid) = @_;
211         return if $winner_tid == $loser_tid;
212         my $dbh = $self->{dbh};
213         $dbh->prepare_cached(<<'')->execute($winner_tid, $loser_tid);
214 UPDATE over SET tid = ? WHERE tid = ?
215
216 }
217
218 sub link_refs {
219         my ($self, $refs, $old_tid) = @_;
220         my $tid;
221
222         if (@$refs) {
223                 # first ref *should* be the thread root,
224                 # but we can never trust clients to do the right thing
225                 my $ref = $refs->[0];
226                 $tid = resolve_mid_to_tid($self, $ref);
227                 merge_threads($self, $tid, $old_tid) if defined $old_tid;
228
229                 # the rest of the refs should point to this tid:
230                 foreach my $i (1..$#$refs) {
231                         $ref = $refs->[$i];
232                         my $ptid = resolve_mid_to_tid($self, $ref);
233                         merge_threads($self, $tid, $ptid);
234                 }
235         } else {
236                 $tid = $old_tid // next_tid($self);
237         }
238         $tid;
239 }
240
241 sub parse_references ($$$) {
242         my ($smsg, $hdr, $mids) = @_;
243         my $refs = references($hdr);
244         push(@$refs, @$mids) if scalar(@$mids) > 1;
245         return $refs if scalar(@$refs) == 0;
246
247         # prevent circular references here:
248         my %seen = ( $smsg->{mid} => 1 );
249         my @keep;
250         foreach my $ref (@$refs) {
251                 if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
252                         warn "References: <$ref> too long, ignoring\n";
253                         next;
254                 }
255                 push(@keep, $ref) unless $seen{$ref}++;
256         }
257         $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
258         \@keep;
259 }
260
261 # normalize subjects so they are suitable as pathnames for URLs
262 # XXX: consider for removal
263 sub subject_path ($) {
264         my ($subj) = @_;
265         $subj = subject_normalized($subj);
266         $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
267         lc($subj);
268 }
269
270 sub ddd_for ($) {
271         my ($smsg) = @_;
272         my $dd = $smsg->to_doc_data;
273         utf8::encode($dd);
274         compress($dd);
275 }
276
277 sub add_overview {
278         my ($self, $eml, $smsg) = @_;
279         $smsg->{lines} = $eml->body_raw =~ tr!\n!\n!;
280         my $mids = mids_for_index($eml);
281         my $refs = parse_references($smsg, $eml, $mids);
282         my $subj = $smsg->{subject};
283         my $xpath;
284         if ($subj ne '') {
285                 $xpath = subject_path($subj);
286                 $xpath = id_compress($xpath);
287         }
288         add_over($self, $smsg, $mids, $refs, $xpath, ddd_for($smsg));
289 }
290
291 sub _add_over {
292         my ($self, $smsg, $mid, $refs, $old_tid, $v) = @_;
293         my $cur_tid = $smsg->{tid};
294         my $n = $smsg->{num};
295         die "num must not be zero for $mid" if !$n;
296         my $cur_valid = $cur_tid > $self->{min_tid};
297
298         if ($n > 0) { # regular mail
299                 if ($cur_valid) {
300                         $$old_tid //= $cur_tid;
301                         merge_threads($self, $$old_tid, $cur_tid);
302                 } else {
303                         $$old_tid //= next_tid($self);
304                 }
305         } elsif ($n < 0) { # ghost
306                 $$old_tid //= $cur_valid ? $cur_tid : next_tid($self);
307                 $$old_tid = link_refs($self, $refs, $$old_tid);
308                 delete_by_num($self, $n);
309                 $$v++;
310         }
311         1;
312 }
313
314 sub add_over {
315         my ($self, $smsg, $mids, $refs, $xpath, $ddd) = @_;
316         my $old_tid;
317         my $vivified = 0;
318         my $num = $smsg->{num};
319
320         begin_lazy($self);
321         delete_by_num($self, $num, \$old_tid);
322         $old_tid = undef if ($old_tid // 0) <= $self->{min_tid};
323         foreach my $mid (@$mids) {
324                 my $v = 0;
325                 each_by_mid($self, $mid, ['tid'], \&_add_over,
326                                 $mid, $refs, \$old_tid, \$v);
327                 $v > 1 and warn "BUG: vivified multiple ($v) ghosts for $mid\n";
328                 $vivified += $v;
329         }
330         $smsg->{tid} = $vivified ? $old_tid : link_refs($self, $refs, $old_tid);
331         $smsg->{sid} = sid($self, $xpath);
332         my $dbh = $self->{dbh};
333         my $sth = $dbh->prepare_cached(<<'');
334 INSERT INTO over (num, tid, sid, ts, ds, ddd)
335 VALUES (?,?,?,?,?,?)
336
337         my $nc = 1;
338         $sth->bind_param($nc, $num);
339         $sth->bind_param(++$nc, $smsg->{$_}) for (qw(tid sid ts ds));
340         $sth->bind_param(++$nc, $ddd, SQL_BLOB);
341         $sth->execute;
342         $sth = $dbh->prepare_cached(<<'');
343 INSERT INTO id2num (id, num) VALUES (?,?)
344
345         foreach my $mid (@$mids) {
346                 my $id = mid2id($self, $mid);
347                 $sth->execute($id, $num);
348         }
349 }
350
351 sub _remove_oid {
352         my ($self, $smsg, $oid, $removed) = @_;
353         if (!defined($oid) || $smsg->{blob} eq $oid) {
354                 delete_by_num($self, $smsg->{num});
355                 push @$removed, $smsg->{num};
356         }
357         1;
358 }
359
360 # returns number of removed messages in scalar context,
361 # array of removed article numbers in array context.
362 # $oid may be undef to match only on $mid
363 sub remove_oid {
364         my ($self, $oid, $mid) = @_;
365         my $removed = [];
366         begin_lazy($self);
367         each_by_mid($self, $mid, ['ddd'], \&_remove_oid, $oid, $removed);
368         @$removed;
369 }
370
371 sub _num_mid0_for_oid {
372         my ($self, $smsg, $oid, $res) = @_;
373         my $blob = $smsg->{blob};
374         return 1 if (!defined($blob) || $blob ne $oid); # continue;
375         @$res = ($smsg->{num}, $smsg->{mid});
376         0; # done
377 }
378
379 sub num_mid0_for_oid {
380         my ($self, $oid, $mid) = @_;
381         my $res = [];
382         begin_lazy($self);
383         each_by_mid($self, $mid, ['ddd'], \&_num_mid0_for_oid, $oid, $res);
384         @$res, # ($num, $mid0);
385 }
386
387 sub create_tables {
388         my ($dbh) = @_;
389
390         $dbh->do(<<'');
391 CREATE TABLE IF NOT EXISTS over (
392         num INTEGER NOT NULL, /* NNTP article number == IMAP UID */
393         tid INTEGER NOT NULL, /* THREADID (IMAP REFERENCES threading, JMAP) */
394         sid INTEGER, /* Subject ID (IMAP ORDEREDSUBJECT "threading") */
395         ts INTEGER, /* IMAP INTERNALDATE (Received: header, git commit time) */
396         ds INTEGER, /* RFC-2822 sent Date: header, git author time */
397         ddd VARBINARY, /* doc-data-deflated (->to_doc_data, ->load_from_data) */
398         UNIQUE (num)
399 )
400
401         $dbh->do('CREATE INDEX IF NOT EXISTS idx_tid ON over (tid)');
402         $dbh->do('CREATE INDEX IF NOT EXISTS idx_sid ON over (sid)');
403         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ts ON over (ts)');
404         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ds ON over (ds)');
405
406         $dbh->do(<<'');
407 CREATE TABLE IF NOT EXISTS counter (
408         key VARCHAR(8) PRIMARY KEY NOT NULL,
409         val INTEGER DEFAULT 0,
410         UNIQUE (key)
411 )
412
413         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('thread')");
414         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('ghost')");
415
416         $dbh->do(<<'');
417 CREATE TABLE IF NOT EXISTS subject (
418         sid INTEGER PRIMARY KEY AUTOINCREMENT,
419         path VARCHAR(40) NOT NULL, /* SHA-1 of normalized subject */
420         UNIQUE (path)
421 )
422
423         $dbh->do(<<'');
424 CREATE TABLE IF NOT EXISTS id2num (
425         id INTEGER NOT NULL, /* <=> msgid.id */
426         num INTEGER NOT NULL,
427         UNIQUE (id, num)
428 )
429
430         # performance critical:
431         $dbh->do('CREATE INDEX IF NOT EXISTS idx_inum ON id2num (num)');
432         $dbh->do('CREATE INDEX IF NOT EXISTS idx_id ON id2num (id)');
433
434         $dbh->do(<<'');
435 CREATE TABLE IF NOT EXISTS msgid (
436         id INTEGER PRIMARY KEY AUTOINCREMENT, /* <=> id2num.id */
437         mid VARCHAR(244) NOT NULL,
438         UNIQUE (mid)
439 )
440
441 }
442
443 sub commit_lazy {
444         my ($self) = @_;
445         delete $self->{txn} or return;
446         $self->{dbh}->commit;
447 }
448
449 sub begin_lazy {
450         my ($self) = @_;
451         return if $self->{txn};
452         my $dbh = $self->dbh or return;
453         $dbh->begin_work;
454         # $dbh->{Profile} = 2;
455         $self->{txn} = 1;
456 }
457
458 sub rollback_lazy {
459         my ($self) = @_;
460         delete $self->{txn} or return;
461         $self->{dbh}->rollback;
462 }
463
464 sub dbh_close {
465         my ($self) = @_;
466         die "in transaction" if $self->{txn};
467         $self->SUPER::dbh_close;
468 }
469
470 sub create {
471         my ($self) = @_;
472         unless (-r $self->{filename}) {
473                 require File::Path;
474                 require File::Basename;
475                 File::Path::mkpath(File::Basename::dirname($self->{filename}));
476         }
477         # create the DB:
478         PublicInbox::Over::dbh($self);
479         $self->dbh_close;
480 }
481
482 sub rethread_prepare {
483         my ($self, $opt) = @_;
484         return unless $opt->{rethread};
485         begin_lazy($self);
486         my $min = $self->{min_tid} = get_counter($self->{dbh}, 'thread') // 0;
487         my $pr = $opt->{-progress};
488         $pr->("rethread min THREADID ".($min + 1)."\n") if $pr && $min;
489 }
490
491 sub rethread_done {
492         my ($self, $opt) = @_;
493         return unless $opt->{rethread} && $self->{txn};
494         defined(my $min = $self->{min_tid}) or croak('BUG: no min_tid');
495         my $dbh = $self->{dbh} or croak('BUG: no dbh');
496         my $rows = $dbh->selectall_arrayref(<<'', { Slice => {} }, $min);
497 SELECT num,tid FROM over WHERE num < 0 AND tid < ?
498
499         my $show_id = $dbh->prepare('SELECT id FROM id2num WHERE num = ?');
500         my $show_mid = $dbh->prepare('SELECT mid FROM msgid WHERE id = ?');
501         my $pr = $opt->{-progress};
502         my $total = 0;
503         for my $r (@$rows) {
504                 my $exp = 0;
505                 $show_id->execute($r->{num});
506                 while (defined(my $id = $show_id->fetchrow_array)) {
507                         ++$exp;
508                         $show_mid->execute($id);
509                         my $mid = $show_mid->fetchrow_array;
510                         if (!defined($mid)) {
511                                 warn <<EOF;
512 E: ghost NUM=$r->{num} ID=$id THREADID=$r->{tid} has no Message-ID
513 EOF
514                                 next;
515                         }
516                         $pr->(<<EOM) if $pr;
517 I: ghost $r->{num} <$mid> THREADID=$r->{tid} culled
518 EOM
519                 }
520                 delete_by_num($self, $r->{num});
521         }
522         $pr->("I: rethread culled $total ghosts\n") if $pr && $total;
523 }
524
525 # used for cross-inbox search
526 sub eidx_prep ($) {
527         my ($self) = @_;
528         $self->{-eidx_prep} //= do {
529                 my $dbh = $self->dbh;
530                 $dbh->do(<<"");
531 INSERT OR IGNORE INTO counter (key) VALUES ('eidx_docid')
532
533                 $dbh->do(<<'');
534 CREATE TABLE IF NOT EXISTS inboxes (
535         ibx_id INTEGER PRIMARY KEY AUTOINCREMENT,
536         eidx_key VARCHAR(255) NOT NULL, /* {newsgroup} // {inboxdir} */
537         UNIQUE (eidx_key)
538 )
539
540                 $dbh->do(<<'');
541 CREATE TABLE IF NOT EXISTS xref3 (
542         docid INTEGER NOT NULL, /* <=> over.num */
543         ibx_id INTEGER NOT NULL, /* <=> inboxes.ibx_id */
544         xnum INTEGER NOT NULL, /* NNTP article number in ibx */
545         oidbin VARBINARY NOT NULL, /* 20-byte SHA-1 or 32-byte SHA-256 */
546         UNIQUE (docid, ibx_id, xnum, oidbin)
547 )
548
549         $dbh->do('CREATE INDEX IF NOT EXISTS idx_docid ON xref3 (docid)');
550
551         # performance critical, this is not UNIQUE since we may need to
552         # tolerate some old bugs from indexing mirrors
553         $dbh->do('CREATE INDEX IF NOT EXISTS idx_nntp ON '.
554                 'xref3 (oidbin,xnum,ibx_id)');
555
556                 $dbh->do(<<'');
557 CREATE TABLE IF NOT EXISTS eidx_meta (
558         key VARCHAR(255) PRIMARY KEY,
559         val VARCHAR(255) NOT NULL
560 )
561
562                 $dbh;
563         };
564 }
565
566 sub eidx_meta { # requires transaction
567         my ($self, $key, $val) = @_;
568
569         my $sql = 'SELECT val FROM eidx_meta WHERE key = ? LIMIT 1';
570         my $dbh = $self->{dbh};
571         defined($val) or return $dbh->selectrow_array($sql, undef, $key);
572
573         my $prev = $dbh->selectrow_array($sql, undef, $key);
574         if (defined $prev) {
575                 $sql = 'UPDATE eidx_meta SET val = ? WHERE key = ?';
576                 $dbh->do($sql, undef, $val, $key);
577         } else {
578                 $sql = 'INSERT INTO eidx_meta (key,val) VALUES (?,?)';
579                 $dbh->do($sql, undef, $key, $val);
580         }
581         $prev;
582 }
583
584 sub eidx_max {
585         my ($self) = @_;
586         get_counter($self->{dbh}, 'eidx_docid');
587 }
588
589 sub add_xref3 {
590         my ($self, $docid, $xnum, $oidhex, $eidx_key) = @_;
591         begin_lazy($self);
592         my $ibx_id = id_for($self, 'inboxes', 'ibx_id', eidx_key => $eidx_key);
593         my $oidbin = pack('H*', $oidhex);
594         my $sth = $self->{dbh}->prepare_cached(<<'');
595 INSERT OR IGNORE INTO xref3 (docid, ibx_id, xnum, oidbin) VALUES (?, ?, ?, ?)
596
597         $sth->bind_param(1, $docid);
598         $sth->bind_param(2, $ibx_id);
599         $sth->bind_param(3, $xnum);
600         $sth->bind_param(4, $oidbin, SQL_BLOB);
601         $sth->execute;
602 }
603
604 # returns remaining reference count to $docid
605 sub remove_xref3 {
606         my ($self, $docid, $oidhex, $eidx_key, $rm_eidx_info) = @_;
607         begin_lazy($self);
608         my $oidbin = pack('H*', $oidhex);
609         my ($sth, $ibx_id);
610         if (defined $eidx_key) {
611                 $ibx_id = id_for($self, 'inboxes', 'ibx_id',
612                                         eidx_key => $eidx_key);
613                 $sth = $self->{dbh}->prepare_cached(<<'');
614 DELETE FROM xref3 WHERE docid = ? AND ibx_id = ? AND oidbin = ?
615
616                 $sth->bind_param(1, $docid);
617                 $sth->bind_param(2, $ibx_id);
618                 $sth->bind_param(3, $oidbin, SQL_BLOB);
619         } else {
620                 $sth = $self->{dbh}->prepare_cached(<<'');
621 DELETE FROM xref3 WHERE docid = ? AND oidbin = ?
622
623                 $sth->bind_param(1, $docid);
624                 $sth->bind_param(2, $oidbin, SQL_BLOB);
625         }
626         $sth->execute;
627         $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
628 SELECT COUNT(*) FROM xref3 WHERE docid = ?
629
630         $sth->execute($docid);
631         my $nr = $sth->fetchrow_array;
632         if ($nr == 0) {
633                 delete_by_num($self, $docid);
634         } elsif (defined($ibx_id) && $rm_eidx_info) {
635                 # if deduplication rules in ContentHash change, it's
636                 # possible a docid can have multiple rows with the
637                 # same ibx_id.  This governs whether or not we call
638                 # ->shard_remove_eidx_info in ExtSearchIdx.
639                 $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
640 SELECT COUNT(*) FROM xref3 WHERE docid = ? AND ibx_id = ?
641
642                 $sth->execute($docid, $ibx_id);
643                 my $count = $sth->fetchrow_array;
644                 $$rm_eidx_info = ($count == 0);
645         }
646         $nr;
647 }
648
649 # for when an xref3 goes missing, this does NOT update {ts}
650 sub update_blob {
651         my ($self, $smsg, $oidhex) = @_;
652         my $sth = $self->{dbh}->prepare(<<'');
653 UPDATE over SET ddd = ? WHERE num = ?
654
655         $smsg->{blob} = $oidhex;
656         $sth->bind_param(1, ddd_for($smsg), SQL_BLOB);
657         $sth->bind_param(2, $smsg->{num});
658         $sth->execute;
659 }
660
661 1;