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