]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
lei_store: quiet down git user info being unset
[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         my $rdr = {};
55         open $rdr->{2}, '>', '/dev/null' or die "open /dev/null: $!";
56         chomp(my $i = $git->qx([qw(var GIT_COMMITTER_IDENT)], undef, $rdr));
57         $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
58                 ('lei user', 'x@example.com')
59 }
60
61 sub importer {
62         my ($self) = @_;
63         my $max;
64         my $im = $self->{im};
65         if ($im) {
66                 return $im if $im->{bytes_added} < $self->rotate_bytes;
67
68                 delete $self->{im};
69                 $im->done;
70                 undef $im;
71                 $self->checkpoint;
72                 $max = $self->git_epoch_max + 1;
73         }
74         my $pfx = $self->git_pfx;
75         $max //= $self->git_epoch_max;
76         while (1) {
77                 my $latest = "$pfx/$max.git";
78                 my $old = -e $latest;
79                 PublicInbox::Import::init_bare($latest);
80                 my $git = PublicInbox::Git->new($latest);
81                 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
82                 my $packed_bytes = $git->packed_bytes;
83                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
84                 if ($unpacked_bytes >= $self->rotate_bytes) {
85                         $max++;
86                         next;
87                 }
88                 my ($n, $e) = git_ident($git);
89                 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
90                 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
91                 $im->{lock_path} = undef;
92                 $im->{path_type} = 'v2';
93                 return $im;
94         }
95 }
96
97 sub search {
98         PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
99 }
100
101 sub eidx_init {
102         my ($self) = @_;
103         my $eidx = $self->{priv_eidx};
104         $eidx->idx_init({-private => 1});
105         $eidx;
106 }
107
108 sub _docids_for ($$) {
109         my ($self, $eml) = @_;
110         my %docids;
111         my ($chash, $mids) = PublicInbox::LeiSearch::content_key($eml);
112         my $eidx = eidx_init($self);
113         my $oidx = $eidx->{oidx};
114         my $im = $self->{im};
115         for my $mid (@$mids) {
116                 my ($id, $prev);
117                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
118                         next if $cur->{bytes} == 0; # external-only message
119                         my $oid = $cur->{blob};
120                         my $docid = $cur->{num};
121                         my $bref = $im ? $im->cat_blob($oid) : undef;
122                         $bref //= $eidx->git->cat_file($oid) // do {
123                                 warn "W: $oid (#$docid) <$mid> not found\n";
124                                 next;
125                         };
126                         local $self->{current_info} = $oid;
127                         my $x = PublicInbox::Eml->new($bref);
128                         $docids{$docid} = $docid if content_hash($x) eq $chash;
129                 }
130         }
131         sort { $a <=> $b } values %docids;
132 }
133
134 sub set_eml_vmd {
135         my ($self, $eml, $vmd, $docids) = @_;
136         my $eidx = eidx_init($self);
137         $docids //= [ _docids_for($self, $eml) ];
138         for my $docid (@$docids) {
139                 $eidx->idx_shard($docid)->ipc_do('set_vmd', $docid, $vmd);
140         }
141         $docids;
142 }
143
144 sub add_eml_vmd {
145         my ($self, $eml, $vmd) = @_;
146         my $eidx = eidx_init($self);
147         my @docids = _docids_for($self, $eml);
148         for my $docid (@docids) {
149                 $eidx->idx_shard($docid)->ipc_do('add_vmd', $docid, $vmd);
150         }
151         \@docids;
152 }
153
154 sub remove_eml_vmd {
155         my ($self, $eml, $vmd) = @_;
156         my $eidx = eidx_init($self);
157         my @docids = _docids_for($self, $eml);
158         for my $docid (@docids) {
159                 $eidx->idx_shard($docid)->ipc_do('remove_vmd', $docid, $vmd);
160         }
161         \@docids;
162 }
163
164 sub add_eml {
165         my ($self, $eml, $vmd, $xoids) = @_;
166         my $im = $self->importer; # may create new epoch
167         my $eidx = eidx_init($self); # writes ALL.git/objects/info/alternates
168         my $oidx = $eidx->{oidx}; # PublicInbox::Import::add checks this
169         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
170         $im->add($eml, undef, $smsg) or return; # duplicate returns undef
171
172         local $self->{current_info} = $smsg->{blob};
173         my $vivify_xvmd = delete($smsg->{-vivify_xvmd}) // []; # exact matches
174         if ($xoids) { # fuzzy matches from externals in ale->xoids_for
175                 delete $xoids->{$smsg->{blob}}; # added later
176                 if (scalar keys %$xoids) {
177                         my %docids = map { $_ => 1 } @$vivify_xvmd;
178                         for my $oid (keys %$xoids) {
179                                 my @id = $oidx->blob_exists($oid);
180                                 @docids{@id} = @id;
181                         }
182                         @$vivify_xvmd = sort { $a <=> $b } keys(%docids);
183                 }
184         }
185         if (@$vivify_xvmd) {
186                 $xoids //= {};
187                 $xoids->{$smsg->{blob}} = 1;
188                 for my $docid (@$vivify_xvmd) {
189                         my $cur = $oidx->get_art($docid);
190                         my $idx = $eidx->idx_shard($docid);
191                         if (!$cur || $cur->{bytes} == 0) { # really vivifying
192                                 $smsg->{num} = $docid;
193                                 $oidx->add_overview($eml, $smsg);
194                                 $smsg->{-merge_vmd} = 1;
195                                 $idx->index_eml($eml, $smsg);
196                         } else { # lse fuzzy hit off ale
197                                 $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
198                         }
199                         for my $oid (keys %$xoids) {
200                                 $oidx->add_xref3($docid, -1, $oid, '.');
201                         }
202                         $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
203                 }
204                 $vivify_xvmd;
205         } elsif (my @docids = _docids_for($self, $eml)) {
206                 # fuzzy match from within lei/store
207                 for my $docid (@docids) {
208                         my $idx = $eidx->idx_shard($docid);
209                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
210                         # add_eidx_info for List-Id
211                         $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
212                         $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
213                 }
214                 \@docids;
215         } else { # totally new message
216                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
217                 $oidx->add_overview($eml, $smsg);
218                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
219                 my $idx = $eidx->idx_shard($smsg->{num});
220                 $idx->index_eml($eml, $smsg);
221                 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd) if $vmd;
222                 $smsg;
223         }
224 }
225
226 sub set_eml {
227         my ($self, $eml, $vmd, $xoids) = @_;
228         add_eml($self, $eml, $vmd, $xoids) //
229                 set_eml_vmd($self, $eml, $vmd);
230 }
231
232 sub _external_only ($$$) {
233         my ($self, $xoids, $eml) = @_;
234         my $eidx = $self->{priv_eidx};
235         my $oidx = $eidx->{oidx} // die 'BUG: {oidx} missing';
236         my $smsg = bless { blob => '' }, 'PublicInbox::Smsg';
237         $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
238         # save space for an externals-only message
239         my $hdr = $eml->header_obj;
240         $smsg->populate($hdr); # sets lines == 0
241         $smsg->{bytes} = 0;
242         delete @$smsg{qw(From Subject)};
243         $smsg->{to} = $smsg->{cc} = $smsg->{from} = '';
244         $oidx->add_overview($hdr, $smsg); # subject+references for threading
245         $smsg->{subject} = '';
246         for my $oid (keys %$xoids) {
247                 $oidx->add_xref3($smsg->{num}, -1, $oid, '.');
248         }
249         my $idx = $eidx->idx_shard($smsg->{num});
250         $idx->index_eml(PublicInbox::Eml->new("\n\n"), $smsg);
251         ($smsg, $idx);
252 }
253
254 sub update_xvmd {
255         my ($self, $xoids, $eml, $vmd_mod) = @_;
256         my $eidx = eidx_init($self);
257         my $oidx = $eidx->{oidx};
258         my %seen;
259         for my $oid (keys %$xoids) {
260                 my @docids = $oidx->blob_exists($oid) or next;
261                 scalar(@docids) > 1 and
262                         warn "W: $oid indexed as multiple docids: @docids\n";
263                 for my $docid (@docids) {
264                         next if $seen{$docid}++;
265                         my $idx = $eidx->idx_shard($docid);
266                         $idx->ipc_do('update_vmd', $docid, $vmd_mod);
267                 }
268                 delete $xoids->{$oid};
269         }
270         return unless scalar(keys(%$xoids));
271
272         # see if it was indexed, but with different OID(s)
273         if (my @docids = _docids_for($self, $eml)) {
274                 for my $docid (@docids) {
275                         next if $seen{$docid};
276                         for my $oid (keys %$xoids) {
277                                 $oidx->add_xref3($docid, -1, $oid, '.');
278                         }
279                         my $idx = $eidx->idx_shard($docid);
280                         $idx->ipc_do('update_vmd', $docid, $vmd_mod);
281                 }
282                 return;
283         }
284         # totally unseen
285         my ($smsg, $idx) = _external_only($self, $xoids, $eml);
286         $idx->ipc_do('update_vmd', $smsg->{num}, $vmd_mod);
287 }
288
289 # set or update keywords for external message, called via ipc_do
290 sub set_xvmd {
291         my ($self, $xoids, $eml, $vmd) = @_;
292
293         my $eidx = eidx_init($self);
294         my $oidx = $eidx->{oidx};
295         my %seen;
296
297         # see if we can just update existing docs
298         for my $oid (keys %$xoids) {
299                 my @docids = $oidx->blob_exists($oid) or next;
300                 scalar(@docids) > 1 and
301                         warn "W: $oid indexed as multiple docids: @docids\n";
302                 for my $docid (@docids) {
303                         next if $seen{$docid}++;
304                         my $idx = $eidx->idx_shard($docid);
305                         $idx->ipc_do('set_vmd', $docid, $vmd);
306                 }
307                 delete $xoids->{$oid}; # all done with this oid
308         }
309         return unless scalar(keys(%$xoids));
310
311         # n.b. we don't do _docids_for here, we expect the caller
312         # already checked $lse->kw_changed before calling this sub
313
314         return unless (@{$vmd->{kw} // []}) || (@{$vmd->{L} // []});
315         # totally unseen:
316         my ($smsg, $idx) = _external_only($self, $xoids, $eml);
317         $idx->ipc_do('add_vmd', $smsg->{num}, $vmd);
318 }
319
320 sub checkpoint {
321         my ($self, $wait) = @_;
322         if (my $im = $self->{im}) {
323                 $wait ? $im->barrier : $im->checkpoint;
324         }
325         $self->{priv_eidx}->checkpoint($wait);
326 }
327
328 sub done {
329         my ($self) = @_;
330         my $err = '';
331         if (my $im = delete($self->{im})) {
332                 eval { $im->done };
333                 if ($@) {
334                         $err .= "import done: $@\n";
335                         warn $err;
336                 }
337         }
338         $self->{priv_eidx}->done;
339         die $err if $err;
340 }
341
342 sub ipc_atfork_child {
343         my ($self) = @_;
344         my $lei = $self->{lei};
345         $lei->_lei_atfork_child(1) if $lei;
346         $self->SUPER::ipc_atfork_child;
347 }
348
349 sub write_prepare {
350         my ($self, $lei) = @_;
351         unless ($self->{-ipc_req}) {
352                 my $d = $lei->store_path;
353                 $self->ipc_lock_init("$d/ipc.lock");
354                 substr($d, -length('/lei/store'), 10, '');
355                 # Mail we import into lei are private, so headers filtered out
356                 # by -mda for public mail are not appropriate
357                 local @PublicInbox::MDA::BAD_HEADERS = ();
358                 $self->ipc_worker_spawn("lei/store $d", $lei->oldset,
359                                         { lei => $lei });
360         }
361         $lei->{sto} = $self;
362 }
363
364 1;