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