]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiXSearch.pm
lei: normalize whitespace in remote queries
[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         bless $srch, 'PublicInbox::LeiSearch'; # for ->qparse_new
165         my $mo = { %{$lei->{mset_opt}} }; # copy
166         my $mset;
167         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
168         my $can_kw = !!$ibxish->can('msg_keywords');
169         my $threads = $lei->{opt}->{threads} // 0;
170         my $fl = $threads > 1 ? 1 : undef;
171         my $lss = $lei->{dedupe};
172         $lss = undef unless $lss && $lss->can('cfg_set'); # saved search
173         my $maxk = "external.$dir.maxuid";
174         my $stop_at = $lss ? $lss->{-cfg}->{$maxk} : undef;
175         if (defined $stop_at) {
176                 die "$maxk=$stop_at has multiple values" if ref $stop_at;
177                 my @e;
178                 local $SIG{__WARN__} = sub { push @e, @_ };
179                 $stop_at += 0;
180                 return warn("$maxk=$stop_at: @e") if @e;
181         }
182         my $first_ids;
183         do {
184                 $mset = $srch->mset($mo->{qstr}, $mo);
185                 mset_progress($lei, $dir, $mset->size,
186                                 $mset->get_matches_estimated);
187                 wait_startq($lei); # wait for keyword updates
188                 my $ids = $srch->mset_to_artnums($mset, $mo);
189                 @$ids = grep { $_ > $stop_at } @$ids if defined($stop_at);
190                 my $i = 0;
191                 if ($threads) {
192                         # copy $ids if $lss since over->expand_thread
193                         # shifts @{$ctx->{ids}}
194                         $first_ids = [ @$ids ] if $lss;
195                         my $ctx = { ids => $ids };
196                         my %n2item = map { ($ids->[$i++], $_) } $mset->items;
197                         while ($over->expand_thread($ctx)) {
198                                 for my $n (@{$ctx->{xids}}) {
199                                         my $smsg = $over->get_art($n) or next;
200                                         my $mitem = delete $n2item{$n};
201                                         next if $smsg->{bytes} == 0;
202                                         if ($mitem && $can_kw) {
203                                                 mitem_kw($srch, $smsg, $mitem,
204                                                         $fl);
205                                         } elsif ($mitem && $fl) {
206                                                 # call ->xsmsg_vmd, later
207                                                 $smsg->{lei_q_tt_flagged} = 1;
208                                         }
209                                         $each_smsg->($smsg, $mitem);
210                                 }
211                                 @{$ctx->{xids}} = ();
212                         }
213                 } else {
214                         $first_ids = $ids;
215                         my @items = $mset->items;
216                         for my $n (@$ids) {
217                                 my $mitem = $items[$i++];
218                                 my $smsg = $over->get_art($n) or next;
219                                 next if $smsg->{bytes} == 0;
220                                 mitem_kw($srch, $smsg, $mitem, $fl) if $can_kw;
221                                 $each_smsg->($smsg, $mitem);
222                         }
223                 }
224         } while (_mset_more($mset, $mo));
225         if ($lss && scalar(@$first_ids)) {
226                 undef $stop_at;
227                 my $max = $first_ids->[0];
228                 $lss->cfg_set($maxk, $max);
229                 undef $lss;
230         }
231         undef $each_smsg; # may commit
232         $lei->{ovv}->ovv_atexit_child($lei);
233 }
234
235 sub query_combined_mset { # non-parallel for non-"--threads" users
236         my ($self) = @_;
237         local $0 = "$0 query_combined_mset";
238         my $lei = $self->{lei};
239         my $mo = { %{$lei->{mset_opt}} };
240         my $mset;
241         for my $loc (locals($self)) {
242                 attach_external($self, $loc);
243         }
244         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
245         do {
246                 $mset = $self->mset($mo->{qstr}, $mo);
247                 mset_progress($lei, 'xsearch', $mset->size,
248                                 $mset->get_matches_estimated);
249                 wait_startq($lei); # wait for keyword updates
250                 for my $mitem ($mset->items) {
251                         my $smsg = smsg_for($self, $mitem) or next;
252                         $each_smsg->($smsg, $mitem);
253                 }
254         } while (_mset_more($mset, $mo));
255         undef $each_smsg; # may commit
256         $lei->{ovv}->ovv_atexit_child($lei);
257 }
258
259 sub _smsg_fill ($$) {
260         my ($smsg, $eml) = @_;
261         $smsg->populate($eml);
262         $smsg->parse_references($eml, mids($eml));
263         $smsg->{$_} //= '' for qw(from to cc ds subject references mid);
264         delete @$smsg{qw(From Subject -ds -ts)};
265 }
266
267 sub each_remote_eml { # callback for MboxReader->mboxrd
268         my ($eml, $self, $lei, $each_smsg) = @_;
269         my $xoids = $lei->{ale}->xoids_for($eml, 1);
270         my $smsg = bless {}, 'PublicInbox::Smsg';
271         if ($self->{import_sto} && !$xoids) {
272                 my $res = $self->{import_sto}->ipc_do('add_eml', $eml);
273                 if (ref($res) eq ref($smsg)) { # totally new message
274                         $smsg = $res;
275                         $smsg->{kw} = []; # short-circuit xsmsg_vmd
276                 }
277         }
278         $smsg->{blob} //= $xoids ? (keys(%$xoids))[0] : $lei->git_blob_id($eml);
279         _smsg_fill($smsg, $eml);
280         wait_startq($lei);
281         if ($lei->{-progress}) {
282                 ++$lei->{-nr_remote_eml};
283                 my $now = now();
284                 my $next = $lei->{-next_progress} //= ($now + 1);
285                 if ($now > $next) {
286                         $lei->{-next_progress} = $now + 1;
287                         my $nr = $lei->{-nr_remote_eml};
288                         mset_progress($lei, $lei->{-current_url}, $nr, '?');
289                 }
290         }
291         $each_smsg->($smsg, undef, $eml);
292 }
293
294 sub query_remote_mboxrd {
295         my ($self, $uris) = @_;
296         local $0 = "$0 query_remote_mboxrd";
297         local $SIG{TERM} = sub { exit(0) }; # for DESTROY (File::Temp, $reap)
298         my $lei = $self->{lei};
299         my $opt = $lei->{opt};
300         my $qstr = $lei->{mset_opt}->{qstr};
301         $qstr =~ s/[ \n\t]+/ /sg; # make URLs less ugly
302         my @qform = (q => $qstr, x => 'm');
303         push(@qform, t => 1) if $opt->{threads};
304         my $verbose = $opt->{verbose};
305         my ($reap_tail, $reap_curl);
306         my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
307         fcntl($cerr, F_SETFL, O_APPEND|O_RDWR) or warn "set O_APPEND: $!";
308         my $rdr = { 2 => $cerr, pgid => 0 };
309         my $sigint_reap = $lei->can('sigint_reap');
310         if ($verbose) {
311                 # spawn a process to force line-buffering, otherwise curl
312                 # will write 1 character at-a-time and parallel outputs
313                 # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
314                 my $o = { 1 => $lei->{2}, 2 => $lei->{2}, pgid => 0 };
315                 my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
316                 $reap_tail = PublicInbox::OnDestroy->new($sigint_reap, $pid);
317         }
318         my $curl = PublicInbox::LeiCurl->new($lei, $self->{curl}) or return;
319         push @$curl, '-s', '-d', '';
320         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
321         $self->{import_sto} = $lei->{sto} if $lei->{opt}->{'import-remote'};
322         for my $uri (@$uris) {
323                 $lei->{-current_url} = $uri->as_string;
324                 $lei->{-nr_remote_eml} = 0;
325                 $uri->query_form(@qform);
326                 my $cmd = $curl->for_uri($lei, $uri);
327                 $lei->qerr("# $cmd");
328                 my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
329                 $reap_curl = PublicInbox::OnDestroy->new($sigint_reap, $pid);
330                 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
331                 PublicInbox::MboxReader->mboxrd($fh, \&each_remote_eml, $self,
332                                                 $lei, $each_smsg);
333                 my $err = waitpid($pid, 0) == $pid ? undef
334                                                 : "BUG: waitpid($cmd): $!";
335                 @$reap_curl = (); # cancel OnDestroy
336                 die $err if $err;
337                 my $nr = $lei->{-nr_remote_eml};
338                 if ($nr && $lei->{sto}) {
339                         my $wait = $lei->{sto}->ipc_do('done');
340                 }
341                 if ($? == 0) {
342                         mset_progress($lei, $lei->{-current_url}, $nr, $nr);
343                         next;
344                 }
345                 $err = '';
346                 if (-s $cerr) {
347                         seek($cerr, 0, SEEK_SET) or
348                                         $lei->err("seek($cmd stderr): $!");
349                         $err = do { local $/; <$cerr> } //
350                                         "read($cmd stderr): $!";
351                         truncate($cerr, 0) or
352                                         $lei->err("truncate($cmd stderr): $!");
353                 }
354                 next if (($? >> 8) == 22 && $err =~ /\b404\b/);
355                 $uri->query_form(q => $lei->{mset_opt}->{qstr});
356                 $lei->child_error($?, "E: <$uri> $err");
357         }
358         undef $each_smsg;
359         $lei->{ovv}->ovv_atexit_child($lei);
360 }
361
362 sub git { $_[0]->{git} // die 'BUG: git uninitialized' }
363
364 sub xsearch_done_wait { # dwaitpid callback
365         my ($arg, $pid) = @_;
366         my ($wq, $lei) = @$arg;
367         $lei->child_error($?, 'non-fatal error from '.ref($wq)) if $?;
368 }
369
370 sub query_done { # EOF callback for main daemon
371         my ($lei) = @_;
372         my $l2m = delete $lei->{l2m};
373         $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
374         if (my $lxs = delete $lei->{lxs}) {
375                 $lxs->wq_wait_old(\&xsearch_done_wait, $lei);
376         }
377         if ($lei->{opt}->{'mail-sync'} && !$lei->{sto}) {
378                 warn "BUG: {sto} missing with --mail-sync";
379         }
380         $lei->sto_done_request if $lei->{sto};
381         my $wait = $lei->{v2w} ? $lei->{v2w}->ipc_do('done') : undef;
382         $lei->{ovv}->ovv_end($lei);
383         my $start_mua;
384         if ($l2m) { # close() calls LeiToMail reap_compress
385                 if (my $out = delete $lei->{old_1}) {
386                         if (my $mbout = $lei->{1}) {
387                                 close($mbout) or return $lei->fail(<<"");
388 Error closing $lei->{ovv}->{dst}: $!
389
390                         }
391                         $lei->{1} = $out;
392                 }
393                 if ($l2m->lock_free) {
394                         $l2m->poke_dst;
395                         $lei->poke_mua;
396                 } else { # mbox users
397                         delete $l2m->{mbl}; # drop dotlock
398                         $start_mua = 1;
399                 }
400         }
401         if ($lei->{-progress}) {
402                 my $tot = $lei->{-mset_total} // 0;
403                 my $nr = $lei->{-nr_write} // 0;
404                 if ($l2m) {
405                         my $m = "# $nr written to " .
406                                 "$lei->{ovv}->{dst} ($tot matches)";
407                         $nr ? $lei->qfin($m) : $lei->qerr($m);
408                 } else {
409                         $lei->qerr("# $tot matches");
410                 }
411         }
412         $lei->start_mua if $start_mua;
413         $lei->dclose;
414 }
415
416 sub do_post_augment {
417         my ($lei) = @_;
418         my $l2m = $lei->{l2m} or return; # client disconnected
419         $lei->fchdir or return;
420         my $err;
421         eval { $l2m->post_augment($lei) };
422         $err = $@;
423         if ($err) {
424                 if (my $lxs = delete $lei->{lxs}) {
425                         $lxs->wq_kill;
426                         $lxs->wq_close(0, undef, $lei);
427                 }
428                 $lei->fail("$err");
429         }
430         if (!$err && delete $lei->{early_mua}) { # non-augment case
431                 $lei->start_mua;
432         }
433         close(delete $lei->{au_done}); # triggers wait_startq in lei_xsearch
434 }
435
436 sub incr_post_augment { # called whenever an l2m shard finishes augment
437         my ($lei) = @_;
438         my $l2m = $lei->{l2m} or return; # client disconnected
439         return if ++$lei->{nr_post_augment} != $l2m->{-wq_nr_workers};
440         do_post_augment($lei);
441 }
442
443 my $MAX_PER_HOST = 4;
444
445 sub concurrency {
446         my ($self, $opt) = @_;
447         my $nl = $opt->{threads} ? locals($self) : 1;
448         my $nr = remotes($self);
449         $nr = $MAX_PER_HOST if $nr > $MAX_PER_HOST;
450         $nl + $nr;
451 }
452
453 sub start_query ($;$) { # always runs in main (lei-daemon) process
454         my ($self, $l2m) = @_;
455         if ($self->{opt_threads} || ($l2m && !$self->{opt_sort})) {
456                 for my $ibxish (locals($self)) {
457                         $self->wq_io_do('query_one_mset', [], $ibxish);
458                 }
459         } elsif (locals($self)) {
460                 $self->wq_io_do('query_combined_mset', []);
461         }
462         my $i = 0;
463         my $q = [];
464         for my $uri (remotes($self)) {
465                 push @{$q->[$i++ % $MAX_PER_HOST]}, $uri;
466         }
467         for my $uris (@$q) {
468                 $self->wq_io_do('query_remote_mboxrd', [], $uris);
469         }
470         if ($self->{-do_lcat}) {
471                 $self->wq_io_do('lcat_dump', []);
472         }
473         $self->wq_close(1); # lei_xsearch workers stop when done
474 }
475
476 sub incr_start_query { # called whenever an l2m shard starts do_post_auth
477         my ($self, $l2m) = @_;
478         return if ++$self->{nr_start_query} != $l2m->{-wq_nr_workers};
479         start_query($self, $l2m);
480 }
481
482 sub ipc_atfork_child {
483         my ($self) = @_;
484         $self->{lei}->_lei_atfork_child;
485         $self->SUPER::ipc_atfork_child;
486 }
487
488 sub do_query {
489         my ($self, $lei) = @_;
490         my $l2m = $lei->{l2m};
491         my $ops = {
492                 '|' => [ $lei->can('sigpipe_handler'), $lei ],
493                 '!' => [ $lei->can('fail_handler'), $lei ],
494                 '.' => [ \&do_post_augment, $lei ],
495                 '+' => [ \&incr_post_augment, $lei ],
496                 '' => [ \&query_done, $lei ],
497                 'mset_progress' => [ \&mset_progress, $lei ],
498                 'l2m_progress' => [ \&l2m_progress, $lei ],
499                 'x_it' => [ $lei->can('x_it'), $lei ],
500                 'child_error' => [ $lei->can('child_error'), $lei ],
501                 'incr_start_query' => [ \&incr_start_query, $self, $l2m ],
502         };
503         $lei->{auth}->op_merge($ops, $l2m) if $l2m && $lei->{auth};
504         my $end = $lei->pkt_op_pair;
505         $lei->{1}->autoflush(1);
506         $lei->start_pager if delete $lei->{need_pager};
507         $lei->{ovv}->ovv_begin($lei);
508         die 'BUG: xdb|over open' if $lei->{lse}->{xdb} || $lei->{lse}->{over};
509         if ($l2m) {
510                 $l2m->pre_augment($lei);
511                 if ($lei->{opt}->{augment} && delete $lei->{early_mua}) {
512                         $lei->start_mua;
513                 }
514                 my $F_SETPIPE_SZ = $^O eq 'linux' ? 1031 : undef;
515                 if ($l2m->{-wq_nr_workers} > 1 &&
516                                 $l2m->{base_type} =~ /\A(?:maildir|mbox)\z/) {
517                         # setup two barriers to coordinate ->has_entries
518                         # between l2m workers
519                         pipe(my ($a_r, $a_w)) or die "pipe: $!";
520                         fcntl($a_r, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
521                         pipe(my ($b_r, $b_w)) or die "pipe: $!";
522                         fcntl($b_r, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
523                         $l2m->{au_peers} = [ $a_r, $a_w, $b_r, $b_w ];
524                 }
525                 $l2m->wq_workers_start('lei2mail', undef,
526                                         $lei->oldset, { lei => $lei });
527                 pipe($lei->{startq}, $lei->{au_done}) or die "pipe: $!";
528                 fcntl($lei->{startq}, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
529                 delete $l2m->{au_peers};
530         }
531         $self->wq_workers_start('lei_xsearch', undef,
532                                 $lei->oldset, { lei => $lei });
533         my $op_c = delete $lei->{pkt_op_c};
534         delete $lei->{pkt_op_p};
535         @$end = ();
536         $self->{opt_threads} = $lei->{opt}->{threads};
537         $self->{opt_sort} = $lei->{opt}->{'sort'};
538         $self->{-do_lcat} = $lei->{lcat_blob} // $lei->{lcat_fid};
539         if ($l2m) {
540                 $l2m->net_merge_all_done unless $lei->{auth};
541         } else {
542                 start_query($self);
543         }
544         $lei->event_step_init; # wait for shutdowns
545         $lei->wait_wq_events($op_c, $ops);
546 }
547
548 sub add_uri {
549         my ($self, $uri) = @_;
550         if (my $curl = $self->{curl} //= which('curl') // 0) {
551                 require PublicInbox::MboxReader;
552                 require IO::Uncompress::Gunzip;
553                 require PublicInbox::LeiCurl;
554                 push @{$self->{remotes}}, $uri;
555         } else {
556                 warn "curl missing, ignoring $uri\n";
557         }
558 }
559
560 sub prepare_external {
561         my ($self, $loc, $boost) = @_; # n.b. already ordered by boost
562         if (ref $loc) { # already a URI, or PublicInbox::Inbox-like object
563                 return add_uri($self, $loc) if $loc->can('scheme');
564         } elsif ($loc =~ m!\Ahttps?://!) {
565                 require URI;
566                 return add_uri($self, URI->new($loc));
567         } elsif (-f "$loc/ei.lock") {
568                 require PublicInbox::ExtSearch;
569                 die "`\\n' not allowed in `$loc'\n" if index($loc, "\n") >= 0;
570                 $loc = PublicInbox::ExtSearch->new($loc);
571         } elsif (-f "$loc/inbox.lock" || -d "$loc/public-inbox") {
572                 die "`\\n' not allowed in `$loc'\n" if index($loc, "\n") >= 0;
573                 require PublicInbox::Inbox; # v2, v1
574                 $loc = bless { inboxdir => $loc }, 'PublicInbox::Inbox';
575         } else {
576                 warn "W: ignoring $loc, unable to determine type\n";
577                 return;
578         }
579         push @{$self->{locals}}, $loc;
580 }
581
582 sub _lcat_i { # LeiMailSync->each_src iterator callback
583         my ($oidbin, $id, $each_smsg) = @_;
584         $each_smsg->({blob => unpack('H*', $oidbin), pct => 100});
585 }
586
587 sub _lcat2smsg { # git->cat_async callback
588         my ($bref, $oid, $type, $size, $smsg) = @_;
589         if ($bref) {
590                 my $eml = PublicInbox::Eml->new($bref);
591                 my $json_dump = delete $smsg->{-json_dump};
592                 bless $smsg, 'PublicInbox::Smsg';
593                 _smsg_fill($smsg, $eml);
594                 $json_dump->($smsg, undef, $eml);
595         }
596 }
597
598 sub lcat_dump {
599         my ($self) = @_;
600         my $lei = $self->{lei};
601         my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
602         my $git = $lei->{ale}->git;
603         if (!$lei->{l2m}) {
604                 my $json_dump = $each_smsg;
605                 $each_smsg = sub {
606                         my ($smsg) = @_;
607                         use Data::Dumper;
608                         $smsg->{-json_dump} = $json_dump;
609                         $git->cat_async($smsg->{blob}, \&_lcat2smsg, $smsg);
610                 };
611         }
612         for my $oid (@{$lei->{lcat_blob} // []}) {
613                 $each_smsg->({ blob => $oid, pct => 100 });
614         }
615         if (my $fids = delete $lei->{lcat_fid}) {
616                 my $lms = $lei->{lse}->lms;
617                 for my $fid (@$fids) {
618                         $lms->each_src({fid => $fid}, \&_lcat_i, $each_smsg);
619                 }
620         }
621         $git->async_wait_all;
622         undef $each_smsg; # may commit
623         $lei->{ovv}->ovv_atexit_child($lei);
624 }
625
626 1;