]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiALE.pm
lei: tie ALE lifetime to config file
[public-inbox.git] / lib / PublicInbox / LeiALE.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 # All Locals Ever: track lei/store + externals ever used as
5 # long as they're on an accessible FS.  Includes "lei q" --include
6 # and --only targets that haven't been through "lei add-external".
7 # Typically: ~/.cache/lei/all_locals_ever.git
8 package PublicInbox::LeiALE;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::LeiSearch PublicInbox::Lock);
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use PublicInbox::LeiXSearch;
15 use Fcntl qw(SEEK_SET);
16
17 sub _new {
18         my ($d) = @_;
19         PublicInbox::Import::init_bare($d, 'ale');
20         bless {
21                 git => PublicInbox::Git->new($d),
22                 lock_path => "$d/lei_ale.state", # dual-duty lock + state
23                 ibxish => [], # Inbox and ExtSearch (and LeiSearch) objects
24         }, __PACKAGE__
25 }
26
27 sub new {
28         my ($self, $lei) = @_;
29         ref($self) or $self = _new($lei->cache_dir . '/all_locals_ever.git');
30         my $lxs = PublicInbox::LeiXSearch->new;
31         $lxs->prepare_external($lei->_lei_store(1)->search);
32         for my $loc ($lei->externals_each) { # locals only
33                 $lxs->prepare_external($loc) if -d $loc;
34         }
35         $self->refresh_externals($lxs);
36         $self;
37 }
38
39 sub over {} # undef for xoids_for
40
41 sub overs_all { # for xoids_for (called only in lei workers?)
42         my ($self) = @_;
43         my $pid = $$;
44         if (($self->{owner_pid} // $pid) != $pid) {
45                 delete($_->{over}) for @{$self->{ibxish}};
46         }
47         $self->{owner_pid} = $pid;
48         grep(defined, map { $_->over } @{$self->{ibxish}});
49 }
50
51 sub refresh_externals {
52         my ($self, $lxs) = @_;
53         $self->git->cleanup;
54         my $lk = $self->lock_for_scope;
55         my $cur_lxs = ref($lxs)->new;
56         my $orig = do {
57                 local $/;
58                 readline($self->{lockfh}) //
59                                 die "readline($self->{lock_path}): $!";
60         };
61         my $new = '';
62         my $old = '';
63         my $gone = 0;
64         my %seen_ibxish; # $dir => any-defined value
65         for my $dir (split(/\n/, $orig)) {
66                 if (-d $dir && -r _ && $cur_lxs->prepare_external($dir)) {
67                         $seen_ibxish{$dir} //= length($old .= "$dir\n");
68                 } else {
69                         ++$gone;
70                 }
71         }
72         my @ibxish = $cur_lxs->locals;
73         for my $x ($lxs->locals) {
74                 my $d = File::Spec->canonpath($x->{inboxdir} // $x->{topdir});
75                 $seen_ibxish{$d} //= do {
76                         $new .= "$d\n";
77                         push @ibxish, $x;
78                 };
79         }
80         if ($new ne '' || $gone) {
81                 $self->{lockfh}->autoflush(1);
82                 if ($gone) {
83                         seek($self->{lockfh}, 0, SEEK_SET) or die "seek: $!";
84                         truncate($self->{lockfh}, 0) or die "truncate: $!";
85                 } else {
86                         $old = '';
87                 }
88                 print { $self->{lockfh} } $old, $new or die "print: $!";
89         }
90         $new = $old = '';
91         my $f = $self->git->{git_dir}.'/objects/info/alternates';
92         if (open my $fh, '<', $f) {
93                 local $/;
94                 $old = <$fh> // die "readline($f): $!";
95         }
96         for my $x (@ibxish) {
97                 $new .= File::Spec->canonpath($x->git->{git_dir})."/objects\n";
98         }
99         $self->{ibxish} = \@ibxish;
100         return if $old eq $new;
101
102         # this needs to be atomic since child processes may start
103         # git-cat-file at any time
104         my $tmp = "$f.$$.tmp";
105         open my $fh, '>', $tmp or die "open($tmp): $!";
106         print $fh $new or die "print($tmp): $!";
107         close $fh or die "close($tmp): $!";
108         rename($tmp, $f) or die "rename($tmp, $f): $!";
109 }
110
111 1;