]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei up: improve --all=local stderr output
[public-inbox.git] / lib / PublicInbox / LeiXSearch.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Combine any combination of PublicInbox::Search,
5 # PublicInbox::ExtSearch, and PublicInbox::LeiSearch objects
6 # into one Xapian DB
7 package PublicInbox::LeiXSearch;
8 use strict;
9 use v5.10.1;
10 use parent qw(PublicInbox::LeiSearch PublicInbox::IPC);
11 use PublicInbox::DS qw(now);
12 use File::Temp 0.19 (); # 0.19 for ->newdir
13 use File::Spec ();
14 use PublicInbox::Search qw(xap_terms);
15 use PublicInbox::Spawn qw(popen_rd spawn which);
16 use PublicInbox::MID qw(mids);
17 use PublicInbox::Smsg;
18 use PublicInbox::Eml;
19 use Fcntl qw(SEEK_SET F_SETFL O_APPEND O_RDWR);
20 use PublicInbox::ContentHash qw(git_sha);
21
22 sub new {
23         my ($class) = @_;
24         PublicInbox::Search::load_xapian();
25         bless {
26                 qp_flags => $PublicInbox::Search::QP_FLAGS |
27                                 PublicInbox::Search::FLAG_PURE_NOT(),
28         }, $class
29 }
30
31 sub attach_external {
32         my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
33         my $desc = $ibxish->{inboxdir} // $ibxish->{topdir};
34         my $srch = $ibxish->search or
35                 return warn("$desc not indexed for Xapian ($@ $!)\n");
36         my @shards = $srch->xdb_shards_flat or
37                 return warn("$desc has no Xapian shards\n");
38
39         if (delete $self->{xdb}) { # XXX: do we need this?
40                 # clobber existing {xdb} if amending
41                 my $expect = delete $self->{nshard};
42                 my $shards = delete $self->{shards_flat};
43                 scalar(@$shards) == $expect or die
44                         "BUG: {nshard}$expect != shards=".scalar(@$shards);
45
46                 my $prev = {};
47                 for my $old_ibxish (@{$self->{shard2ibx}}) {
48                         next if $prev == $old_ibxish;
49                         $prev = $old_ibxish;
50                         my @shards = $old_ibxish->search->xdb_shards_flat;
51                         push @{$self->{shards_flat}}, @shards;
52                 }
53                 my $nr = scalar(@{$self->{shards_flat}});
54                 $nr == $expect or die
55                         "BUG: reloaded $nr shards, expected $expect"
56         }
57         push @{$self->{shards_flat}}, @shards;
58         push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
59 }
60
61 # returns a list of local inboxes (or count in scalar context)
62 sub locals { @{$_[0]->{locals} // []} }
63
64 sub remotes { @{$_[0]->{remotes} // []} }
65
66 # called by PublicInbox::Search::xdb (usually via ->mset)
67 sub xdb_shards_flat { @{$_[0]->{shards_flat} // []} }
68
69 sub _mitem_kw { # retry_reopen callback
70         my ($srch, $smsg, $mitem, $flagged) = @_;
71         my $doc = $mitem->get_document;
72         my $kw = xap_terms('K', $doc);
73         $kw->{flagged} = 1 if $flagged;
74         my @L = xap_terms('L', $doc);
75         # we keep the empty {kw} array here to prevent expensive work in
76         # ->xsmsg_vmd, _unbless_smsg will clobber it iff it's empty
77         $smsg->{kw} = [ sort keys %$kw ];
78         $smsg->{L} = \@L if scalar(@L);
79 }
80
81 sub mitem_kw ($$$;$) {
82         my ($srch, $smsg, $mitem, $flagged) = @_;
83         $srch->retry_reopen(\&_mitem_kw, $smsg, $mitem, $flagged);
84 }
85
86 # like over->get_art
87 sub smsg_for {
88         my ($self, $mitem) = @_;
89         # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
90         my $nshard = $self->{nshard};
91         my $docid = $mitem->get_docid;
92         my $shard = ($docid - 1) % $nshard;
93         my $num = int(($docid - 1) / $nshard) + 1;
94         my $ibx = $self->{shard2ibx}->[$shard];
95         my $smsg = $ibx->over->get_art($num);
96         return if $smsg->{bytes} == 0; # external message
97         if ($ibx->can('msg_keywords')) {
98                 mitem_kw($self, $smsg, $mitem);
99         }
100         $smsg;
101 }
102
103 sub recent {
104         my ($self, $qstr, $opt) = @_;
105         $opt //= {};
106         $opt->{relevance} //= -2;
107         $self->mset($qstr //= 'z:1..', $opt);
108 }
109
110 sub over {}
111
112 sub _mset_more ($$) {
113         my ($mset, $mo) = @_;
114         my $size = $mset->size;
115         $size >= $mo->{limit} && (($mo->{offset} += $size) < $mo->{limit});
116 }
117
118 # $startq will EOF when do_augment is done augmenting and allow
119 # query_combined_mset and query_thread_mset to proceed.
120 sub wait_startq ($) {
121         my ($lei) = @_;
122         my $startq = delete $lei->{startq} or return;
123         while (1) {
124                 my $n = sysread($startq, my $do_augment_done, 1);
125                 if (defined $n) {
126                         return if $n == 0; # no MUA
127                         if ($do_augment_done eq 'q') {
128                                 $lei->{opt}->{quiet} = 1;
129                                 delete $lei->{opt}->{verbose};
130                                 delete $lei->{-progress};
131                         } else {
132                                 $lei->fail("$$ WTF `$do_augment_done'");
133                         }
134                         return;
135                 }
136                 return $lei->fail("$$ wait_startq: $!") unless $!{EINTR};
137         }
138 }
139
140 sub mset_progress {
141         my $lei = shift;
142         return if $lei->{early_mua} || !$lei->{-progress};
143         if ($lei->{pkt_op_p}) {
144                 $lei->{pkt_op_p}->pkt_do('mset_progress', @_);
145         } else { # single lei-daemon consumer
146                 my ($desc, $mset_size, $mset_total_est) = @_;
147                 $lei->{-mset_total} += $mset_size if $mset_total_est ne '?';
148                 $lei->qerr("# $desc $mset_size/$mset_total_est");
149         }
150 }
151
152 sub l2m_progress {
153         my ($lei, $nr) = @_;
154         $lei->{-nr_write} += $nr;
155 }
156
157 sub query_one_mset { # for --threads and l2m w/o sort
158         my ($self, $ibxish) = @_;
159         local $0 = "$0 query_one_mset";
160         my $lei = $self->{lei};
161         my ($srch, $over) = ($ibxish->search, $ibxish->over);
162         my $dir = $ibxish->{inboxdir} // $ibxish->{topdir};
163         return warn("$dir not indexed by Xapian\n") unless ($srch && $over);
164         my $mo = { %{$lei->{mset_opt}} }; # copy
165         my $mset;
166         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
167         my $can_kw = !!$ibxish->can('msg_keywords');
168         my $threads = $lei->{opt}->{threads} // 0;
169         my $fl = $threads > 1 ? 1 : undef;
170         my $lss = $lei->{dedupe};
171         $lss = undef unless $lss && $lss->can('cfg_set'); # saved search
172         my $maxk = "external.$dir.maxuid";
173         my $stop_at = $lss ? $lss->{-cfg}->{$maxk} : undef;
174         if (defined $stop_at) {
175                 die "$maxk=$stop_at has multiple values" if ref $stop_at;
176                 my @e;
177                 local $SIG{__WARN__} = sub { push @e, @_ };
178                 $stop_at += 0;
179                 return warn("$maxk=$stop_at: @e") if @e;
180         }
181         my $first_ids;
182         do {
183                 $mset = $srch->mset($mo->{qstr}, $mo);
184                 mset_progress($lei, $dir, $mset->size,
185                                 $mset->get_matches_estimated);
186                 wait_startq($lei); # wait for keyword updates
187                 my $ids = $srch->mset_to_artnums($mset, $mo);
188                 @$ids = grep { $_ > $stop_at } @$ids if defined($stop_at);
189                 my $i = 0;
190                 if ($threads) {
191                         # copy $ids if $lss since over->expand_thread
192                         # shifts @{$ctx->{ids}}
193                         $first_ids = [ @$ids ] if $lss;
194                         my $ctx = { ids => $ids };
195                         my %n2item = map { ($ids->[$i++], $_) } $mset->items;
196                         while ($over->expand_thread($ctx)) {
197                                 for my $n (@{$ctx->{xids}}) {
198                                         my $smsg = $over->get_art($n) or next;
199                                         my $mitem = delete $n2item{$n};
200                                         next if $smsg->{bytes} == 0;
201                                         if ($mitem && $can_kw) {
202                                                 mitem_kw($srch, $smsg, $mitem,
203                                                         $fl);
204                                         } elsif ($mitem && $fl) {
205                                                 # call ->xsmsg_vmd, later
206                                                 $smsg->{lei_q_tt_flagged} = 1;
207                                         }
208                                         $each_smsg->($smsg, $mitem);
209                                 }
210                                 @{$ctx->{xids}} = ();
211                         }
212                 } else {
213                         $first_ids = $ids;
214                         my @items = $mset->items;
215                         for my $n (@$ids) {
216                                 my $mitem = $items[$i++];
217                                 my $smsg = $over->get_art($n) or next;
218                                 next if $smsg->{bytes} == 0;
219                                 mitem_kw($srch, $smsg, $mitem, $fl) if $can_kw;
220                                 $each_smsg->($smsg, $mitem);
221                         }
222                 }
223         } while (_mset_more($mset, $mo));
224         if ($lss && scalar(@$first_ids)) {
225                 undef $stop_at;
226                 my $max = $first_ids->[0];
227                 $lss->cfg_set($maxk, $max);
228                 undef $lss;
229         }
230         undef $each_smsg; # may commit
231         $lei->{ovv}->ovv_atexit_child($lei);
232 }
233
234 sub query_combined_mset { # non-parallel for non-"--threads" users
235         my ($self) = @_;
236         local $0 = "$0 query_combined_mset";
237         my $lei = $self->{lei};
238         my $mo = { %{$lei->{mset_opt}} };
239         my $mset;
240         for my $loc (locals($self)) {
241                 attach_external($self, $loc);
242         }
243         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
244         do {
245                 $mset = $self->mset($mo->{qstr}, $mo);
246                 mset_progress($lei, 'xsearch', $mset->size,
247                                 $mset->get_matches_estimated);
248                 wait_startq($lei); # wait for keyword updates
249                 for my $mitem ($mset->items) {
250                         my $smsg = smsg_for($self, $mitem) or next;
251                         $each_smsg->($smsg, $mitem);
252                 }
253         } while (_mset_more($mset, $mo));
254         undef $each_smsg; # may commit
255         $lei->{ovv}->ovv_atexit_child($lei);
256 }
257
258 sub _smsg_fill ($$) {
259         my ($smsg, $eml) = @_;
260         $smsg->populate($eml);
261         $smsg->parse_references($eml, mids($eml));
262         $smsg->{$_} //= '' for qw(from to cc ds subject references mid);
263         delete @$smsg{qw(From Subject -ds -ts)};
264 }
265
266 sub each_remote_eml { # callback for MboxReader->mboxrd
267         my ($eml, $self, $lei, $each_smsg) = @_;
268         my $xoids = $lei->{ale}->xoids_for($eml, 1);
269         my $smsg = bless {}, 'PublicInbox::Smsg';
270         if ($self->{import_sto} && !$xoids) {
271                 my $res = $self->{import_sto}->ipc_do('add_eml', $eml);
272                 if (ref($res) eq ref($smsg)) { # totally new message
273                         $smsg = $res;
274                         $smsg->{kw} = []; # short-circuit xsmsg_vmd
275                 }
276         }
277         $smsg->{blob} //= $xoids ? (keys(%$xoids))[0] : $lei->git_blob_id($eml);
278         _smsg_fill($smsg, $eml);
279         wait_startq($lei);
280         if ($lei->{-progress}) {
281                 ++$lei->{-nr_remote_eml};
282                 my $now = now();
283                 my $next = $lei->{-next_progress} //= ($now + 1);
284                 if ($now > $next) {
285                         $lei->{-next_progress} = $now + 1;
286                         my $nr = $lei->{-nr_remote_eml};
287                         mset_progress($lei, $lei->{-current_url}, $nr, '?');
288                 }
289         }
290         $each_smsg->($smsg, undef, $eml);
291 }
292
293 sub query_remote_mboxrd {
294         my ($self, $uris) = @_;
295         local $0 = "$0 query_remote_mboxrd";
296         local $SIG{TERM} = sub { exit(0) }; # for DESTROY (File::Temp, $reap)
297         my $lei = $self->{lei};
298         my $opt = $lei->{opt};
299         my @qform = (q => $lei->{mset_opt}->{qstr}, x => 'm');
300         push(@qform, t => 1) if $opt->{threads};
301         my $verbose = $opt->{verbose};
302         my ($reap_tail, $reap_curl);
303         my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
304         fcntl($cerr, F_SETFL, O_APPEND|O_RDWR) or warn "set O_APPEND: $!";
305         my $rdr = { 2 => $cerr, pgid => 0 };
306         my $sigint_reap = $lei->can('sigint_reap');
307         if ($verbose) {
308                 # spawn a process to force line-buffering, otherwise curl
309                 # will write 1 character at-a-time and parallel outputs
310                 # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
311                 my $o = { 1 => $lei->{2}, 2 => $lei->{2}, pgid => 0 };
312                 my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
313                 $reap_tail = PublicInbox::OnDestroy->new($sigint_reap, $pid);
314         }
315         my $curl = PublicInbox::LeiCurl->new($lei, $self->{curl}) or return;
316         push @$curl, '-s', '-d', '';
317         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
318         $self->{import_sto} = $lei->{sto} if $lei->{opt}->{'import-remote'};
319         for my $uri (@$uris) {
320                 $lei->{-current_url} = $uri->as_string;
321                 $lei->{-nr_remote_eml} = 0;
322                 $uri->query_form(@qform);
323                 my $cmd = $curl->for_uri($lei, $uri);
324                 $lei->qerr("# $cmd");
325                 my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
326                 $reap_curl = PublicInbox::OnDestroy->new($sigint_reap, $pid);
327                 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
328                 PublicInbox::MboxReader->mboxrd($fh, \&each_remote_eml, $self,
329                                                 $lei, $each_smsg);
330                 my $err = waitpid($pid, 0) == $pid ? undef
331                                                 : "BUG: waitpid($cmd): $!";
332                 @$reap_curl = (); # cancel OnDestroy
333                 die $err if $err;
334                 my $nr = $lei->{-nr_remote_eml};
335                 if ($nr && $lei->{sto}) {
336                         my $wait = $lei->{sto}->ipc_do('done');
337                 }
338                 if ($? == 0) {
339                         mset_progress($lei, $lei->{-current_url}, $nr, $nr);
340                         next;
341                 }
342                 $err = '';
343                 if (-s $cerr) {
344                         seek($cerr, 0, SEEK_SET) or
345                                         $lei->err("seek($cmd stderr): $!");
346                         $err = do { local $/; <$cerr> } //
347                                         "read($cmd stderr): $!";
348                         truncate($cerr, 0) or
349                                         $lei->err("truncate($cmd stderr): $!");
350                 }
351                 next if (($? >> 8) == 22 && $err =~ /\b404\b/);
352                 $uri->query_form(q => $lei->{mset_opt}->{qstr});
353                 $lei->child_error($?, "E: <$uri> $err");
354         }
355         undef $each_smsg;
356         $lei->{ovv}->ovv_atexit_child($lei);
357 }
358
359 sub git { $_[0]->{git} // die 'BUG: git uninitialized' }
360
361 sub xsearch_done_wait { # dwaitpid callback
362         my ($arg, $pid) = @_;
363         my ($wq, $lei) = @$arg;
364         $lei->child_error($?, 'non-fatal error from '.ref($wq)) if $?;
365 }
366
367 sub query_done { # EOF callback for main daemon
368         my ($lei) = @_;
369         my $l2m = delete $lei->{l2m};
370         $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
371         if (my $lxs = delete $lei->{lxs}) {
372                 $lxs->wq_wait_old(\&xsearch_done_wait, $lei);
373         }
374         if ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) {
375                 warn "BUG: {sto} missing with --mail-sync";
376         }
377         $lei->sto_done_request if $lei->{sto};
378         my $wait = $lei->{v2w} ? $lei->{v2w}->ipc_do('done') : undef;
379         $lei->{ovv}->ovv_end($lei);
380         my $start_mua;
381         if ($l2m) { # close() calls LeiToMail reap_compress
382                 if (my $out = delete $lei->{old_1}) {
383                         if (my $mbout = $lei->{1}) {
384                                 close($mbout) or return $lei->fail(<<"");
385 Error closing $lei->{ovv}->{dst}: $!
386
387                         }
388                         $lei->{1} = $out;
389                 }
390                 if ($l2m->lock_free) {
391                         $l2m->poke_dst;
392                         $lei->poke_mua;
393                 } else { # mbox users
394                         delete $l2m->{mbl}; # drop dotlock
395                         $start_mua = 1;
396                 }
397         }
398         if ($lei->{-progress}) {
399                 my $tot = $lei->{-mset_total} // 0;
400                 my $nr = $lei->{-nr_write} // 0;
401                 if ($l2m) {
402                         $lei->qfin("# $nr written to " .
403                                 "$lei->{ovv}->{dst} ($tot matches)");
404                 } else {
405                         $lei->qerr("# $tot matches");
406                 }
407         }
408         $lei->start_mua if $start_mua;
409         $lei->dclose;
410 }
411
412 sub do_post_augment {
413         my ($lei) = @_;
414         my $l2m = $lei->{l2m} or return; # client disconnected
415         $lei->fchdir or return;
416         my $err;
417         eval { $l2m->post_augment($lei) };
418         $err = $@;
419         if ($err) {
420                 if (my $lxs = delete $lei->{lxs}) {
421                         $lxs->wq_kill;
422                         $lxs->wq_close(0, undef, $lei);
423                 }
424                 $lei->fail("$err");
425         }
426         if (!$err && delete $lei->{early_mua}) { # non-augment case
427                 $lei->start_mua;
428         }
429         close(delete $lei->{au_done}); # triggers wait_startq in lei_xsearch
430 }
431
432 sub incr_post_augment { # called whenever an l2m shard finishes augment
433         my ($lei) = @_;
434         my $l2m = $lei->{l2m} or return; # client disconnected
435         return if ++$lei->{nr_post_augment} != $l2m->{-wq_nr_workers};
436         do_post_augment($lei);
437 }
438
439 my $MAX_PER_HOST = 4;
440
441 sub concurrency {
442         my ($self, $opt) = @_;
443         my $nl = $opt->{threads} ? locals($self) : 1;
444         my $nr = remotes($self);
445         $nr = $MAX_PER_HOST if $nr > $MAX_PER_HOST;
446         $nl + $nr;
447 }
448
449 sub start_query ($;$) { # always runs in main (lei-daemon) process
450         my ($self, $l2m) = @_;
451         if ($self->{opt_threads} || ($l2m && !$self->{opt_sort})) {
452                 for my $ibxish (locals($self)) {
453                         $self->wq_io_do('query_one_mset', [], $ibxish);
454                 }
455         } elsif (locals($self)) {
456                 $self->wq_io_do('query_combined_mset', []);
457         }
458         my $i = 0;
459         my $q = [];
460         for my $uri (remotes($self)) {
461                 push @{$q->[$i++ % $MAX_PER_HOST]}, $uri;
462         }
463         for my $uris (@$q) {
464                 $self->wq_io_do('query_remote_mboxrd', [], $uris);
465         }
466         if ($self->{-do_lcat}) {
467                 $self->wq_io_do('lcat_dump', []);
468         }
469         $self->wq_close(1); # lei_xsearch workers stop when done
470 }
471
472 sub incr_start_query { # called whenever an l2m shard starts do_post_auth
473         my ($self, $l2m) = @_;
474         return if ++$self->{nr_start_query} != $l2m->{-wq_nr_workers};
475         start_query($self, $l2m);
476 }
477
478 sub ipc_atfork_child {
479         my ($self) = @_;
480         $self->{lei}->_lei_atfork_child;
481         $self->SUPER::ipc_atfork_child;
482 }
483
484 sub do_query {
485         my ($self, $lei) = @_;
486         my $l2m = $lei->{l2m};
487         my $ops = {
488                 '|' => [ $lei->can('sigpipe_handler'), $lei ],
489                 '!' => [ $lei->can('fail_handler'), $lei ],
490                 '.' => [ \&do_post_augment, $lei ],
491                 '+' => [ \&incr_post_augment, $lei ],
492                 '' => [ \&query_done, $lei ],
493                 'mset_progress' => [ \&mset_progress, $lei ],
494                 'l2m_progress' => [ \&l2m_progress, $lei ],
495                 'x_it' => [ $lei->can('x_it'), $lei ],
496                 'child_error' => [ $lei->can('child_error'), $lei ],
497                 'incr_start_query' => [ \&incr_start_query, $self, $l2m ],
498         };
499         $lei->{auth}->op_merge($ops, $l2m) if $l2m && $lei->{auth};
500         my $end = $lei->pkt_op_pair;
501         $lei->{1}->autoflush(1);
502         $lei->start_pager if delete $lei->{need_pager};
503         $lei->{ovv}->ovv_begin($lei);
504         die 'BUG: xdb|over open' if $lei->{lse}->{xdb} || $lei->{lse}->{over};
505         if ($l2m) {
506                 $l2m->pre_augment($lei);
507                 if ($lei->{opt}->{augment} && delete $lei->{early_mua}) {
508                         $lei->start_mua;
509                 }
510                 my $F_SETPIPE_SZ = $^O eq 'linux' ? 1031 : undef;
511                 if ($l2m->{-wq_nr_workers} > 1 &&
512                                 $l2m->{base_type} =~ /\A(?:maildir|mbox)\z/) {
513                         # setup two barriers to coordinate ->has_entries
514                         # between l2m workers
515                         pipe(my ($a_r, $a_w)) or die "pipe: $!";
516                         fcntl($a_r, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
517                         pipe(my ($b_r, $b_w)) or die "pipe: $!";
518                         fcntl($b_r, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
519                         $l2m->{au_peers} = [ $a_r, $a_w, $b_r, $b_w ];
520                 }
521                 $l2m->wq_workers_start('lei2mail', undef,
522                                         $lei->oldset, { lei => $lei });
523                 pipe($lei->{startq}, $lei->{au_done}) or die "pipe: $!";
524                 fcntl($lei->{startq}, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
525                 delete $l2m->{au_peers};
526         }
527         $self->wq_workers_start('lei_xsearch', undef,
528                                 $lei->oldset, { lei => $lei });
529         my $op_c = delete $lei->{pkt_op_c};
530         delete $lei->{pkt_op_p};
531         @$end = ();
532         $self->{opt_threads} = $lei->{opt}->{threads};
533         $self->{opt_sort} = $lei->{opt}->{'sort'};
534         $self->{-do_lcat} = $lei->{lcat_blob} // $lei->{lcat_fid};
535         if ($l2m) {
536                 $l2m->net_merge_all_done unless $lei->{auth};
537         } else {
538                 start_query($self);
539         }
540         $lei->event_step_init; # wait for shutdowns
541         $lei->wait_wq_events($op_c, $ops);
542 }
543
544 sub add_uri {
545         my ($self, $uri) = @_;
546         if (my $curl = $self->{curl} //= which('curl') // 0) {
547                 require PublicInbox::MboxReader;
548                 require IO::Uncompress::Gunzip;
549                 require PublicInbox::LeiCurl;
550                 push @{$self->{remotes}}, $uri;
551         } else {
552                 warn "curl missing, ignoring $uri\n";
553         }
554 }
555
556 sub prepare_external {
557         my ($self, $loc, $boost) = @_; # n.b. already ordered by boost
558         if (ref $loc) { # already a URI, or PublicInbox::Inbox-like object
559                 return add_uri($self, $loc) if $loc->can('scheme');
560         } elsif ($loc =~ m!\Ahttps?://!) {
561                 require URI;
562                 return add_uri($self, URI->new($loc));
563         } elsif (-f "$loc/ei.lock") {
564                 require PublicInbox::ExtSearch;
565                 die "`\\n' not allowed in `$loc'\n" if index($loc, "\n") >= 0;
566                 $loc = PublicInbox::ExtSearch->new($loc);
567         } elsif (-f "$loc/inbox.lock" || -d "$loc/public-inbox") {
568                 die "`\\n' not allowed in `$loc'\n" if index($loc, "\n") >= 0;
569                 require PublicInbox::Inbox; # v2, v1
570                 $loc = bless { inboxdir => $loc }, 'PublicInbox::Inbox';
571         } else {
572                 warn "W: ignoring $loc, unable to determine type\n";
573                 return;
574         }
575         push @{$self->{locals}}, $loc;
576 }
577
578 sub _lcat_i { # LeiMailSync->each_src iterator callback
579         my ($oidbin, $id, $each_smsg) = @_;
580         $each_smsg->({blob => unpack('H*', $oidbin), pct => 100});
581 }
582
583 sub _lcat2smsg { # git->cat_async callback
584         my ($bref, $oid, $type, $size, $smsg) = @_;
585         if ($bref) {
586                 my $eml = PublicInbox::Eml->new($bref);
587                 my $json_dump = delete $smsg->{-json_dump};
588                 bless $smsg, 'PublicInbox::Smsg';
589                 _smsg_fill($smsg, $eml);
590                 $json_dump->($smsg, undef, $eml);
591         }
592 }
593
594 sub lcat_dump {
595         my ($self) = @_;
596         my $lei = $self->{lei};
597         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
598         my $git = $lei->{ale}->git;
599         if (!$lei->{l2m}) {
600                 my $json_dump = $each_smsg;
601                 $each_smsg = sub {
602                         my ($smsg) = @_;
603                         use Data::Dumper;
604                         $smsg->{-json_dump} = $json_dump;
605                         $git->cat_async($smsg->{blob}, \&_lcat2smsg, $smsg);
606                 };
607         }
608         for my $oid (@{$lei->{lcat_blob} // []}) {
609                 $each_smsg->({ blob => $oid, pct => 100 });
610         }
611         if (my $fids = delete $lei->{lcat_fid}) {
612                 my $lms = $lei->{lse}->lms;
613                 for my $fid (@$fids) {
614                         $lms->each_src({fid => $fid}, \&_lcat_i, $each_smsg);
615                 }
616         }
617         $git->async_wait_all;
618         undef $each_smsg; # may commit
619         $lei->{ovv}->ovv_atexit_child($lei);
620 }
621
622 1;