1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Detached/external index cross inbox search indexing support
5 # read-write counterpart to PublicInbox::ExtSearch
7 # It's based on the same ideas as public-inbox-v2-format(5) using
8 # over.sqlite3 for dedupe and sharded Xapian. msgmap.sqlite3 is
9 # missing, so there is no Message-ID conflict resolution, meaning
10 # no NNTP support for now.
12 # v2 has a 1:1 mapping of index:inbox or msgmap for NNTP support.
13 # This is intended to be an M:N index:inbox mapping, but it'll likely
14 # be 1:N in common practice (M==1)
16 package PublicInbox::ExtSearchIdx;
19 use parent qw(PublicInbox::ExtSearch PublicInbox::Lock);
20 use Carp qw(croak carp);
21 use PublicInbox::Search;
22 use PublicInbox::SearchIdx qw(crlf_adjust prepare_stack is_ancestor
24 use PublicInbox::OverIdx;
25 use PublicInbox::MiscIdx;
26 use PublicInbox::MID qw(mids);
27 use PublicInbox::V2Writable;
28 use PublicInbox::InboxWritable;
29 use PublicInbox::ContentHash qw(content_hash);
32 use DBI qw(:sql_types); # SQL_BLOB
35 my (undef, $dir, $opt) = @_;
36 $dir = File::Spec->canonpath($dir);
37 my $l = $opt->{indexlevel} // 'full';
38 $l !~ $PublicInbox::SearchIdx::INDEXLEVELS and
39 die "invalid indexlevel=$l\n";
40 $l eq 'basic' and die "E: indexlevel=basic not yet supported\n";
42 xpfx => "$dir/ei".PublicInbox::Search::SCHEMA_VERSION,
44 creat => $opt->{creat},
45 ibx_map => {}, # (newsgroup//inboxdir) => $ibx
52 lock_path => "$dir/ei.lock",
54 $self->{shards} = $self->count_shards || nproc_shards($opt->{creat});
55 my $oidx = PublicInbox::OverIdx->new("$self->{xpfx}/over.sqlite3");
56 $oidx->{-no_fsync} = 1 if $opt->{-no_fsync};
57 $self->{oidx} = $oidx;
62 my ($self, $ibx) = @_;
63 $ibx = PublicInbox::InboxWritable->new($ibx);
64 my $key = $ibx->eidx_key;
65 if (!$ibx->over || !$ibx->mm) {
66 warn "W: skipping $key (unindexed)\n";
69 if (!defined($ibx->uidvalidity)) {
70 warn "W: skipping $key (no UIDVALIDITY)\n";
73 my $ibxdir = File::Spec->canonpath($ibx->{inboxdir});
74 if ($ibxdir ne $ibx->{inboxdir}) {
75 warn "W: `$ibx->{inboxdir}' canonicalized to `$ibxdir'\n";
76 $ibx->{inboxdir} = $ibxdir;
78 $ibx = PublicInbox::InboxWritable->new($ibx);
79 $self->{ibx_map}->{$key} //= do {
80 push @{$self->{ibx_list}}, $ibx;
85 sub _ibx_attach { # each_inbox callback
86 my ($ibx, $self) = @_;
87 attach_inbox($self, $ibx);
91 my ($self, $cfg) = @_;
93 $cfg->each_inbox(\&_ibx_attach, $self);
96 sub check_batch_limit ($) {
98 my $self = $req->{self};
99 my $new_smsg = $req->{new_smsg};
101 # {raw_bytes} may be unset, so just use {bytes}
102 my $n = $self->{transact_bytes} += $new_smsg->{bytes};
104 # set flag for PublicInbox::V2Writable::index_todo:
105 ${$req->{need_checkpoint}} = 1 if $n >= $self->{batch_bytes};
109 my ($req, $smsg) = @_;
110 my $self = $req->{self};
111 my $docid = $smsg->{num};
112 my $idx = $self->idx_shard($docid);
113 my $oid = $req->{oid};
114 my $xibx = $req->{ibx};
115 my $eml = $req->{eml};
116 my $eidx_key = $xibx->eidx_key;
117 if (my $new_smsg = $req->{new_smsg}) { # 'm' on cross-posted message
118 my $xnum = $req->{xnum};
119 $self->{oidx}->add_xref3($docid, $xnum, $oid, $eidx_key);
120 $idx->shard_add_eidx_info($docid, $eidx_key, $eml);
121 check_batch_limit($req);
124 my $nr = $self->{oidx}->remove_xref3($docid, $oid, $eidx_key,
127 $self->{oidx}->eidxq_del($docid);
128 $idx->shard_remove($docid);
129 } elsif ($rm_eidx_info) {
130 $idx->shard_remove_eidx_info($docid, $eidx_key, $eml);
131 $self->{oidx}->eidxq_add($docid); # yes, add
136 # called by V2Writable::sync_prepare
137 sub artnum_max { $_[0]->{oidx}->eidx_max }
139 sub index_unseen ($) {
141 my $new_smsg = $req->{new_smsg} or die 'BUG: {new_smsg} unset';
142 my $eml = delete $req->{eml};
143 $new_smsg->populate($eml, $req);
144 my $self = $req->{self};
145 my $docid = $self->{oidx}->adj_counter('eidx_docid', '+');
146 $new_smsg->{num} = $docid;
147 my $idx = $self->idx_shard($docid);
148 $self->{oidx}->add_overview($eml, $new_smsg);
149 my $oid = $new_smsg->{blob};
150 my $ibx = delete $req->{ibx} or die 'BUG: {ibx} unset';
151 $self->{oidx}->add_xref3($docid, $req->{xnum}, $oid, $ibx->eidx_key);
152 $idx->index_raw(undef, $eml, $new_smsg, $ibx);
153 check_batch_limit($req);
156 sub do_finalize ($) {
158 if (my $indexed = $req->{indexed}) {
159 do_xpost($req, $_) for @$indexed;
160 } elsif (exists $req->{new_smsg}) { # totally unseen messsage
163 # `d' message was already unindexed in the v1/v2 inboxes,
164 # so it's too noisy to warn, here.
166 # cur_cmt may be undef for unindex_oid, set by V2Writable::index_todo
167 if (defined(my $cur_cmt = $req->{cur_cmt})) {
168 ${$req->{latest_cmt}} = $cur_cmt;
172 sub do_step ($) { # main iterator for adding messages to the index
174 my $self = $req->{self} // die 'BUG: {self} missing';
176 if (my $next_arg = $req->{next_arg}) {
177 if (my $smsg = $self->{oidx}->next_by_mid(@$next_arg)) {
178 $req->{cur_smsg} = $smsg;
179 $self->git->cat_async($smsg->{blob},
180 \&ck_existing, $req);
181 return; # ck_existing calls do_step
183 delete $req->{cur_smsg};
184 delete $req->{next_arg};
186 my $mid = shift(@{$req->{mids}});
187 last unless defined $mid;
189 $req->{next_arg} = [ $mid, \$id, \$prev ];
195 sub _blob_missing ($) { # called when req->{cur_smsg}->{blob} is bad
197 my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
198 my $self = $req->{self};
199 my $xref3 = $self->{oidx}->get_xref3($smsg->{num});
200 my @keep = grep(!/:$smsg->{blob}\z/, @$xref3);
202 $keep[0] =~ /:([a-f0-9]{40,}+)\z/ or
203 die "BUG: xref $keep[0] has no OID";
205 $self->{oidx}->remove_xref3($smsg->{num}, $smsg->{blob});
206 my $upd = $self->{oidx}->update_blob($smsg, $oidhex);
207 my $saved = $self->{oidx}->get_art($smsg->{num});
209 $self->{oidx}->delete_by_num($smsg->{num});
213 sub ck_existing { # git->cat_async callback
214 my ($bref, $oid, $type, $size, $req) = @_;
215 my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
216 if ($type eq 'missing') {
218 } elsif (!is_bad_blob($oid, $type, $size, $smsg->{blob})) {
219 my $self = $req->{self} // die 'BUG: {self} missing';
220 local $self->{current_info} = "$self->{current_info} $oid";
221 my $cur = PublicInbox::Eml->new($bref);
222 if (content_hash($cur) eq $req->{chash}) {
223 push @{$req->{indexed}}, $smsg; # for do_xpost
224 } # else { index_unseen later }
229 # is the messages visible in the inbox currently being indexed?
230 # return the number if so
231 sub cur_ibx_xnum ($$) {
232 my ($req, $bref) = @_;
233 my $ibx = $req->{ibx} or die 'BUG: current {ibx} missing';
235 $req->{eml} = PublicInbox::Eml->new($bref);
236 $req->{chash} = content_hash($req->{eml});
237 $req->{mids} = mids($req->{eml});
238 my @q = @{$req->{mids}}; # copy
239 while (defined(my $mid = shift @q)) {
241 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
242 return $x->{num} if $x->{blob} eq $req->{oid};
248 sub index_oid { # git->cat_async callback for 'm'
249 my ($bref, $oid, $type, $size, $req) = @_;
250 my $self = $req->{self};
251 local $self->{current_info} = "$self->{current_info} $oid";
252 return if is_bad_blob($oid, $type, $size, $req->{oid});
253 my $new_smsg = $req->{new_smsg} = bless {
255 }, 'PublicInbox::Smsg';
256 $new_smsg->{bytes} = $size + crlf_adjust($$bref);
257 defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
262 sub unindex_oid { # git->cat_async callback for 'd'
263 my ($bref, $oid, $type, $size, $req) = @_;
264 my $self = $req->{self};
265 local $self->{current_info} = "$self->{current_info} $oid";
266 return if is_bad_blob($oid, $type, $size, $req->{oid});
267 return if defined(cur_ibx_xnum($req, $bref)); # was re-added
271 # overrides V2Writable::last_commits, called by sync_ranges via sync_prepare
273 my ($self, $sync) = @_;
275 my $ekey = $sync->{ibx}->eidx_key;
276 my $uv = $sync->{ibx}->uidvalidity;
277 for my $i (0..$sync->{epoch_max}) {
278 $heads->[$i] = $self->{oidx}->eidx_meta("lc-v2:$ekey//$uv;$i");
283 sub _sync_inbox ($$$) {
284 my ($self, $sync, $ibx) = @_;
286 $sync->{nr} = \(my $nr = 0);
287 my $v = $ibx->version;
288 my $ekey = $ibx->eidx_key;
291 defined($ibx->git_dir_latest(\$epoch_max)) or return;
292 $sync->{epoch_max} = $epoch_max;
293 sync_prepare($self, $sync); # or return # TODO: once MiscIdx is stable
295 my $uv = $ibx->uidvalidity;
296 my $lc = $self->{oidx}->eidx_meta("lc-v1:$ekey//$uv");
297 my $head = $ibx->mm->last_commit;
298 unless (defined $head) {
299 warn "E: $ibx->{inboxdir} is not indexed\n";
302 my $stk = prepare_stack($sync, $lc ? "$lc..$head" : $head);
303 my $unit = { stack => $stk, git => $ibx->git };
304 push @{$sync->{todo}}, $unit;
306 warn "E: $ekey unsupported inbox version (v$v)\n";
309 for my $unit (@{delete($sync->{todo}) // []}) {
310 last if $sync->{quit};
311 index_todo($self, $sync, $unit);
313 $self->{midx}->index_ibx($ibx) unless $sync->{quit};
314 $ibx->git->cleanup; # done with this inbox, now
317 sub gc_unref_doc ($$$$) {
318 my ($self, $ibx_id, $eidx_key, $docid) = @_;
319 my $dbh = $self->{oidx}->dbh;
321 # for debug/info purposes, oids may no longer be accessible
322 my $sth = $dbh->prepare_cached(<<'', undef, 1);
323 SELECT oidbin FROM xref3 WHERE docid = ? AND ibx_id = ?
325 $sth->execute($docid, $ibx_id);
326 my @oid = map { unpack('H*', $_->[0]) } @{$sth->fetchall_arrayref};
328 $dbh->prepare_cached(<<'')->execute($docid, $ibx_id);
329 DELETE FROM xref3 WHERE docid = ? AND ibx_id = ?
331 my $remain = $self->{oidx}->get_xref3($docid);
332 if (scalar(@$remain)) {
333 $self->{oidx}->eidxq_add($docid); # enqueue for reindex
335 warn "I: unref #$docid $eidx_key $oid\n";
338 warn "I: remove #$docid $eidx_key @oid\n";
339 $self->idx_shard($docid)->shard_remove($docid);
344 my ($self, $opt) = @_;
345 $self->{cfg} or die "E: GC requires ->attach_config\n";
347 $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
349 my $dbh = $self->{oidx}->dbh;
350 my $x3_doc = $dbh->prepare('SELECT docid FROM xref3 WHERE ibx_id = ?');
351 my $ibx_ck = $dbh->prepare('SELECT ibx_id,eidx_key FROM inboxes');
352 my $lc_i = $dbh->prepare('SELECT key FROM eidx_meta WHERE key LIKE ?');
355 while (my ($ibx_id, $eidx_key) = $ibx_ck->fetchrow_array) {
356 next if $self->{ibx_map}->{$eidx_key};
357 $self->{midx}->remove_eidx_key($eidx_key);
358 warn "I: deleting messages for $eidx_key...\n";
359 $x3_doc->execute($ibx_id);
360 while (defined(my $docid = $x3_doc->fetchrow_array)) {
361 gc_unref_doc($self, $ibx_id, $eidx_key, $docid);
363 $dbh->prepare_cached(<<'')->execute($ibx_id);
364 DELETE FROM inboxes WHERE ibx_id = ?
366 # drop last_commit info
368 $pat =~ s/([_%])/\\$1/g;
369 $lc_i->execute("lc-%:$pat//%");
370 while (my ($key) = $lc_i->fetchrow_array) {
371 next if $key !~ m!\Alc-v[1-9]+:\Q$eidx_key\E//!;
372 warn "I: removing $key\n";
373 $dbh->prepare_cached(<<'')->execute($key);
374 DELETE FROM eidx_meta WHERE key = ?
378 warn "I: $eidx_key removed\n";
381 # it's not real unless it's in `over', we use parallelism here,
382 # shards will be reading directly from over, so commit
383 $self->{oidx}->commit_lazy;
384 $self->{oidx}->begin_lazy;
386 for my $idx (@{$self->{idx_shards}}) {
387 warn "I: cleaning up shard #$idx->{shard}\n";
388 $idx->shard_over_check($self->{oidx});
390 my $nr = $dbh->do(<<'');
391 DELETE FROM xref3 WHERE docid NOT IN (SELECT num FROM over)
393 warn "I: eliminated $nr stale xref3 entries\n" if $nr != 0;
398 sub eidxq_process ($$) { # for reindexing
399 my ($self, $sync) = @_;
401 $self->{oidx}->commit_lazy; # ensure shard workers can see it
402 $self->{oidx}->begin_lazy;
403 my $dbh = $self->{oidx}->dbh;
404 my $tot = $dbh->selectrow_array('SELECT COUNT(*) FROM eidxq') or return;
406 $sync->{-regen_fmt} = "%u/$tot\n";
407 my $pr = $sync->{-opt}->{-progress};
409 my $min = $dbh->selectrow_array('SELECT MIN(docid) FROM eidxq');
410 my $max = $dbh->selectrow_array('SELECT MAX(docid) FROM eidxq');
411 $pr->("Xapian indexing $min..$max (total=$tot)\n");
414 my $del = $dbh->prepare('DELETE FROM eidxq WHERE docid = ?');
415 my $iter = $dbh->prepare('SELECT docid FROM eidxq ORDER BY docid ASC');
417 while (defined(my $docid = $iter->fetchrow_array)) {
418 $self->idx_shard($docid)->shard_reindex_docid($docid);
419 $del->execute($docid);
420 last if $sync->{quit};
421 my $cur = ++${$sync->{nr}};
423 # shards flush on their own, just don't queue up too many
425 if (($cur % 1000) == 0) {
426 $self->{oidx}->commit_lazy;
427 $self->{oidx}->begin_lazy;
428 $pr->("reindexed $cur/$tot\n") if $pr;
430 # this is only for SIGUSR1, shards do their own accounting:
431 reindex_checkpoint($self, $sync) if ${$sync->{need_checkpoint}};
433 $pr->("reindexed ${$sync->{nr}}/$tot\n") if $pr;
434 $self->{oidx}->commit_lazy;
435 $self->{oidx}->begin_lazy;
438 sub _reindex_unseen { # git->cat_async callback
439 my ($bref, $oid, $type, $size, $req) = @_;
440 return if is_bad_blob($oid, $type, $size, $req->{oid});
441 my $self = $req->{self} // die 'BUG: {self} unset';
442 local $self->{current_info} = "$self->{current_info} $oid";
443 my $new_smsg = bless { blob => $oid, }, 'PublicInbox::Smsg';
444 $new_smsg->{bytes} = $size + crlf_adjust($$bref);
445 my $eml = $req->{eml} = PublicInbox::Eml->new($bref);
446 $req->{new_smsg} = $new_smsg;
447 $req->{chash} = content_hash($eml);
448 $req->{mids} = mids($eml); # do_step iterates through this
449 do_step($req); # enter the normal indexing flow
452 # --reindex may catch totally unseen messages, this handles them
453 sub reindex_unseen ($$$$) {
454 my ($self, $sync, $ibx, $xsmsg) = @_;
457 autime => $xsmsg->{ds},
458 cotime => $xsmsg->{ts},
459 oid => $xsmsg->{blob},
461 xnum => $xsmsg->{num},
462 # {mids} and {chash} will be filled in at _reindex_unseen
464 warn "I: reindex_unseen ${\$ibx->eidx_key}:$req->{xnum}:$req->{oid}\n";
465 $self->git->cat_async($xsmsg->{blob}, \&_reindex_unseen, $req);
468 sub _reindex_check_unseen ($$$) {
469 my ($self, $sync, $ibx) = @_;
470 my $ibx_id = $ibx->{-ibx_id};
471 my ($beg, $end) = (1, 1000);
473 # first, check if we missed any messages in target $ibx
474 my $inx3 = $self->{oidx}->dbh->prepare(<<'');
475 SELECT DISTINCT(docid) FROM xref3 WHERE ibx_id = ? AND xnum = ? AND oidbin = ?
478 while (scalar(@{$msgs = $ibx->over->query_xover($beg, $end)})) {
479 $beg = $msgs->[-1]->{num} + 1;
481 for my $xsmsg (@$msgs) {
482 my $oidbin = pack('H*', $xsmsg->{blob});
483 $inx3->bind_param(1, $ibx_id);
484 $inx3->bind_param(2, $xsmsg->{num});
485 $inx3->bind_param(3, $oidbin, SQL_BLOB);
487 my $docids = $inx3->fetchall_arrayref;
488 # index messages which were totally missed
489 # the first time around ASAP:
490 if (scalar(@$docids) == 0) {
491 reindex_unseen($self, $sync, $ibx, $xsmsg);
492 } else { # already seen, reindex later
493 for my $r (@$docids) {
494 $self->{oidx}->eidxq_add($r->[0]);
497 last if $sync->{quit};
499 last if $sync->{quit};
503 sub _reindex_check_stale ($$$) {
504 my ($self, $sync, $ibx) = @_;
506 # now, check if there's stale xrefs
507 my $get_xnum = $self->{oidx}->dbh->prepare(<<'');
508 SELECT docid,xnum,oidbin FROM xref3 WHERE ibx_id = ? ORDER BY docid ASC
510 $get_xnum->execute($ibx->{-ibx_id});
511 my $del_xref3 = $self->{oidx}->dbh->prepare(<<'');
512 DELETE FROM xref3 WHERE ibx_id = ? AND xnum = ? AND oidbin = ?
514 while (my ($docid, $xnum, $oidbin) = $get_xnum->fetchrow_array) {
515 last if $sync->{quit};
516 my $smsg = $ibx->over->get_art($xnum);
517 my $oidhex = unpack('H*', $oidbin);
521 } elsif ($smsg->{blob} ne $oidhex) {
522 $err = "mismatch (!= $smsg->{blob})";
524 next; # likely, all good
526 warn $ibx->eidx_key . ":$xnum:$oidhex (#$docid): $err\n";
527 $del_xref3->bind_param(1, $ibx->{-ibx_id});
528 $del_xref3->bind_param(2, $xnum);
529 $del_xref3->bind_param(3, $oidbin, SQL_BLOB);
532 # get_xref3 over-fetches, but this is a rare path:
533 my $xr3 = $self->{oidx}->get_xref3($docid);
534 my $idx = $self->idx_shard($docid);
535 if (scalar(@$xr3) == 0) { # all gone
536 $self->{oidx}->delete_by_num($docid);
537 $self->{oidx}->eidxq_del($docid);
538 $idx->shard_remove($docid);
539 } else { # enqueue for reindex of remaining messages
540 $idx->shard_remove_eidx_info($docid, $ibx->eidx_key);
541 $self->{oidx}->eidxq_add($docid); # yes, add
546 sub _reindex_inbox ($$$) {
547 my ($self, $sync, $ibx) = @_;
548 _reindex_check_unseen($self, $sync, $ibx);
549 _reindex_check_stale($self, $sync, $ibx) unless $sync->{quit};
550 delete @$ibx{qw(over mm search git)}; # won't need these for a bit
554 my ($self, $sync) = @_;
556 for my $ibx (@{$self->{ibx_list}}) {
557 _reindex_inbox($self, $sync, $ibx);
558 last if $sync->{quit};
560 $self->git->async_wait_all; # ensure eidxq gets filled completely
561 eidxq_process($self, $sync) unless $sync->{quit};
564 sub eidx_sync { # main entry point
565 my ($self, $opt) = @_;
567 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
568 local $self->{current_info} = '';
569 local $SIG{__WARN__} = sub {
570 $warn_cb->($self->{current_info}, ': ', @_);
572 $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
573 $self->{oidx}->rethread_prepare($opt);
575 need_checkpoint => \(my $need_checkpoint = 0),
577 # DO NOT SET {reindex} here, it's incompatible with reused
578 # V2Writable code, reindex is totally different here
579 # compared to v1/v2 inboxes because we have multiple histories
581 -regen_fmt => "%u/?\n",
583 local $SIG{USR1} = sub { $need_checkpoint = 1 };
584 my $quit = PublicInbox::SearchIdx::quit_cb($sync);
585 local $SIG{QUIT} = $quit;
586 local $SIG{INT} = $quit;
587 local $SIG{TERM} = $quit;
588 for my $ibx (@{$self->{ibx_list}}) {
589 $ibx->{-ibx_id} //= $self->{oidx}->ibx_id($ibx->eidx_key);
591 eidx_reindex($self, $sync) if delete($opt->{reindex});
593 # don't use $_ here, it'll get clobbered by reindex_checkpoint
594 for my $ibx (@{$self->{ibx_list}}) {
595 last if $sync->{quit};
596 _sync_inbox($self, $sync, $ibx);
598 $self->{oidx}->rethread_done($opt) unless $sync->{quit};
599 eidxq_process($self, $sync) unless $sync->{quit};
601 PublicInbox::V2Writable::done($self);
604 sub update_last_commit { # overrides V2Writable
605 my ($self, $sync, $stk) = @_;
606 my $unit = $sync->{unit} // return;
607 my $latest_cmt = $stk ? $stk->{latest_cmt} : ${$sync->{latest_cmt}};
608 defined($latest_cmt) or return;
609 my $ibx = $sync->{ibx} or die 'BUG: {ibx} missing';
610 my $ekey = $ibx->eidx_key;
611 my $uv = $ibx->uidvalidity;
612 my $epoch = $unit->{epoch};
614 my $v = $ibx->version;
616 die 'No {epoch} for v2 unit' unless defined $epoch;
617 $meta_key = "lc-v2:$ekey//$uv;$epoch";
619 die 'Unexpected {epoch} for v1 unit' if defined $epoch;
620 $meta_key = "lc-v1:$ekey//$uv";
622 die "Unsupported inbox version: $v";
624 my $last = $self->{oidx}->eidx_meta($meta_key);
625 if (defined $last && is_ancestor($self->git, $last, $latest_cmt)) {
626 my @cmd = (qw(rev-list --count), "$last..$latest_cmt");
627 chomp(my $n = $unit->{git}->qx(@cmd));
628 return if $n ne '' && $n == 0;
630 $self->{oidx}->eidx_meta($meta_key, $latest_cmt);
633 sub _idx_init { # with_umask callback
634 my ($self, $opt) = @_;
635 PublicInbox::V2Writable::_idx_init($self, $opt);
636 $self->{midx} = PublicInbox::MiscIdx->new($self);
639 sub idx_init { # similar to V2Writable
640 my ($self, $opt) = @_;
641 return if $self->{idx_shards};
645 my $ALL = $self->git->{git_dir}; # ALL.git
646 PublicInbox::Import::init_bare($ALL) unless -d $ALL;
647 my $info_dir = "$ALL/objects/info";
648 my $alt = "$info_dir/alternates";
650 my (@old, @new, %seen); # seen: st_dev + st_ino
652 open(my $fh, '<', $alt) or die "open $alt: $!";
653 $mode = (stat($fh))[2] & 07777;
654 while (my $line = <$fh>) {
655 chomp(my $d = $line);
656 if (my @st = stat($d)) {
657 next if $seen{"$st[0]\0$st[1]"}++;
659 warn "W: stat($d) failed (from $alt): $!\n";
660 next if $opt->{-idx_gc};
665 for my $ibx (@{$self->{ibx_list}}) {
666 my $line = $ibx->git->{git_dir} . "/objects\n";
667 chomp(my $d = $line);
668 if (my @st = stat($d)) {
669 next if $seen{"$st[0]\0$st[1]"}++;
671 warn "W: stat($d) failed (from $ibx->{inboxdir}): $!\n";
672 next if $opt->{-idx_gc};
679 PublicInbox::V2Writable::write_alternates($info_dir, $mode, $o);
681 $self->parallel_init($self->{indexlevel});
682 $self->umask_prepare;
683 $self->with_umask(\&_idx_init, $self, $opt);
684 $self->{oidx}->begin_lazy;
685 $self->{oidx}->eidx_prep;
686 $self->{midx}->begin_txn;
690 *done = \&PublicInbox::V2Writable::done;
691 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
692 *with_umask = \&PublicInbox::InboxWritable::with_umask;
693 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
694 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
695 *sync_prepare = \&PublicInbox::V2Writable::sync_prepare;
696 *index_todo = \&PublicInbox::V2Writable::index_todo;
697 *count_shards = \&PublicInbox::V2Writable::count_shards;
698 *atfork_child = \&PublicInbox::V2Writable::atfork_child;
699 *idx_shard = \&PublicInbox::V2Writable::idx_shard;
700 *reindex_checkpoint = \&PublicInbox::V2Writable::reindex_checkpoint;