]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/OverIdx.pm
overidx: {num} column is INTEGER PRIMARY KEY
[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 PRIMARY KEY 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 )
399
400         $dbh->do('CREATE INDEX IF NOT EXISTS idx_tid ON over (tid)');
401         $dbh->do('CREATE INDEX IF NOT EXISTS idx_sid ON over (sid)');
402         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ts ON over (ts)');
403         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ds ON over (ds)');
404
405         $dbh->do(<<'');
406 CREATE TABLE IF NOT EXISTS counter (
407         key VARCHAR(8) PRIMARY KEY NOT NULL,
408         val INTEGER DEFAULT 0,
409         UNIQUE (key)
410 )
411
412         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('thread')");
413         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('ghost')");
414
415         $dbh->do(<<'');
416 CREATE TABLE IF NOT EXISTS subject (
417         sid INTEGER PRIMARY KEY AUTOINCREMENT,
418         path VARCHAR(40) NOT NULL, /* SHA-1 of normalized subject */
419         UNIQUE (path)
420 )
421
422         $dbh->do(<<'');
423 CREATE TABLE IF NOT EXISTS id2num (
424         id INTEGER NOT NULL, /* <=> msgid.id */
425         num INTEGER NOT NULL,
426         UNIQUE (id, num)
427 )
428
429         # performance critical:
430         $dbh->do('CREATE INDEX IF NOT EXISTS idx_inum ON id2num (num)');
431         $dbh->do('CREATE INDEX IF NOT EXISTS idx_id ON id2num (id)');
432
433         $dbh->do(<<'');
434 CREATE TABLE IF NOT EXISTS msgid (
435         id INTEGER PRIMARY KEY AUTOINCREMENT, /* <=> id2num.id */
436         mid VARCHAR(244) NOT NULL,
437         UNIQUE (mid)
438 )
439
440 }
441
442 sub commit_lazy {
443         my ($self) = @_;
444         delete $self->{txn} or return;
445         $self->{dbh}->commit;
446 }
447
448 sub begin_lazy {
449         my ($self) = @_;
450         return if $self->{txn};
451         my $dbh = $self->dbh or return;
452         $dbh->begin_work;
453         # $dbh->{Profile} = 2;
454         $self->{txn} = 1;
455 }
456
457 sub rollback_lazy {
458         my ($self) = @_;
459         delete $self->{txn} or return;
460         $self->{dbh}->rollback;
461 }
462
463 sub dbh_close {
464         my ($self) = @_;
465         die "in transaction" if $self->{txn};
466         $self->SUPER::dbh_close;
467 }
468
469 sub create {
470         my ($self) = @_;
471         unless (-r $self->{filename}) {
472                 require File::Path;
473                 require File::Basename;
474                 File::Path::mkpath(File::Basename::dirname($self->{filename}));
475         }
476         # create the DB:
477         PublicInbox::Over::dbh($self);
478         $self->dbh_close;
479 }
480
481 sub rethread_prepare {
482         my ($self, $opt) = @_;
483         return unless $opt->{rethread};
484         begin_lazy($self);
485         my $min = $self->{min_tid} = get_counter($self->{dbh}, 'thread') // 0;
486         my $pr = $opt->{-progress};
487         $pr->("rethread min THREADID ".($min + 1)."\n") if $pr && $min;
488 }
489
490 sub rethread_done {
491         my ($self, $opt) = @_;
492         return unless $opt->{rethread} && $self->{txn};
493         defined(my $min = $self->{min_tid}) or croak('BUG: no min_tid');
494         my $dbh = $self->{dbh} or croak('BUG: no dbh');
495         my $rows = $dbh->selectall_arrayref(<<'', { Slice => {} }, $min);
496 SELECT num,tid FROM over WHERE num < 0 AND tid < ?
497
498         my $show_id = $dbh->prepare('SELECT id FROM id2num WHERE num = ?');
499         my $show_mid = $dbh->prepare('SELECT mid FROM msgid WHERE id = ?');
500         my $pr = $opt->{-progress};
501         my $total = 0;
502         for my $r (@$rows) {
503                 my $exp = 0;
504                 $show_id->execute($r->{num});
505                 while (defined(my $id = $show_id->fetchrow_array)) {
506                         ++$exp;
507                         $show_mid->execute($id);
508                         my $mid = $show_mid->fetchrow_array;
509                         if (!defined($mid)) {
510                                 warn <<EOF;
511 E: ghost NUM=$r->{num} ID=$id THREADID=$r->{tid} has no Message-ID
512 EOF
513                                 next;
514                         }
515                         $pr->(<<EOM) if $pr;
516 I: ghost $r->{num} <$mid> THREADID=$r->{tid} culled
517 EOM
518                 }
519                 delete_by_num($self, $r->{num});
520         }
521         $pr->("I: rethread culled $total ghosts\n") if $pr && $total;
522 }
523
524 # used for cross-inbox search
525 sub eidx_prep ($) {
526         my ($self) = @_;
527         $self->{-eidx_prep} //= do {
528                 my $dbh = $self->dbh;
529                 $dbh->do(<<"");
530 INSERT OR IGNORE INTO counter (key) VALUES ('eidx_docid')
531
532                 $dbh->do(<<'');
533 CREATE TABLE IF NOT EXISTS inboxes (
534         ibx_id INTEGER PRIMARY KEY AUTOINCREMENT,
535         eidx_key VARCHAR(255) NOT NULL, /* {newsgroup} // {inboxdir} */
536         UNIQUE (eidx_key)
537 )
538
539                 $dbh->do(<<'');
540 CREATE TABLE IF NOT EXISTS xref3 (
541         docid INTEGER NOT NULL, /* <=> over.num */
542         ibx_id INTEGER NOT NULL, /* <=> inboxes.ibx_id */
543         xnum INTEGER NOT NULL, /* NNTP article number in ibx */
544         oidbin VARBINARY NOT NULL, /* 20-byte SHA-1 or 32-byte SHA-256 */
545         UNIQUE (docid, ibx_id, xnum, oidbin)
546 )
547
548         $dbh->do('CREATE INDEX IF NOT EXISTS idx_docid ON xref3 (docid)');
549
550         # performance critical, this is not UNIQUE since we may need to
551         # tolerate some old bugs from indexing mirrors
552         $dbh->do('CREATE INDEX IF NOT EXISTS idx_nntp ON '.
553                 'xref3 (oidbin,xnum,ibx_id)');
554
555                 $dbh->do(<<'');
556 CREATE TABLE IF NOT EXISTS eidx_meta (
557         key VARCHAR(255) PRIMARY KEY,
558         val VARCHAR(255) NOT NULL
559 )
560
561                 $dbh;
562         };
563 }
564
565 sub eidx_meta { # requires transaction
566         my ($self, $key, $val) = @_;
567
568         my $sql = 'SELECT val FROM eidx_meta WHERE key = ? LIMIT 1';
569         my $dbh = $self->{dbh};
570         defined($val) or return $dbh->selectrow_array($sql, undef, $key);
571
572         my $prev = $dbh->selectrow_array($sql, undef, $key);
573         if (defined $prev) {
574                 $sql = 'UPDATE eidx_meta SET val = ? WHERE key = ?';
575                 $dbh->do($sql, undef, $val, $key);
576         } else {
577                 $sql = 'INSERT INTO eidx_meta (key,val) VALUES (?,?)';
578                 $dbh->do($sql, undef, $key, $val);
579         }
580         $prev;
581 }
582
583 sub eidx_max {
584         my ($self) = @_;
585         get_counter($self->{dbh}, 'eidx_docid');
586 }
587
588 sub add_xref3 {
589         my ($self, $docid, $xnum, $oidhex, $eidx_key) = @_;
590         begin_lazy($self);
591         my $ibx_id = id_for($self, 'inboxes', 'ibx_id', eidx_key => $eidx_key);
592         my $oidbin = pack('H*', $oidhex);
593         my $sth = $self->{dbh}->prepare_cached(<<'');
594 INSERT OR IGNORE INTO xref3 (docid, ibx_id, xnum, oidbin) VALUES (?, ?, ?, ?)
595
596         $sth->bind_param(1, $docid);
597         $sth->bind_param(2, $ibx_id);
598         $sth->bind_param(3, $xnum);
599         $sth->bind_param(4, $oidbin, SQL_BLOB);
600         $sth->execute;
601 }
602
603 # returns remaining reference count to $docid
604 sub remove_xref3 {
605         my ($self, $docid, $oidhex, $eidx_key, $rm_eidx_info) = @_;
606         begin_lazy($self);
607         my $oidbin = pack('H*', $oidhex);
608         my ($sth, $ibx_id);
609         if (defined $eidx_key) {
610                 $ibx_id = id_for($self, 'inboxes', 'ibx_id',
611                                         eidx_key => $eidx_key);
612                 $sth = $self->{dbh}->prepare_cached(<<'');
613 DELETE FROM xref3 WHERE docid = ? AND ibx_id = ? AND oidbin = ?
614
615                 $sth->bind_param(1, $docid);
616                 $sth->bind_param(2, $ibx_id);
617                 $sth->bind_param(3, $oidbin, SQL_BLOB);
618         } else {
619                 $sth = $self->{dbh}->prepare_cached(<<'');
620 DELETE FROM xref3 WHERE docid = ? AND oidbin = ?
621
622                 $sth->bind_param(1, $docid);
623                 $sth->bind_param(2, $oidbin, SQL_BLOB);
624         }
625         $sth->execute;
626         $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
627 SELECT COUNT(*) FROM xref3 WHERE docid = ?
628
629         $sth->execute($docid);
630         my $nr = $sth->fetchrow_array;
631         if ($nr == 0) {
632                 delete_by_num($self, $docid);
633         } elsif (defined($ibx_id) && $rm_eidx_info) {
634                 # if deduplication rules in ContentHash change, it's
635                 # possible a docid can have multiple rows with the
636                 # same ibx_id.  This governs whether or not we call
637                 # ->shard_remove_eidx_info in ExtSearchIdx.
638                 $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
639 SELECT COUNT(*) FROM xref3 WHERE docid = ? AND ibx_id = ?
640
641                 $sth->execute($docid, $ibx_id);
642                 my $count = $sth->fetchrow_array;
643                 $$rm_eidx_info = ($count == 0);
644         }
645         $nr;
646 }
647
648 # for when an xref3 goes missing, this does NOT update {ts}
649 sub update_blob {
650         my ($self, $smsg, $oidhex) = @_;
651         my $sth = $self->{dbh}->prepare(<<'');
652 UPDATE over SET ddd = ? WHERE num = ?
653
654         $smsg->{blob} = $oidhex;
655         $sth->bind_param(1, ddd_for($smsg), SQL_BLOB);
656         $sth->bind_param(2, $smsg->{num});
657         $sth->execute;
658 }
659
660 1;