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