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;
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;
21 use List::Util qw(max);
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};
31 sub git { $_[0]->{priv_eidx}->git } # read-only
33 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
36 $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
39 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
43 if (opendir(my $dh, $self->git_pfx)) {
45 substr($_, 0, -4) + 0; # drop ".git" suffix
46 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
48 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
55 open $rdr->{2}, '>', '/dev/null' or die "open /dev/null: $!";
56 chomp(my $i = $git->qx([qw(var GIT_COMMITTER_IDENT)], undef, $rdr));
57 $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
58 ('lei user', 'x@example.com')
66 return $im if $im->{bytes_added} < $self->rotate_bytes;
72 $max = $self->git_epoch_max + 1;
74 my $pfx = $self->git_pfx;
75 $max //= $self->git_epoch_max;
77 my $latest = "$pfx/$max.git";
79 PublicInbox::Import::init_bare($latest);
80 my $git = PublicInbox::Git->new($latest);
81 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
82 my $packed_bytes = $git->packed_bytes;
83 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
84 if ($unpacked_bytes >= $self->rotate_bytes) {
88 my ($n, $e) = git_ident($git);
89 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
90 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
91 $im->{lock_path} = undef;
92 $im->{path_type} = 'v2';
98 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
103 my $eidx = $self->{priv_eidx};
104 $eidx->idx_init({-private => 1});
108 sub _docids_for ($$) {
109 my ($self, $eml) = @_;
111 my ($chash, $mids) = PublicInbox::LeiSearch::content_key($eml);
112 my $eidx = eidx_init($self);
113 my $oidx = $eidx->{oidx};
114 my $im = $self->{im};
115 for my $mid (@$mids) {
117 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
118 next if $cur->{bytes} == 0; # external-only message
119 my $oid = $cur->{blob};
120 my $docid = $cur->{num};
121 my $bref = $im ? $im->cat_blob($oid) : undef;
122 $bref //= $eidx->git->cat_file($oid) // do {
123 warn "W: $oid (#$docid) <$mid> not found\n";
126 local $self->{current_info} = $oid;
127 my $x = PublicInbox::Eml->new($bref);
128 $docids{$docid} = $docid if content_hash($x) eq $chash;
131 sort { $a <=> $b } values %docids;
135 my ($self, $eml, $vmd, $docids) = @_;
136 my $eidx = eidx_init($self);
137 $docids //= [ _docids_for($self, $eml) ];
138 for my $docid (@$docids) {
139 $eidx->idx_shard($docid)->ipc_do('set_vmd', $docid, $vmd);
145 my ($self, $eml, $vmd) = @_;
146 my $eidx = eidx_init($self);
147 my @docids = _docids_for($self, $eml);
148 for my $docid (@docids) {
149 $eidx->idx_shard($docid)->ipc_do('add_vmd', $docid, $vmd);
155 my ($self, $eml, $vmd) = @_;
156 my $eidx = eidx_init($self);
157 my @docids = _docids_for($self, $eml);
158 for my $docid (@docids) {
159 $eidx->idx_shard($docid)->ipc_do('remove_vmd', $docid, $vmd);
165 my ($self, $eml, $vmd, $xoids) = @_;
166 my $im = $self->importer; # may create new epoch
167 my $eidx = eidx_init($self); # writes ALL.git/objects/info/alternates
168 my $oidx = $eidx->{oidx}; # PublicInbox::Import::add checks this
169 my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
170 $im->add($eml, undef, $smsg) or return; # duplicate returns undef
172 local $self->{current_info} = $smsg->{blob};
173 my $vivify_xvmd = delete($smsg->{-vivify_xvmd}) // []; # exact matches
174 if ($xoids) { # fuzzy matches from externals in ale->xoids_for
175 delete $xoids->{$smsg->{blob}}; # added later
176 if (scalar keys %$xoids) {
177 my %docids = map { $_ => 1 } @$vivify_xvmd;
178 for my $oid (keys %$xoids) {
179 my @id = $oidx->blob_exists($oid);
182 @$vivify_xvmd = sort { $a <=> $b } keys(%docids);
187 $xoids->{$smsg->{blob}} = 1;
188 for my $docid (@$vivify_xvmd) {
189 my $cur = $oidx->get_art($docid);
190 my $idx = $eidx->idx_shard($docid);
191 if (!$cur || $cur->{bytes} == 0) { # really vivifying
192 $smsg->{num} = $docid;
193 $oidx->add_overview($eml, $smsg);
194 $smsg->{-merge_vmd} = 1;
195 $idx->index_eml($eml, $smsg);
196 } else { # lse fuzzy hit off ale
197 $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
199 for my $oid (keys %$xoids) {
200 $oidx->add_xref3($docid, -1, $oid, '.');
202 $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
205 } elsif (my @docids = _docids_for($self, $eml)) {
206 # fuzzy match from within lei/store
207 for my $docid (@docids) {
208 my $idx = $eidx->idx_shard($docid);
209 $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
210 # add_eidx_info for List-Id
211 $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
212 $idx->ipc_do('add_vmd', $docid, $vmd) if $vmd;
215 } else { # totally new message
216 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
217 $oidx->add_overview($eml, $smsg);
218 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
219 my $idx = $eidx->idx_shard($smsg->{num});
220 $idx->index_eml($eml, $smsg);
221 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd) if $vmd;
227 my ($self, $eml, $vmd, $xoids) = @_;
228 add_eml($self, $eml, $vmd, $xoids) //
229 set_eml_vmd($self, $eml, $vmd);
232 sub _external_only ($$$) {
233 my ($self, $xoids, $eml) = @_;
234 my $eidx = $self->{priv_eidx};
235 my $oidx = $eidx->{oidx} // die 'BUG: {oidx} missing';
236 my $smsg = bless { blob => '' }, 'PublicInbox::Smsg';
237 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
238 # save space for an externals-only message
239 my $hdr = $eml->header_obj;
240 $smsg->populate($hdr); # sets lines == 0
242 delete @$smsg{qw(From Subject)};
243 $smsg->{to} = $smsg->{cc} = $smsg->{from} = '';
244 $oidx->add_overview($hdr, $smsg); # subject+references for threading
245 $smsg->{subject} = '';
246 for my $oid (keys %$xoids) {
247 $oidx->add_xref3($smsg->{num}, -1, $oid, '.');
249 my $idx = $eidx->idx_shard($smsg->{num});
250 $idx->index_eml(PublicInbox::Eml->new("\n\n"), $smsg);
255 my ($self, $xoids, $eml, $vmd_mod) = @_;
256 my $eidx = eidx_init($self);
257 my $oidx = $eidx->{oidx};
259 for my $oid (keys %$xoids) {
260 my @docids = $oidx->blob_exists($oid) or next;
261 scalar(@docids) > 1 and
262 warn "W: $oid indexed as multiple docids: @docids\n";
263 for my $docid (@docids) {
264 next if $seen{$docid}++;
265 my $idx = $eidx->idx_shard($docid);
266 $idx->ipc_do('update_vmd', $docid, $vmd_mod);
268 delete $xoids->{$oid};
270 return unless scalar(keys(%$xoids));
272 # see if it was indexed, but with different OID(s)
273 if (my @docids = _docids_for($self, $eml)) {
274 for my $docid (@docids) {
275 next if $seen{$docid};
276 for my $oid (keys %$xoids) {
277 $oidx->add_xref3($docid, -1, $oid, '.');
279 my $idx = $eidx->idx_shard($docid);
280 $idx->ipc_do('update_vmd', $docid, $vmd_mod);
285 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
286 $idx->ipc_do('update_vmd', $smsg->{num}, $vmd_mod);
289 # set or update keywords for external message, called via ipc_do
291 my ($self, $xoids, $eml, $vmd) = @_;
293 my $eidx = eidx_init($self);
294 my $oidx = $eidx->{oidx};
297 # see if we can just update existing docs
298 for my $oid (keys %$xoids) {
299 my @docids = $oidx->blob_exists($oid) or next;
300 scalar(@docids) > 1 and
301 warn "W: $oid indexed as multiple docids: @docids\n";
302 for my $docid (@docids) {
303 next if $seen{$docid}++;
304 my $idx = $eidx->idx_shard($docid);
305 $idx->ipc_do('set_vmd', $docid, $vmd);
307 delete $xoids->{$oid}; # all done with this oid
309 return unless scalar(keys(%$xoids));
311 # n.b. we don't do _docids_for here, we expect the caller
312 # already checked $lse->kw_changed before calling this sub
314 return unless (@{$vmd->{kw} // []}) || (@{$vmd->{L} // []});
316 my ($smsg, $idx) = _external_only($self, $xoids, $eml);
317 $idx->ipc_do('add_vmd', $smsg->{num}, $vmd);
321 my ($self, $wait) = @_;
322 if (my $im = $self->{im}) {
323 $wait ? $im->barrier : $im->checkpoint;
325 $self->{priv_eidx}->checkpoint($wait);
331 if (my $im = delete($self->{im})) {
334 $err .= "import done: $@\n";
338 $self->{priv_eidx}->done;
342 sub ipc_atfork_child {
344 my $lei = $self->{lei};
345 $lei->_lei_atfork_child(1) if $lei;
346 $self->SUPER::ipc_atfork_child;
350 my ($self, $lei) = @_;
351 unless ($self->{-ipc_req}) {
352 my $d = $lei->store_path;
353 $self->ipc_lock_init("$d/ipc.lock");
354 substr($d, -length('/lei/store'), 10, '');
355 # Mail we import into lei are private, so headers filtered out
356 # by -mda for public mail are not appropriate
357 local @PublicInbox::MDA::BAD_HEADERS = ();
358 $self->ipc_worker_spawn("lei/store $d", $lei->oldset,