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
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 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);
37 File::Path::make_path($dir); # raises on error
38 $self->{'-f'} = "$dir/lei.saved-search";
39 my $q = $lei->{mset_opt}->{q_raw};
41 cfg_set($self, '--add', 'lei.q', $_) for @$q;
43 cfg_set($self, 'lei.q', $q);
46 bless $self->{-cfg}, 'PublicInbox::Config';
47 $self->{lock_path} = "$self->{-f}.flock";
48 $self->{-ovf} = "$dir/over.sqlite3";
52 sub description { $_[0]->{qstr} } # for WWW
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]);
60 # drop-in for LeiDedupe API
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, '.');
74 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
76 $smsg = bless {}, 'PublicInbox::Smsg';
78 $smsg->populate($eml);
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}, '.');
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;
99 $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
100 $oidx->eidx_prep; # for xref3
106 sub over { $_[0]->{oidx} } # for xoids_for
108 sub git { $_[0]->{ale}->git }
112 $self->{ale}->git->cleanup;
113 my $oidx = delete($self->{oidx}) // return;
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;