]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSavedSearch.pm
lei: add "lei up" to complement "lei q --save"
[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(content_hash git_sha);
14 use PublicInbox::Eml;
15 use PublicInbox::Hval qw(to_filename);
16
17 sub new {
18         my ($cls, $lei, $dir) = @_;
19         my $self = bless { ale => $lei->ale, -cfg => {} }, $cls;
20         if (defined $dir) { # updating existing saved search via "lei up"
21                 my $f = $self->{'-f'} = "$dir/lei.saved-search";
22                 -f $f && -r _ or
23                         return $lei->fail("$f non-existent or unreadable");
24                 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
25         } else { # new saved search "lei q --save"
26                 my $saved_dir = $lei->store_path . '/../saved-searches/';
27                 my (@name) = ($lei->{ovv}->{dst} =~ m{([\w\-\.]+)/*\z});
28                 push @name, to_filename($lei->{mset_opt}->{qstr});
29                 $dir = $saved_dir . join('-', @name);
30                 require File::Path;
31                 File::Path::make_path($dir); # raises on error
32                 $self->{'-f'} = "$dir/lei.saved-search";
33                 my $q = $lei->{mset_opt}->{q_raw};
34                 if (ref $q) {
35                         cfg_set($self, '--add', 'lei.q', $_) for @$q;
36                 } else {
37                         cfg_set($self, 'lei.q', $q);
38                 }
39                 my $fmt = $lei->{opt}->{'format'};
40                 cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
41                 cfg_set($self, 'lei.q.output', $lei->{opt}->{output});
42                 for my $k (qw(only include exclude)) {
43                         my $ary = $lei->{opt}->{$k} // next;
44                         for my $x (@$ary) {
45                                 cfg_set($self, '--add', "lei.q.$k", $x);
46                         }
47                 }
48                 for my $k (qw(external local remote import-remote
49                                 import-before threads)) {
50                         my $val = $lei->{opt}->{$k} // next;
51                         cfg_set($self, "lei.q.$k", $val);
52                 }
53         }
54         bless $self->{-cfg}, 'PublicInbox::Config';
55         $self->{lock_path} = "$self->{-f}.flock";
56         $self->{-ovf} = "$dir/over.sqlite3";
57         $self;
58 }
59
60 sub description { $_[0]->{qstr} } # for WWW
61
62 sub cfg_set {
63         my ($self, @args) = @_;
64         my $lk = $self->lock_for_scope; # git-config doesn't wait
65         run_die([qw(git config -f), $self->{'-f'}, @args]);
66 }
67
68 # drop-in for LeiDedupe API
69 sub is_dup {
70         my ($self, $eml, $smsg) = @_;
71         my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
72         my $blob = $smsg ? $smsg->{blob} : undef;
73         return 1 if $blob && $oidx->blob_exists($blob);
74         my $lk = $self->lock_for_scope_fast;
75         if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
76                 for my $docid (values %$xoids) {
77                         $oidx->add_xref3($docid, -1, $blob, '.');
78                 }
79                 $oidx->commit_lazy;
80                 1;
81         } else {
82                 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
83                 unless ($smsg) {
84                         $smsg = bless {}, 'PublicInbox::Smsg';
85                         $smsg->{bytes} = 0;
86                         $smsg->populate($eml);
87                 }
88                 $oidx->begin_lazy;
89                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
90                 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
91                 $oidx->add_overview($eml, $smsg);
92                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
93                 $oidx->commit_lazy;
94                 undef;
95         }
96 }
97
98 sub prepare_dedupe {
99         my ($self) = @_;
100         $self->{oidx} //= do {
101                 my $creat = !-f $self->{-ovf};
102                 my $lk = $self->lock_for_scope; # git-config doesn't wait
103                 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
104                 $oidx->{-no_fsync} = 1;
105                 $oidx->dbh;
106                 if ($creat) {
107                         $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
108                         $oidx->eidx_prep; # for xref3
109                 }
110                 $oidx
111         };
112 }
113
114 sub over { $_[0]->{oidx} } # for xoids_for
115
116 sub git { $_[0]->{ale}->git }
117
118 sub pause_dedupe {
119         my ($self) = @_;
120         $self->{ale}->git->cleanup;
121         my $oidx = delete($self->{oidx}) // return;
122         $oidx->commit_lazy;
123 }
124
125 sub mm { undef }
126
127 sub altid_map { {} }
128
129 sub cloneurl { [] }
130 no warnings 'once';
131 *nntp_url = \&cloneurl;
132 *base_url = \&PublicInbox::Inbox::base_url;
133 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
134 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
135 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
136 *modified = \&PublicInbox::Inbox::modified;
137 *recent = \&PublicInbox::Inbox::recent;
138 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
139 *isrch = *search = \&mm; # TODO
140 *DESTROY = \&pause_dedupe;
141
142 1;