]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSavedSearch.pm
lei up: support output destination as arg
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.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 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::Lock);
9 use PublicInbox::OverIdx;
10 use PublicInbox::LeiSearch;
11 use PublicInbox::Config;
12 use PublicInbox::Spawn qw(run_die);
13 use PublicInbox::ContentHash qw(git_sha);
14 use Digest::SHA qw(sha256_hex);
15
16 sub lss_dir_for ($$) {
17         my ($lei, $dstref) = @_;
18         my @n;
19         if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
20                 require PublicInbox::URIimap;
21                 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
22                 $$dstref = $$uri;
23                 @n = ($uri->mailbox);
24         } else { # basename
25                 @n = ($$dstref =~ m{([\w\-\.]+)/*\z});
26                 $$dstref = $lei->rel2abs($$dstref);
27         }
28         push @n, sha256_hex($$dstref);
29         $lei->share_path . '/saved-searches/' . join('-', @n);
30 }
31
32 sub new {
33         my ($cls, $lei, $dir) = @_;
34         my $self = bless { ale => $lei->ale }, $cls;
35         if (defined $dir) { # updating existing saved search via "lei up"
36                 my $f = "$dir/lei.saved-search";
37                 ((-f $f && -r _) || output2lssdir($self, $lei, \$dir, \$f)) or
38                         return $lei->fail("$f non-existent or unreadable");
39                 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
40                 $self->{'-f'} = $f;
41         } else { # new saved search "lei q --save"
42                 my $dst = $lei->{ovv}->{dst};
43                 $dir = lss_dir_for($lei, \$dst);
44                 require File::Path;
45                 File::Path::make_path($dir); # raises on error
46                 $self->{-cfg} = {};
47                 $self->{'-f'} = "$dir/lei.saved-search";
48                 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
49                 if (ref $q) {
50                         cfg_set($self, '--add', 'lei.q', $_) for @$q;
51                 } else {
52                         cfg_set($self, 'lei.q', $q);
53                 }
54                 my $fmt = $lei->{opt}->{'format'};
55                 cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
56                 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
57                 cfg_set($self, 'lei.q.output', $dst);
58                 for my $k (qw(only include exclude)) {
59                         my $ary = $lei->{opt}->{$k} // next;
60                         for my $x (@$ary) {
61                                 cfg_set($self, '--add', "lei.q.$k", $x);
62                         }
63                 }
64                 for my $k (qw(external local remote import-remote
65                                 import-before threads)) {
66                         my $val = $lei->{opt}->{$k} // next;
67                         cfg_set($self, "lei.q.$k", $val);
68                 }
69         }
70         bless $self->{-cfg}, 'PublicInbox::Config';
71         $self->{lock_path} = "$self->{-f}.flock";
72         $self->{-ovf} = "$dir/over.sqlite3";
73         $self;
74 }
75
76 sub description { $_[0]->{qstr} } # for WWW
77
78 sub cfg_set {
79         my ($self, @args) = @_;
80         my $lk = $self->lock_for_scope; # git-config doesn't wait
81         run_die([qw(git config -f), $self->{'-f'}, @args]);
82 }
83
84 # drop-in for LeiDedupe API
85 sub is_dup {
86         my ($self, $eml, $smsg) = @_;
87         my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
88         my $blob = $smsg ? $smsg->{blob} : undef;
89         return 1 if $blob && $oidx->blob_exists($blob);
90         my $lk = $self->lock_for_scope_fast;
91         if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
92                 for my $docid (values %$xoids) {
93                         $oidx->add_xref3($docid, -1, $blob, '.');
94                 }
95                 $oidx->commit_lazy;
96                 1;
97         } else {
98                 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
99                 unless ($smsg) {
100                         $smsg = bless {}, 'PublicInbox::Smsg';
101                         $smsg->{bytes} = 0;
102                         $smsg->populate($eml);
103                 }
104                 $oidx->begin_lazy;
105                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
106                 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
107                 $oidx->add_overview($eml, $smsg);
108                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
109                 $oidx->commit_lazy;
110                 undef;
111         }
112 }
113
114 sub prepare_dedupe {
115         my ($self) = @_;
116         $self->{oidx} //= do {
117                 my $creat = !-f $self->{-ovf};
118                 my $lk = $self->lock_for_scope; # git-config doesn't wait
119                 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
120                 $oidx->{-no_fsync} = 1;
121                 $oidx->dbh;
122                 if ($creat) {
123                         $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
124                         $oidx->eidx_prep; # for xref3
125                 }
126                 $oidx
127         };
128 }
129
130 sub over { $_[0]->{oidx} } # for xoids_for
131
132 sub git { $_[0]->{ale}->git }
133
134 sub pause_dedupe {
135         my ($self) = @_;
136         $self->{ale}->git->cleanup;
137         my $oidx = delete($self->{oidx}) // return;
138         $oidx->commit_lazy;
139 }
140
141 sub mm { undef }
142
143 sub altid_map { {} }
144
145 sub cloneurl { [] }
146
147 # find existing directory containing a `lei.saved-search' file based on
148 # $dir_ref which is an output
149 sub output2lssdir {
150         my ($self, $lei, $dir_ref, $fn_ref) = @_;
151         my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
152         my $dir = lss_dir_for($lei, \$dst);
153         my $f = "$dir/lei.saved-search";
154         if (-f $f && -r _) {
155                 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
156                 $$dir_ref = $dir;
157                 $$fn_ref = $f;
158                 return 1;
159         }
160         undef;
161 }
162
163 no warnings 'once';
164 *nntp_url = \&cloneurl;
165 *base_url = \&PublicInbox::Inbox::base_url;
166 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
167 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
168 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
169 *modified = \&PublicInbox::Inbox::modified;
170 *recent = \&PublicInbox::Inbox::recent;
171 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
172 *isrch = *search = \&mm; # TODO
173 *DESTROY = \&pause_dedupe;
174
175 1;