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>
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
8 # for xref3, the following are constant: $eidx_key = '.', $xnum = -1
9 package PublicInbox::LeiStore;
12 use parent qw(PublicInbox::Lock PublicInbox::IPC);
13 use PublicInbox::ExtSearchIdx;
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;
22 use List::Util qw(max);
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};
32 sub git { $_[0]->{priv_eidx}->git } # read-only
34 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
37 $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
40 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
44 if (opendir(my $dh, $self->git_pfx)) {
46 substr($_, 0, -4) + 0; # drop ".git" suffix
47 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
49 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
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')
67 return $im if $im->{bytes_added} < $self->rotate_bytes;
73 $max = $self->git_epoch_max + 1;
75 my $pfx = $self->git_pfx;
76 $max //= $self->git_epoch_max;
78 my $latest = "$pfx/$max.git";
80 PublicInbox::Import::init_bare($latest);
81 my $git = PublicInbox::Git->new($latest);
83 $git->qx(qw(config core.sharedRepository 0600));
84 $self->done; # force eidx_init on next round
86 my $packed_bytes = $git->packed_bytes;
87 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
88 if ($unpacked_bytes >= $self->rotate_bytes) {
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';
102 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
107 my $eidx = $self->{priv_eidx};
108 $eidx->idx_init({-private => 1});
112 sub _docids_for ($$) {
113 my ($self, $eml) = @_;
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) {
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";
130 local $self->{current_info} = $oid;
131 my $x = PublicInbox::Eml->new($bref);
132 $docids{$docid} = $docid if content_hash($x) eq $chash;
135 sort { $a <=> $b } values %docids;
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);
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);
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);
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
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);
186 @$vivify_xvmd = sort { $a <=> $b } keys(%docids);
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);
203 for my $oid (keys %$xoids) {
204 $oidx->add_xref3($docid, -1, $oid, '.');
206 $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
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;
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;
231 my ($self, $eml, $vmd, $xoids) = @_;
232 add_eml($self, $eml, $vmd, $xoids) //
233 set_eml_vmd($self, $eml, $vmd);
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
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, '.');
253 my $idx = $eidx->idx_shard($smsg->{num});
254 $idx->index_eml(PublicInbox::Eml->new("\n\n"), $smsg);
259 my ($self, $xoids, $eml, $vmd_mod) = @_;
260 my $eidx = eidx_init($self);
261 my $oidx = $eidx->{oidx};
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);
272 delete $xoids->{$oid};
274 return unless scalar(keys(%$xoids));
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, '.');
283 my $idx = $eidx->idx_shard($docid);
284 $idx->ipc_do('update_vmd', $docid, $vmd_mod);
289 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
290 $idx->ipc_do('update_vmd', $smsg->{num}, $vmd_mod);
293 # set or update keywords for external message, called via ipc_do
295 my ($self, $xoids, $eml, $vmd) = @_;
297 my $eidx = eidx_init($self);
298 my $oidx = $eidx->{oidx};
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);
311 delete $xoids->{$oid}; # all done with this oid
313 return unless scalar(keys(%$xoids));
315 # n.b. we don't do _docids_for here, we expect the caller
316 # already checked $lse->kw_changed before calling this sub
318 return unless (@{$vmd->{kw} // []}) || (@{$vmd->{L} // []});
320 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
321 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd);
325 my ($self, $wait) = @_;
326 if (my $im = $self->{im}) {
327 $wait ? $im->barrier : $im->checkpoint;
329 $self->{priv_eidx}->checkpoint($wait);
335 if (my $im = delete($self->{im})) {
338 $err .= "import done: $@\n";
342 $self->{priv_eidx}->done;
346 sub ipc_atfork_child {
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;
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,