]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei q: auto-memoize remote messages into lei/store
authorEric Wong <e@80x24.org>
Wed, 24 Feb 2021 23:37:18 +0000 (05:37 +0600)
committerEric Wong <e@80x24.org>
Thu, 25 Feb 2021 09:47:35 +0000 (09:47 +0000)
This lets users avoid network traffic on subsequent searches at
the expense of local disk space.  --no-import-remote may be
specified to reverse this trade-off for users with little
storage.

MANIFEST
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiQuery.pm
lib/PublicInbox/LeiXSearch.pm
t/lei-q-remote-import.t [new file with mode: 0644]

index 4c04eec8db16b7a4c5a0f67b2a728bc909d3258b..adbd108fbd21d8d00690e8698e732dcb49ac9af3 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -372,6 +372,7 @@ t/lei-import-maildir.t
 t/lei-import-nntp.t
 t/lei-import.t
 t/lei-mirror.t
+t/lei-q-remote-import.t
 t/lei.t
 t/lei_dedupe.t
 t/lei_external.t
index 8cd95ac2543c5fe35e1e19bbe4cfd8120bfd95ca..50665b3e5411b742b338d85f71445a78e967380c 100644 (file)
@@ -112,6 +112,7 @@ our %CMD = ( # sorted in order of importance/use:
        save-as=s output|mfolder|o=s format|f=s dedupe|d=s threads|t augment|a
        sort|s=s reverse|r offset=i remote! local! external! pretty
        include|I=s@ exclude=s@ only=s@ jobs|j=s globoff|g stdin|
+       import-remote!
        alert=s@ mua=s no-torsocks torsocks=s verbose|v+ quiet|q C=s@),
        PublicInbox::LeiQuery::curl_opt(), opt_dash('limit|n=i', '[0-9]+') ],
 
@@ -225,6 +226,7 @@ my %OPTDESC = (
                'whether or not to wrap git and curl commands with torsocks'],
 'no-torsocks' => 'alias for --torsocks=no',
 'save-as=s' => ['NAME', 'save a search terms by given name'],
+'import-remote!' => 'do not memoize remote messages into local store',
 
 'type=s' => [ 'any|mid|git', 'disambiguate type' ],
 
index 743fa3f7b23600874f008ffdc9833273515529e9..b57d1cc56aa70327dfccd19ab828a9642e3dfc73 100644 (file)
@@ -51,6 +51,7 @@ sub lei_q {
        # we'll allow "--only $LOCATION --local"
        my $sto = $self->_lei_store(1);
        my $lse = $sto->search;
+       $sto->write_prepare($self) if $opt->{'import-remote'} //= 1;
        if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
                $lxs->prepare_external($lse);
        }
index c46aba3b122b5e9bf46f5fd705cdbeab09376f62..2d399653591d1323749ff615929903ffcaa2bd6c 100644 (file)
@@ -189,8 +189,9 @@ sub query_mset { # non-parallel for non-"--threads" users
        $lei->{ovv}->ovv_atexit_child($lei);
 }
 
-sub each_eml { # callback for MboxReader->mboxrd
+sub each_remote_eml { # callback for MboxReader->mboxrd
        my ($eml, $self, $lei, $each_smsg) = @_;
+       $lei->{sto}->ipc_do('set_eml', $eml) if $lei->{sto}; # --import-remote
        my $smsg = bless {}, 'PublicInbox::Smsg';
        $smsg->populate($eml);
        $smsg->parse_references($eml, mids($eml));
@@ -244,14 +245,17 @@ sub query_remote_mboxrd {
                my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
                $reap_curl = PublicInbox::OnDestroy->new($sigint_reap, $pid);
                $fh = IO::Uncompress::Gunzip->new($fh);
-               PublicInbox::MboxReader->mboxrd($fh, \&each_eml, $self,
+               PublicInbox::MboxReader->mboxrd($fh, \&each_remote_eml, $self,
                                                $lei, $each_smsg);
                my $err = waitpid($pid, 0) == $pid ? undef
                                                : "BUG: waitpid($cmd): $!";
                @$reap_curl = (); # cancel OnDestroy
                die $err if $err;
+               my $nr = $lei->{-nr_remote_eml};
+               if ($nr && $lei->{sto}) {
+                       my $wait = $lei->{sto}->ipc_do('done');
+               }
                if ($? == 0) {
-                       my $nr = $lei->{-nr_remote_eml};
                        mset_progress($lei, $lei->{-current_url}, $nr, $nr);
                        next;
                }
diff --git a/t/lei-q-remote-import.t b/t/lei-q-remote-import.t
new file mode 100644 (file)
index 0000000..f73524c
--- /dev/null
@@ -0,0 +1,50 @@
+#!perl -w
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict; use v5.10.1; use PublicInbox::TestCommon;
+require_git 2.6;
+require_mods(qw(json DBD::SQLite Search::Xapian));
+use PublicInbox::MboxReader;
+my ($ro_home, $cfg_path) = setup_public_inboxes;
+my $sock = tcp_server;
+my ($tmpdir, $for_destroy) = tmpdir;
+my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/1", "--stderr=$tmpdir/2" ];
+my $env = { PI_CONFIG => $cfg_path };
+my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-httpd: $?");
+my $host_port = tcp_host_port($sock);
+my $url = "http://$host_port/t2/";
+my $exp1 = [ eml_load('t/plack-qp.eml') ];
+my $exp2 = [ eml_load('t/iso-2202-jp.eml') ];
+my $slurp_emls = sub {
+       open my $fh, '<', $_[0] or BAIL_OUT "open: $!";
+       my @eml;
+       PublicInbox::MboxReader->mboxrd($fh, sub {
+               my $eml = shift;
+               $eml->header_set('Status');
+               push @eml, $eml;
+       });
+       \@eml;
+};
+
+test_lei({ tmpdir => $tmpdir }, sub {
+       my $o = "$ENV{HOME}/o.mboxrd";
+       my @cmd = ('q', '-o', "mboxrd:$o", 'm:qp@example.com');
+       lei_ok(@cmd);
+       ok(-f $o && !-s _, 'output exists but is empty');
+       unlink $o or BAIL_OUT $!;
+       lei_ok(@cmd, '-I', $url);
+       is_deeply($slurp_emls->($o), $exp1, 'got results after remote search');
+       unlink $o or BAIL_OUT $!;
+       lei_ok(@cmd);
+       ok(-f $o && -s _, 'output exists after import but is not empty');
+       is_deeply($slurp_emls->($o), $exp1, 'got results w/o remote search');
+       unlink $o or BAIL_OUT $!;
+
+       $cmd[-1] = 'm:199707281508.AAA24167@hoyogw.example';
+       lei_ok(@cmd, '-I', $url, '--no-import-remote');
+       is_deeply($slurp_emls->($o), $exp2, 'got another after remote search');
+       unlink $o or BAIL_OUT $!;
+       lei_ok(@cmd);
+       ok(-f $o && !-s _, '--no-import-remote did not memoize');
+});
+done_testing;