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