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 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);
33 File::Path::make_path($dir); # raises on error
34 $self->{'-f'} = "$dir/lei.saved-search";
36 cfg_set($self, '--add', 'lei.q', $_) for @$q;
38 cfg_set($self, 'lei.q', $q);
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;
46 cfg_set($self, '--add', "lei.q.$k", $x);
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);
55 bless $self->{-cfg}, 'PublicInbox::Config';
56 $self->{lock_path} = "$self->{-f}.flock";
57 $self->{-ovf} = "$dir/over.sqlite3";
61 sub description { $_[0]->{qstr} } # for WWW
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]);
69 # drop-in for LeiDedupe API
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, '.');
83 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
85 $smsg = bless {}, 'PublicInbox::Smsg';
87 $smsg->populate($eml);
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}, '.');
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;
108 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
109 $oidx->eidx_prep; # for xref3
115 sub over { $_[0]->{oidx} } # for xoids_for
117 sub git { $_[0]->{ale}->git }
121 $self->{ale}->git->cleanup;
122 my $oidx = delete($self->{oidx}) // return;
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;