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