1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
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);
15 use PublicInbox::Hval qw(to_filename);
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";
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);
31 File::Path::make_path($dir); # raises on error
32 $self->{'-f'} = "$dir/lei.saved-search";
33 my $q = $lei->{mset_opt}->{q_raw};
35 cfg_set($self, '--add', 'lei.q', $_) for @$q;
37 cfg_set($self, 'lei.q', $q);
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;
45 cfg_set($self, '--add', "lei.q.$k", $x);
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);
54 bless $self->{-cfg}, 'PublicInbox::Config';
55 $self->{lock_path} = "$self->{-f}.flock";
56 $self->{-ovf} = "$dir/over.sqlite3";
60 sub description { $_[0]->{qstr} } # for WWW
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]);
68 # drop-in for LeiDedupe API
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, '.');
82 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
84 $smsg = bless {}, 'PublicInbox::Smsg';
86 $smsg->populate($eml);
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}, '.');
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;
107 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
108 $oidx->eidx_prep; # for xref3
114 sub over { $_[0]->{oidx} } # for xoids_for
116 sub git { $_[0]->{ale}->git }
120 $self->{ale}->git->cleanup;
121 my $oidx = delete($self->{oidx}) // return;
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;