]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import: support adding keywords and labels on import
[public-inbox.git] / lib / PublicInbox / LeiImport.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 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9
10 # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass
11
12 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
13         my ($self, $eml, $vmd) = @_;
14         my $xoids = $self->{lei}->{ale}->xoids_for($eml);
15         if (my $all_vmd = $self->{all_vmd}) {
16                 $vmd //= {};
17                 @$vmd{keys %$all_vmd} = values %$all_vmd;
18         }
19         $self->{lei}->{sto}->ipc_do('set_eml', $eml, $vmd, $xoids);
20 }
21
22 sub input_mbox_cb { # MboxReader callback
23         my ($eml, $self) = @_;
24         my $vmd;
25         if ($self->{-import_kw}) {
26                 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
27                 $vmd = { kw => $kw } if scalar(@$kw);
28         }
29         input_eml_cb($self, $eml, $vmd);
30 }
31
32 sub input_maildir_cb { # maildir_each_eml cb
33         my ($f, $kw, $eml, $self) = @_;
34         input_eml_cb($self, $eml, $self->{-import_kw} ? { kw => $kw } : undef);
35 }
36
37 sub input_net_cb { # imap_each, nntp_each cb
38         my ($url, $uid, $kw, $eml, $self) = @_;
39         input_eml_cb($self, $eml, $self->{-import_kw} ? { kw => $kw } : undef);
40 }
41
42 sub import_done { # EOF callback for main daemon
43         my ($lei) = @_;
44         my $imp = delete $lei->{imp} // return $lei->fail('BUG: {imp} gone');
45         $imp->wq_wait_old($lei->can('wq_done_wait'), $lei, 'non-fatal');
46 }
47
48 sub net_merge_complete { # callback used by LeiAuth
49         my ($self) = @_;
50         $self->wq_io_do('process_inputs');
51         $self->wq_close(1);
52 }
53
54 sub lei_import { # the main "lei import" method
55         my ($lei, @inputs) = @_;
56         my $sto = $lei->_lei_store(1);
57         $sto->write_prepare($lei);
58         my $self = bless {}, __PACKAGE__;
59         $self->{-import_kw} = $lei->{opt}->{kw} // 1;
60         my $vmd_mod = $self->vmd_mod_extract(\@inputs);
61         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
62         $self->{all_vmd} = $vmd_mod if scalar keys %$vmd_mod;
63         $self->prepare_inputs($lei, \@inputs) or return;
64         $lei->ale; # initialize for workers to read
65         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
66         if (my $net = $lei->{net}) {
67                 # $j = $net->net_concurrency($j); TODO
68                 if ($lei->{opt}->{incremental} // 1) {
69                         $net->{incremental} = 1;
70                         $net->{itrk_fn} = $lei->store_path .
71                                                 '/net_last.sqlite3';
72                 }
73         } else {
74                 my $nproc = $self->detect_nproc;
75                 $j = $nproc if $j > $nproc;
76         }
77         my $ops = { '' => [ \&import_done, $lei ] };
78         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
79         $self->{-wq_nr_workers} = $j // 1; # locked
80         (my $op_c, $ops) = $lei->workers_start($self, 'lei_import', $j, $ops);
81         $lei->{imp} = $self;
82         net_merge_complete($self) unless $lei->{auth};
83         $op_c->op_wait_event($ops);
84 }
85
86 no warnings 'once';
87 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
88
89 # the following works even when LeiAuth is lazy-loaded
90 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
91 1;