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);
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) {
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';
99 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
104 my $eidx = $self->{priv_eidx};
105 $eidx->idx_init({-private => 1});
109 sub _docids_for ($$) {
110 my ($self, $eml) = @_;
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) {
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";
127 local $self->{current_info} = $oid;
128 my $x = PublicInbox::Eml->new($bref);
129 $docids{$docid} = $docid if content_hash($x) eq $chash;
132 sort { $a <=> $b } values %docids;
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);
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);
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);
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
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);
183 @$vivify_xvmd = sort { $a <=> $b } keys(%docids);
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);
200 for my $oid (keys %$xoids) {
201 $oidx->add_xref3($docid, -1, $oid, '.');
203 $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
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;
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;
228 my ($self, $eml, $vmd, $xoids) = @_;
229 add_eml($self, $eml, $vmd, $xoids) //
230 set_eml_vmd($self, $eml, $vmd);
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
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, '.');
250 my $idx = $eidx->idx_shard($smsg->{num});
251 $idx->index_eml(PublicInbox::Eml->new("\n\n"), $smsg);
256 my ($self, $xoids, $eml, $vmd_mod) = @_;
257 my $eidx = eidx_init($self);
258 my $oidx = $eidx->{oidx};
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);
269 delete $xoids->{$oid};
271 return unless scalar(keys(%$xoids));
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, '.');
280 my $idx = $eidx->idx_shard($docid);
281 $idx->ipc_do('update_vmd', $docid, $vmd_mod);
286 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
287 $idx->ipc_do('update_vmd', $smsg->{num}, $vmd_mod);
290 # set or update keywords for external message, called via ipc_do
292 my ($self, $xoids, $eml, $vmd) = @_;
294 my $eidx = eidx_init($self);
295 my $oidx = $eidx->{oidx};
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);
308 delete $xoids->{$oid}; # all done with this oid
310 return unless scalar(keys(%$xoids));
312 # n.b. we don't do _docids_for here, we expect the caller
313 # already checked $lse->kw_changed before calling this sub
315 return unless (@{$vmd->{kw} // []}) || (@{$vmd->{L} // []});
317 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
318 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd);
322 my ($self, $wait) = @_;
323 if (my $im = $self->{im}) {
324 $wait ? $im->barrier : $im->checkpoint;
326 $self->{priv_eidx}->checkpoint($wait);
332 if (my $im = delete($self->{im})) {
335 $err .= "import done: $@\n";
339 $self->{priv_eidx}->done;
343 sub ipc_atfork_child {
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;
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,