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