]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRemote.pm
lei blob: add remote external support
[public-inbox.git] / lib / PublicInbox / LeiRemote.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Make remote externals HTTP(S) inboxes behave like
5 # PublicInbox::Inbox and PublicInbox::Search/ExtSearch.
6 # This exists solely for SolverGit.  It is a high-latency a
7 # synchronous API that is not at all fast.
8 package PublicInbox::LeiRemote;
9 use v5.10.1;
10 use strict;
11 use IO::Uncompress::Gunzip;
12 use PublicInbox::OnDestroy;
13 use PublicInbox::MboxReader;
14 use PublicInbox::Spawn qw(popen_rd);
15 use PublicInbox::LeiCurl;
16 use PublicInbox::ContentHash qw(git_sha);
17
18 sub new {
19         my ($cls, $lei, $uri) = @_;
20         bless { uri => $uri, lei => $lei }, $cls;
21 }
22
23 sub isrch { $_[0] } # SolverGit expcets this
24
25 sub _each_mboxrd_eml { # callback for MboxReader->mboxrd
26         my ($eml, $self) = @_;
27         my $lei = $self->{lei};
28         my $xoids = $lei->{ale}->xoids_for($eml, 1);
29         if ($lei->{sto} && !$xoids) { # memoize locally
30                 $lei->{sto}->ipc_do('add_eml', $eml);
31         }
32         my $smsg = bless {}, 'PublicInbox::Smsg';
33         $smsg->{blob} = $xoids ? (keys(%$xoids))[0]
34                                 : git_sha(1, $eml)->hexdigest;
35         $smsg->populate($eml);
36         $smsg->{mid} //= '(none)';
37         push @{$self->{smsg}}, $smsg;
38 }
39
40 sub mset {
41         my ($self, $qstr, undef) = @_; # $opt ($_[2]) ignored
42         my $lei = $self->{lei};
43         my $curl = PublicInbox::LeiCurl->new($lei, $lei->{curl});
44         push @$curl, '-s', '-d', '';
45         my $uri = $self->{uri}->clone;
46         $uri->query_form(q => $qstr, x => 'm', r => 1); # r=1: relevance
47         my $cmd = $curl->for_uri($self->{lei}, $uri);
48         $self->{lei}->qerr("# $cmd");
49         my $rdr = { 2 => $lei->{2}, pgid => 0 };
50         my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
51         my $reap = PublicInbox::OnDestroy->new($lei->can('sigint_reap'), $pid);
52         $self->{smsg} = [];
53         $fh = IO::Uncompress::Gunzip->new($fh);
54         PublicInbox::MboxReader->mboxrd($fh, \&_each_mboxrd_eml, $self);
55         my $err = waitpid($pid, 0) == $pid ? undef
56                                         : "BUG: waitpid($cmd): $!";
57         @$reap = (); # cancel OnDestroy
58         my $wait = $self->{lei}->{sto}->ipc_do('done');
59         die $err if $err;
60         $self; # we are the mset (and $ibx, and $self)
61 }
62
63 sub size { scalar @{$_[0]->{smsg}} } # size of previous results
64
65 sub mset_to_smsg {
66         my ($self, $ibx, $mset) = @_; # all 3 are $self
67         wantarray ? ($self->size, @{$self->{smsg}}) : $self->{smsg};
68 }
69
70 sub base_url { "$_[0]->{uri}" }
71
72 sub smsg_eml {
73         my ($self, $smsg) = @_;
74         if (my $bref = $self->{lei}->ale->git->cat_file($smsg->{blob})) {
75                 return PublicInbox::Eml->new($bref);
76         }
77         $self->{lei}->err("E: $self->{uri} $smsg->{blob} gone <$smsg->{mid}>");
78         undef;
79 }
80
81 1;