]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/OverIdx.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / OverIdx.pm
1 # Copyright (C) 2018-2021 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 ibx_id {
83         my ($self, $eidx_key) = @_;
84         id_for($self, 'inboxes', 'ibx_id', eidx_key => $eidx_key);
85 }
86
87 sub sid {
88         my ($self, $path) = @_;
89         return unless defined $path && $path ne '';
90         id_for($self, 'subject', 'sid', 'path' => $path);
91 }
92
93 sub mid2id {
94         my ($self, $mid) = @_;
95         id_for($self, 'msgid', 'id', 'mid' => $mid);
96 }
97
98 sub delete_by_num {
99         my ($self, $num, $tid_ref) = @_;
100         my $dbh = $self->{dbh};
101         if ($tid_ref) {
102                 my $sth = $dbh->prepare_cached(<<'', undef, 1);
103 SELECT tid FROM over WHERE num = ? LIMIT 1
104
105                 $sth->execute($num);
106                 $$tid_ref = $sth->fetchrow_array; # may be undef
107         }
108         foreach (qw(over id2num)) {
109                 $dbh->prepare_cached(<<"")->execute($num);
110 DELETE FROM $_ WHERE num = ?
111
112         }
113 }
114
115 # this includes ghosts
116 sub each_by_mid {
117         my ($self, $mid, $cols, $cb, @arg) = @_;
118         my $dbh = $self->{dbh};
119
120 =over
121         I originally wanted to stuff everything into a single query:
122
123         SELECT over.* FROM over
124         LEFT JOIN id2num ON over.num = id2num.num
125         LEFT JOIN msgid ON msgid.id = id2num.id
126         WHERE msgid.mid = ? AND over.num >= ?
127         ORDER BY over.num ASC
128         LIMIT 1000
129
130         But it's faster broken out (and we're always in a
131         transaction for subroutines in this file)
132 =cut
133
134         my $sth = $dbh->prepare_cached(<<'', undef, 1);
135 SELECT id FROM msgid WHERE mid = ? LIMIT 1
136
137         $sth->execute($mid);
138         my $id = $sth->fetchrow_array;
139         defined $id or return;
140
141         push(@$cols, 'num');
142         $cols = join(',', map { $_ } @$cols);
143         my $lim = 10;
144         my $prev = get_counter($dbh, 'ghost');
145         while (1) {
146                 $sth = $dbh->prepare_cached(<<"", undef, 1);
147 SELECT num FROM id2num WHERE id = ? AND num >= ?
148 ORDER BY num ASC
149 LIMIT $lim
150
151                 $sth->execute($id, $prev);
152                 my $nums = $sth->fetchall_arrayref;
153                 my $nr = scalar(@$nums) or return;
154                 $prev = $nums->[-1]->[0];
155
156                 $sth = $dbh->prepare_cached(<<"", undef, 1);
157 SELECT $cols FROM over WHERE over.num = ? LIMIT 1
158
159                 foreach (@$nums) {
160                         $sth->execute($_->[0]);
161                         # $cb may delete rows and invalidate nums
162                         my $smsg = $sth->fetchrow_hashref // next;
163                         $smsg = PublicInbox::Over::load_from_row($smsg);
164                         $cb->($self, $smsg, @arg) or return;
165                 }
166                 return if $nr != $lim;
167         }
168 }
169
170 sub _resolve_mid_to_tid {
171         my ($self, $smsg, $tid) = @_;
172         my $cur_tid = $smsg->{tid};
173         if (defined $$tid) {
174                 merge_threads($self, $$tid, $cur_tid);
175         } elsif ($cur_tid > $self->{min_tid}) {
176                 $$tid = $cur_tid;
177         } else { # rethreading, queue up dead ghosts
178                 $$tid = next_tid($self);
179                 my $n = $smsg->{num};
180                 if ($n > 0) {
181                         $self->{dbh}->prepare_cached(<<'')->execute($$tid, $n);
182 UPDATE over SET tid = ? WHERE num = ?
183
184                 } elsif ($n < 0) {
185                         push(@{$self->{-ghosts_to_delete}}, $n);
186                 }
187         }
188         1;
189 }
190
191 # this will create a ghost as necessary
192 sub resolve_mid_to_tid {
193         my ($self, $mid) = @_;
194         my $tid;
195         each_by_mid($self, $mid, ['tid'], \&_resolve_mid_to_tid, \$tid);
196         if (my $del = delete $self->{-ghosts_to_delete}) {
197                 delete_by_num($self, $_) for @$del;
198         }
199         $tid // do { # create a new ghost
200                 my $id = mid2id($self, $mid);
201                 my $num = next_ghost_num($self);
202                 $num < 0 or die "ghost num is non-negative: $num\n";
203                 $tid = next_tid($self);
204                 my $dbh = $self->{dbh};
205                 $dbh->prepare_cached(<<'')->execute($num, $tid);
206 INSERT INTO over (num, tid) VALUES (?,?)
207
208                 $dbh->prepare_cached(<<'')->execute($id, $num);
209 INSERT INTO id2num (id, num) VALUES (?,?)
210
211                 $tid;
212         };
213 }
214
215 sub merge_threads {
216         my ($self, $winner_tid, $loser_tid) = @_;
217         return if $winner_tid == $loser_tid;
218         my $dbh = $self->{dbh};
219         $dbh->prepare_cached(<<'')->execute($winner_tid, $loser_tid);
220 UPDATE over SET tid = ? WHERE tid = ?
221
222 }
223
224 sub link_refs {
225         my ($self, $refs, $old_tid) = @_;
226         my $tid;
227
228         if (@$refs) {
229                 # first ref *should* be the thread root,
230                 # but we can never trust clients to do the right thing
231                 my $ref = $refs->[0];
232                 $tid = resolve_mid_to_tid($self, $ref);
233                 merge_threads($self, $tid, $old_tid) if defined $old_tid;
234
235                 # the rest of the refs should point to this tid:
236                 foreach my $i (1..$#$refs) {
237                         $ref = $refs->[$i];
238                         my $ptid = resolve_mid_to_tid($self, $ref);
239                         merge_threads($self, $tid, $ptid);
240                 }
241         } else {
242                 $tid = $old_tid // next_tid($self);
243         }
244         $tid;
245 }
246
247 # normalize subjects somewhat, they used to be ASCII-only but now
248 # we use \w for UTF-8 support.  We may still drop it entirely and
249 # rely on Xapian for subject matches...
250 sub subject_path ($) {
251         my ($subj) = @_;
252         $subj = subject_normalized($subj);
253         $subj =~ s![^\w\.~/\-]+!_!g;
254         lc($subj);
255 }
256
257 sub ddd_for ($) {
258         my ($smsg) = @_;
259         my $dd = $smsg->to_doc_data;
260         utf8::encode($dd);
261         compress($dd);
262 }
263
264 sub add_overview {
265         my ($self, $eml, $smsg) = @_;
266         $smsg->{lines} = $eml->body_raw =~ tr!\n!\n!;
267         my $mids = mids_for_index($eml);
268         my $refs = $smsg->parse_references($eml, $mids);
269         $mids->[0] //= do {
270                 $smsg->{mid} //= '';
271                 $eml->{-lei_fake_mid};
272         };
273         my $subj = $smsg->{subject};
274         my $xpath;
275         if ($subj ne '') {
276                 $xpath = subject_path($subj);
277                 $xpath = id_compress($xpath);
278         }
279         add_over($self, $smsg, $mids, $refs, $xpath, ddd_for($smsg));
280 }
281
282 sub _add_over {
283         my ($self, $smsg, $mid, $refs, $old_tid, $v) = @_;
284         my $cur_tid = $smsg->{tid};
285         my $n = $smsg->{num};
286         die "num must not be zero for $mid" if !$n;
287         my $cur_valid = $cur_tid > $self->{min_tid};
288
289         if ($n > 0) { # regular mail
290                 if ($cur_valid) {
291                         $$old_tid //= $cur_tid;
292                         merge_threads($self, $$old_tid, $cur_tid);
293                 } else {
294                         $$old_tid //= next_tid($self);
295                 }
296         } elsif ($n < 0) { # ghost
297                 $$old_tid //= $cur_valid ? $cur_tid : next_tid($self);
298                 $$old_tid = link_refs($self, $refs, $$old_tid);
299                 delete_by_num($self, $n);
300                 $$v++;
301         }
302         1;
303 }
304
305 sub add_over {
306         my ($self, $smsg, $mids, $refs, $xpath, $ddd) = @_;
307         my $old_tid;
308         my $vivified = 0;
309         my $num = $smsg->{num};
310
311         begin_lazy($self);
312         delete_by_num($self, $num, \$old_tid);
313         $old_tid = undef if ($old_tid // 0) <= $self->{min_tid};
314         foreach my $mid (@$mids) {
315                 my $v = 0;
316                 each_by_mid($self, $mid, ['tid'], \&_add_over,
317                                 $mid, $refs, \$old_tid, \$v);
318                 $v > 1 and warn "BUG: vivified multiple ($v) ghosts for $mid\n";
319                 $vivified += $v;
320         }
321         $smsg->{tid} = $vivified ? $old_tid : link_refs($self, $refs, $old_tid);
322         $smsg->{sid} = sid($self, $xpath);
323         my $dbh = $self->{dbh};
324         my $sth = $dbh->prepare_cached(<<'');
325 INSERT INTO over (num, tid, sid, ts, ds, ddd)
326 VALUES (?,?,?,?,?,?)
327
328         my $nc = 1;
329         $sth->bind_param($nc, $num);
330         $sth->bind_param(++$nc, $smsg->{$_}) for (qw(tid sid ts ds));
331         $sth->bind_param(++$nc, $ddd, SQL_BLOB);
332         $sth->execute;
333         $sth = $dbh->prepare_cached(<<'');
334 INSERT INTO id2num (id, num) VALUES (?,?)
335
336         foreach my $mid (@$mids) {
337                 my $id = mid2id($self, $mid);
338                 $sth->execute($id, $num);
339         }
340 }
341
342 sub _remove_oid {
343         my ($self, $smsg, $oid, $removed) = @_;
344         if (!defined($oid) || $smsg->{blob} eq $oid) {
345                 delete_by_num($self, $smsg->{num});
346                 push @$removed, $smsg->{num};
347         }
348         1;
349 }
350
351 # returns number of removed messages in scalar context,
352 # array of removed article numbers in array context.
353 # $oid may be undef to match only on $mid
354 sub remove_oid {
355         my ($self, $oid, $mid) = @_;
356         my $removed = [];
357         begin_lazy($self);
358         each_by_mid($self, $mid, ['ddd'], \&_remove_oid, $oid, $removed);
359         @$removed;
360 }
361
362 sub _num_mid0_for_oid {
363         my ($self, $smsg, $oid, $res) = @_;
364         my $blob = $smsg->{blob};
365         return 1 if (!defined($blob) || $blob ne $oid); # continue;
366         @$res = ($smsg->{num}, $smsg->{mid});
367         0; # done
368 }
369
370 sub num_mid0_for_oid {
371         my ($self, $oid, $mid) = @_;
372         my $res = [];
373         begin_lazy($self);
374         each_by_mid($self, $mid, ['ddd'], \&_num_mid0_for_oid, $oid, $res);
375         @$res, # ($num, $mid0);
376 }
377
378 sub create_tables {
379         my ($dbh) = @_;
380
381         $dbh->do(<<'');
382 CREATE TABLE IF NOT EXISTS over (
383         num INTEGER PRIMARY KEY NOT NULL, /* NNTP article number == IMAP UID */
384         tid INTEGER NOT NULL, /* THREADID (IMAP REFERENCES threading, JMAP) */
385         sid INTEGER, /* Subject ID (IMAP ORDEREDSUBJECT "threading") */
386         ts INTEGER, /* IMAP INTERNALDATE (Received: header, git commit time) */
387         ds INTEGER, /* RFC-2822 sent Date: header, git author time */
388         ddd VARBINARY /* doc-data-deflated (->to_doc_data, ->load_from_data) */
389 )
390
391         $dbh->do('CREATE INDEX IF NOT EXISTS idx_tid ON over (tid)');
392         $dbh->do('CREATE INDEX IF NOT EXISTS idx_sid ON over (sid)');
393         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ts ON over (ts)');
394         $dbh->do('CREATE INDEX IF NOT EXISTS idx_ds ON over (ds)');
395
396         $dbh->do(<<'');
397 CREATE TABLE IF NOT EXISTS counter (
398         key VARCHAR(8) PRIMARY KEY NOT NULL,
399         val INTEGER DEFAULT 0,
400         UNIQUE (key)
401 )
402
403         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('thread')");
404         $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('ghost')");
405
406         $dbh->do(<<'');
407 CREATE TABLE IF NOT EXISTS subject (
408         sid INTEGER PRIMARY KEY AUTOINCREMENT,
409         path VARCHAR(40) NOT NULL, /* SHA-1 of normalized subject */
410         UNIQUE (path)
411 )
412
413         $dbh->do(<<'');
414 CREATE TABLE IF NOT EXISTS id2num (
415         id INTEGER NOT NULL, /* <=> msgid.id */
416         num INTEGER NOT NULL,
417         UNIQUE (id, num)
418 )
419
420         # performance critical:
421         $dbh->do('CREATE INDEX IF NOT EXISTS idx_inum ON id2num (num)');
422         $dbh->do('CREATE INDEX IF NOT EXISTS idx_id ON id2num (id)');
423
424         $dbh->do(<<'');
425 CREATE TABLE IF NOT EXISTS msgid (
426         id INTEGER PRIMARY KEY AUTOINCREMENT, /* <=> id2num.id */
427         mid VARCHAR(244) NOT NULL,
428         UNIQUE (mid)
429 )
430
431 }
432
433 sub commit_lazy {
434         my ($self) = @_;
435         delete $self->{txn} or return;
436         $self->{dbh}->commit;
437         eval { $self->{dbh}->do('PRAGMA optimize') };
438 }
439
440 sub begin_lazy {
441         my ($self) = @_;
442         return if $self->{txn};
443         my $dbh = $self->dbh or return;
444         $dbh->begin_work;
445         # $dbh->{Profile} = 2;
446         $self->{txn} = 1;
447 }
448
449 sub rollback_lazy {
450         my ($self) = @_;
451         delete $self->{txn} or return;
452         $self->{dbh}->rollback;
453 }
454
455 sub dbh_close {
456         my ($self) = @_;
457         die "in transaction" if $self->{txn};
458         $self->SUPER::dbh_close;
459 }
460
461 sub create {
462         my ($self) = @_;
463         my $fn = $self->{filename} // do {
464                 croak('BUG: no {filename}') unless $self->{dbh};
465                 return;
466         };
467         unless (-r $fn) {
468                 require File::Path;
469                 my ($dir) = ($fn =~ m!(.*?/)[^/]+\z!);
470                 File::Path::mkpath($dir);
471         }
472         # create the DB:
473         PublicInbox::Over::dbh($self);
474         $self->dbh_close;
475 }
476
477 sub rethread_prepare {
478         my ($self, $opt) = @_;
479         return unless $opt->{rethread};
480         begin_lazy($self);
481         my $min = $self->{min_tid} = get_counter($self->{dbh}, 'thread') // 0;
482         my $pr = $opt->{-progress};
483         $pr->("rethread min THREADID ".($min + 1)."\n") if $pr && $min;
484 }
485
486 sub rethread_done {
487         my ($self, $opt) = @_;
488         return unless $opt->{rethread} && $self->{txn};
489         defined(my $min = $self->{min_tid}) or croak('BUG: no min_tid');
490         my $dbh = $self->{dbh} or croak('BUG: no dbh');
491         my $rows = $dbh->selectall_arrayref(<<'', { Slice => {} }, $min);
492 SELECT num,tid FROM over WHERE num < 0 AND tid < ?
493
494         my $show_id = $dbh->prepare('SELECT id FROM id2num WHERE num = ?');
495         my $show_mid = $dbh->prepare('SELECT mid FROM msgid WHERE id = ?');
496         my $pr = $opt->{-progress};
497         my $total = 0;
498         for my $r (@$rows) {
499                 my $exp = 0;
500                 $show_id->execute($r->{num});
501                 while (defined(my $id = $show_id->fetchrow_array)) {
502                         ++$exp;
503                         $show_mid->execute($id);
504                         my $mid = $show_mid->fetchrow_array;
505                         if (!defined($mid)) {
506                                 warn <<EOF;
507 E: ghost NUM=$r->{num} ID=$id THREADID=$r->{tid} has no Message-ID
508 EOF
509                                 next;
510                         }
511                         $pr->(<<EOM) if $pr;
512 I: ghost $r->{num} <$mid> THREADID=$r->{tid} culled
513 EOM
514                 }
515                 delete_by_num($self, $r->{num});
516         }
517         $pr->("I: rethread culled $total ghosts\n") if $pr && $total;
518 }
519
520 # used for cross-inbox search
521 sub eidx_prep ($) {
522         my ($self) = @_;
523         $self->{-eidx_prep} //= do {
524                 my $dbh = $self->dbh;
525                 $dbh->do(<<'');
526 INSERT OR IGNORE INTO counter (key) VALUES ('eidx_docid')
527
528                 $dbh->do(<<'');
529 CREATE TABLE IF NOT EXISTS inboxes (
530         ibx_id INTEGER PRIMARY KEY AUTOINCREMENT,
531         eidx_key VARCHAR(255) NOT NULL, /* {newsgroup} // {inboxdir} */
532         UNIQUE (eidx_key)
533 )
534
535                 $dbh->do(<<'');
536 CREATE TABLE IF NOT EXISTS xref3 (
537         docid INTEGER NOT NULL, /* <=> over.num */
538         ibx_id INTEGER NOT NULL, /* <=> inboxes.ibx_id */
539         xnum INTEGER NOT NULL, /* NNTP article number in ibx */
540         oidbin VARBINARY NOT NULL, /* 20-byte SHA-1 or 32-byte SHA-256 */
541         UNIQUE (docid, ibx_id, xnum, oidbin)
542 )
543
544         $dbh->do('CREATE INDEX IF NOT EXISTS idx_docid ON xref3 (docid)');
545
546         # performance critical, this is not UNIQUE since we may need to
547         # tolerate some old bugs from indexing mirrors.  n.b. we used
548         # to index oidbin here, but leaving it out speeds up reindexing
549         # and "XHDR Xref <$MSGID>" isn't any slower w/o oidbin
550         $dbh->do('CREATE INDEX IF NOT EXISTS idx_reindex ON '.
551                 'xref3 (xnum,ibx_id)');
552
553         $dbh->do('CREATE INDEX IF NOT EXISTS idx_oidbin ON xref3 (oidbin)');
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                 # A queue of current docids which need reindexing.
562                 # eidxq persists across aborted -extindex invocations
563                 # Currently used for "-extindex --reindex" for Xapian
564                 # data, but may be used in more places down the line.
565                 $dbh->do(<<'');
566 CREATE TABLE IF NOT EXISTS eidxq (docid INTEGER PRIMARY KEY NOT NULL)
567
568                 1;
569         };
570 }
571
572 sub eidx_meta { # requires transaction
573         my ($self, $key, $val) = @_;
574
575         my $sql = 'SELECT val FROM eidx_meta WHERE key = ? LIMIT 1';
576         my $dbh = $self->{dbh};
577         defined($val) or return $dbh->selectrow_array($sql, undef, $key);
578
579         my $prev = $dbh->selectrow_array($sql, undef, $key);
580         if (defined $prev) {
581                 $sql = 'UPDATE eidx_meta SET val = ? WHERE key = ?';
582                 $dbh->do($sql, undef, $val, $key);
583         } else {
584                 $sql = 'INSERT INTO eidx_meta (key,val) VALUES (?,?)';
585                 $dbh->do($sql, undef, $key, $val);
586         }
587         $prev;
588 }
589
590 sub eidx_max {
591         my ($self) = @_;
592         get_counter($self->{dbh}, 'eidx_docid');
593 }
594
595 sub add_xref3 {
596         my ($self, $docid, $xnum, $oidhex, $eidx_key) = @_;
597         begin_lazy($self);
598         my $ibx_id = ibx_id($self, $eidx_key);
599         my $oidbin = pack('H*', $oidhex);
600         my $sth = $self->{dbh}->prepare_cached(<<'');
601 INSERT OR IGNORE INTO xref3 (docid, ibx_id, xnum, oidbin) VALUES (?, ?, ?, ?)
602
603         $sth->bind_param(1, $docid);
604         $sth->bind_param(2, $ibx_id);
605         $sth->bind_param(3, $xnum);
606         $sth->bind_param(4, $oidbin, SQL_BLOB);
607         $sth->execute;
608 }
609
610 # for when an xref3 goes missing, this does NOT update {ts}
611 sub update_blob {
612         my ($self, $smsg, $oidhex) = @_;
613         my $sth = $self->{dbh}->prepare(<<'');
614 UPDATE over SET ddd = ? WHERE num = ?
615
616         $smsg->{blob} = $oidhex;
617         $sth->bind_param(1, ddd_for($smsg), SQL_BLOB);
618         $sth->bind_param(2, $smsg->{num});
619         $sth->execute;
620 }
621
622 sub merge_xref3 { # used for "-extindex --dedupe"
623         my ($self, $keep_docid, $drop_docid, $oidbin) = @_;
624         my $sth = $self->{dbh}->prepare_cached(<<'');
625 UPDATE OR IGNORE xref3 SET docid = ? WHERE docid = ? AND oidbin = ?
626
627         $sth->bind_param(1, $keep_docid);
628         $sth->bind_param(2, $drop_docid);
629         $sth->bind_param(3, $oidbin, SQL_BLOB);
630         $sth->execute;
631
632         # drop anything that conflicted
633         $sth = $self->{dbh}->prepare_cached(<<'');
634 DELETE FROM xref3 WHERE docid = ? AND oidbin = ?
635
636         $sth->bind_param(1, $drop_docid);
637         $sth->bind_param(2, $oidbin, SQL_BLOB);
638         $sth->execute;
639 }
640
641 sub eidxq_add {
642         my ($self, $docid) = @_;
643         $self->dbh->prepare_cached(<<'')->execute($docid);
644 INSERT OR IGNORE INTO eidxq (docid) VALUES (?)
645
646 }
647
648 sub eidxq_del {
649         my ($self, $docid) = @_;
650         $self->dbh->prepare_cached(<<'')->execute($docid);
651 DELETE FROM eidxq WHERE docid = ?
652
653 }
654
655 # returns true if we're vivifying a message for lei/store that was
656 # previously external-metadata only
657 sub vivify_xvmd {
658         my ($self, $smsg) = @_;
659         my @docids = $self->blob_exists($smsg->{blob});
660         my @vivify_xvmd;
661         for my $id (@docids) {
662                 if (my $cur = $self->get_art($id)) {
663                         # already indexed if bytes > 0
664                         return if $cur->{bytes} > 0;
665                         push @vivify_xvmd, $id;
666                 } else {
667                         warn "W: $smsg->{blob} #$id gone (bug?)\n";
668                 }
669         }
670         $smsg->{-vivify_xvmd} = \@vivify_xvmd;
671 }
672
673 1;