]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
lei: All Local Externals: bare git dir for alternates
[public-inbox.git] / lib / PublicInbox / LeiStore.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Local storage (cache/memo) for lei(1), suitable for personal/private
5 # mail iff on encrypted device/FS.  Based on v2, but only deduplicates
6 # based on git OID.
7 #
8 # for xref3, the following are constant: $eidx_key = '.', $xnum = -1
9 package PublicInbox::LeiStore;
10 use strict;
11 use v5.10.1;
12 use parent qw(PublicInbox::Lock PublicInbox::IPC);
13 use PublicInbox::ExtSearchIdx;
14 use PublicInbox::Import;
15 use PublicInbox::InboxWritable qw(eml_from_path);
16 use PublicInbox::V2Writable;
17 use PublicInbox::ContentHash qw(content_hash);
18 use PublicInbox::MID qw(mids);
19 use PublicInbox::LeiSearch;
20 use PublicInbox::MDA;
21 use List::Util qw(max);
22
23 sub new {
24         my (undef, $dir, $opt) = @_;
25         my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
26         my $self = bless { priv_eidx => $eidx }, __PACKAGE__;
27         eidx_init($self)->done if $opt->{creat};
28         $self;
29 }
30
31 sub git { $_[0]->{priv_eidx}->git } # read-only
32
33 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
34
35 sub rotate_bytes {
36         $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
37 }
38
39 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
40
41 sub git_epoch_max  {
42         my ($self) = @_;
43         if (opendir(my $dh, $self->git_pfx)) {
44                 max(map {
45                         substr($_, 0, -4) + 0; # drop ".git" suffix
46                 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
47         } else {
48                 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
49         }
50 }
51
52 sub git_ident ($) {
53         my ($git) = @_;
54         chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT)));
55         warn "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
56         $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
57                 ('lei user', 'x@example.com')
58 }
59
60 sub importer {
61         my ($self) = @_;
62         my $max;
63         my $im = $self->{im};
64         if ($im) {
65                 return $im if $im->{bytes_added} < $self->rotate_bytes;
66
67                 delete $self->{im};
68                 $im->done;
69                 undef $im;
70                 $self->checkpoint;
71                 $max = $self->git_epoch_max + 1;
72         }
73         my $pfx = $self->git_pfx;
74         $max //= $self->git_epoch_max;
75         while (1) {
76                 my $latest = "$pfx/$max.git";
77                 my $old = -e $latest;
78                 PublicInbox::Import::init_bare($latest);
79                 my $git = PublicInbox::Git->new($latest);
80                 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
81                 my $packed_bytes = $git->packed_bytes;
82                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
83                 if ($unpacked_bytes >= $self->rotate_bytes) {
84                         $max++;
85                         next;
86                 }
87                 my ($n, $e) = git_ident($git);
88                 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
89                 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
90                 $im->{lock_path} = undef;
91                 $im->{path_type} = 'v2';
92                 return $im;
93         }
94 }
95
96 sub search {
97         PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
98 }
99
100 sub eidx_init {
101         my ($self) = @_;
102         my $eidx = $self->{priv_eidx};
103         $eidx->idx_init({-private => 1});
104         $eidx;
105 }
106
107 sub _docids_for ($$) {
108         my ($self, $eml) = @_;
109         my %docids;
110         my ($chash, $mids) = PublicInbox::LeiSearch::content_key($eml);
111         my $eidx = eidx_init($self);
112         my $oidx = $eidx->{oidx};
113         my $im = $self->{im};
114         for my $mid (@$mids) {
115                 my ($id, $prev);
116                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
117                         my $oid = $cur->{blob};
118                         my $docid = $cur->{num};
119                         my $bref = $im ? $im->cat_blob($oid) : undef;
120                         $bref //= $eidx->git->cat_file($oid) // do {
121                                 warn "W: $oid (#$docid) <$mid> not found\n";
122                                 next;
123                         };
124                         local $self->{current_info} = $oid;
125                         my $x = PublicInbox::Eml->new($bref);
126                         $docids{$docid} = $docid if content_hash($x) eq $chash;
127                 }
128         }
129         sort { $a <=> $b } values %docids;
130 }
131
132 sub set_eml_vmd {
133         my ($self, $eml, $vmd) = @_;
134         my $eidx = eidx_init($self);
135         my @docids = _docids_for($self, $eml);
136         for my $docid (@docids) {
137                 $eidx->idx_shard($docid)->ipc_do('set_vmd', $docid, $vmd);
138         }
139         \@docids;
140 }
141
142 sub add_eml_vmd {
143         my ($self, $eml, $vmd) = @_;
144         my $eidx = eidx_init($self);
145         my @docids = _docids_for($self, $eml);
146         for my $docid (@docids) {
147                 $eidx->idx_shard($docid)->ipc_do('add_vmd', $docid, $vmd);
148         }
149         \@docids;
150 }
151
152 sub remove_eml_vmd {
153         my ($self, $eml, $vmd) = @_;
154         my $eidx = eidx_init($self);
155         my @docids = _docids_for($self, $eml);
156         for my $docid (@docids) {
157                 $eidx->idx_shard($docid)->ipc_do('remove_vmd', $docid, $vmd);
158         }
159         \@docids;
160 }
161
162 sub add_eml {
163         my ($self, $eml, $vmd) = @_;
164         my $im = $self->importer; # may create new epoch
165         my $eidx = eidx_init($self); # writes ALL.git/objects/info/alternates
166         my $oidx = $eidx->{oidx};
167         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
168         $im->add($eml, undef, $smsg) or return; # duplicate returns undef
169
170         local $self->{current_info} = $smsg->{blob};
171         if (my @docids = _docids_for($self, $eml)) {
172                 for my $docid (@docids) {
173                         my $idx = $eidx->idx_shard($docid);
174                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
175                         # add_eidx_info for List-Id
176                         $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
177                         $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
178                 }
179                 \@docids;
180         } else {
181                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
182                 $oidx->add_overview($eml, $smsg);
183                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
184                 my $idx = $eidx->idx_shard($smsg->{num});
185                 $idx->index_eml($eml, $smsg);
186                 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd ) if $vmd;
187                 $smsg;
188         }
189 }
190
191 sub set_eml {
192         my ($self, $eml, $vmd) = @_;
193         add_eml($self, $eml, $vmd) // set_eml_vmd($self, $eml, $vmd);
194 }
195
196 sub add_eml_maybe {
197         my ($self, $eml) = @_;
198         my $lxs = $self->{lxs_all_local} // die 'BUG: no {lxs_all_local}';
199         return if $lxs->xids_for($eml, 1);
200         add_eml($self, $eml);
201 }
202
203 # set or update keywords for external message, called via ipc_do
204 sub set_xkw {
205         my ($self, $eml, $kw) = @_;
206         my $lxs = $self->{lxs_all_local} // die 'BUG: no {lxs_all_local}';
207         if ($lxs->xids_for($eml, 1)) { # is it in a local external?
208                 # TODO: index keywords only
209         } else {
210                 set_eml($self, $eml, { kw => $kw });
211         }
212 }
213
214 sub checkpoint {
215         my ($self, $wait) = @_;
216         if (my $im = $self->{im}) {
217                 $wait ? $im->barrier : $im->checkpoint;
218         }
219         $self->{priv_eidx}->checkpoint($wait);
220 }
221
222 sub done {
223         my ($self) = @_;
224         my $err = '';
225         if (my $im = delete($self->{im})) {
226                 eval { $im->done };
227                 if ($@) {
228                         $err .= "import done: $@\n";
229                         warn $err;
230                 }
231         }
232         $self->{priv_eidx}->done;
233         die $err if $err;
234 }
235
236 sub ipc_atfork_child {
237         my ($self) = @_;
238         my $lei = $self->{lei};
239         $lei->lei_atfork_child(1) if $lei;
240         $self->SUPER::ipc_atfork_child;
241 }
242
243 sub refresh_local_externals {
244         my ($self) = @_;
245         my $cfg = $self->{lei}->_lei_cfg or return;
246         my $cur_cfg = $self->{cur_cfg} // -1;
247         my $lxs = $self->{lxs_all_local};
248         if ($cfg != $cur_cfg || !$lxs) {
249                 $lxs = PublicInbox::LeiXSearch->new;
250                 my @loc = $self->{lei}->externals_each;
251                 for my $loc (@loc) { # locals only
252                         $lxs->prepare_external($loc) if -d $loc;
253                 }
254                 $self->{lei}->ale->refresh_externals($lxs);
255                 $lxs->{git} = $self->{lei}->ale->git;
256                 $self->{lxs_all_local} = $lxs;
257                 $self->{cur_cfg} = $cfg;
258         }
259 }
260
261 sub write_prepare {
262         my ($self, $lei) = @_;
263         unless ($self->{-ipc_req}) {
264                 require PublicInbox::LeiXSearch;
265                 $self->ipc_lock_init($lei->store_path . '/ipc.lock');
266                 # Mail we import into lei are private, so headers filtered out
267                 # by -mda for public mail are not appropriate
268                 local @PublicInbox::MDA::BAD_HEADERS = ();
269                 $self->ipc_worker_spawn('lei_store', $lei->oldset,
270                                         { lei => $lei });
271         }
272         my $wait = $self->ipc_do('refresh_local_externals');
273         $lei->{sto} = $self;
274 }
275
276 1;