]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
lei_store: quiet down "git var" failures
[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::SearchIdx qw(crlf_adjust);
14 use PublicInbox::ExtSearchIdx;
15 use PublicInbox::Import;
16 use PublicInbox::InboxWritable;
17 use PublicInbox::V2Writable;
18 use PublicInbox::ContentHash qw(content_hash content_digest);
19 use PublicInbox::MID qw(mids mids_in);
20 use PublicInbox::LeiSearch;
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         if ($opt->{creat}) {
28                 PublicInbox::SearchIdx::load_xapian_writable();
29                 eidx_init($self);
30         }
31         $self;
32 }
33
34 sub git { $_[0]->{priv_eidx}->git } # read-only
35
36 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
37
38 sub rotate_bytes {
39         $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
40 }
41
42 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
43
44 sub git_epoch_max  {
45         my ($self) = @_;
46         if (opendir(my $dh, $self->git_pfx)) {
47                 max(map {
48                         substr($_, 0, -4) + 0; # drop ".git" suffix
49                 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
50         } else {
51                 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
52         }
53 }
54
55 sub git_ident ($) {
56         my ($git) = @_;
57         open my $null, '>', '/dev/null' or die "open /dev/null: $!";
58         my $opt = { 2 => $null };
59         chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT), undef, $opt));
60         warn "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
61         $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
62                 ('lei user', 'x@example.com')
63 }
64
65 sub importer {
66         my ($self) = @_;
67         my $max;
68         my $im = $self->{im};
69         if ($im) {
70                 return $im if $im->{bytes_added} < $self->rotate_bytes;
71
72                 delete $self->{im};
73                 $im->done;
74                 undef $im;
75                 $self->checkpoint;
76                 $max = $self->git_epoch_max + 1;
77         }
78         my $pfx = $self->git_pfx;
79         $max //= $self->git_epoch_max;
80         while (1) {
81                 my $latest = "$pfx/$max.git";
82                 my $old = -e $latest;
83                 PublicInbox::Import::init_bare($latest);
84                 my $git = PublicInbox::Git->new($latest);
85                 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
86                 my $packed_bytes = $git->packed_bytes;
87                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
88                 if ($unpacked_bytes >= $self->rotate_bytes) {
89                         $max++;
90                         next;
91                 }
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';
97                 return $im;
98         }
99 }
100
101 sub search {
102         PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
103 }
104
105 sub eidx_init {
106         my ($self) = @_;
107         my $eidx = $self->{priv_eidx};
108         $eidx->idx_init({-private => 1});
109         $eidx;
110 }
111
112 # when a message has no Message-IDs at all, this is needed for
113 # unsent Draft messages, at least
114 sub _fake_mid_for ($$) {
115         my ($eml, $dig) = @_;
116         my $mids = mids_in($eml, qw(X-Alt-Message-ID Resent-Message-ID));
117         $eml->{-lei_fake_mid} =
118                 $mids->[0] // PublicInbox::Import::digest2mid($dig, $eml);
119 }
120
121 sub _docids_for ($$) {
122         my ($self, $eml) = @_;
123         my %docids;
124         my $dig = content_digest($eml);
125         my $chash = $dig->clone->digest;
126         my $eidx = eidx_init($self);
127         my $oidx = $eidx->{oidx};
128         my $im = $self->{im};
129         my $mids = mids($eml);
130         $mids->[0] //= _fake_mid_for($eml, $dig);
131         for my $mid (@$mids) {
132                 my ($id, $prev);
133                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
134                         my $oid = $cur->{blob};
135                         my $docid = $cur->{num};
136                         my $bref = $im ? $im->cat_blob($oid) : undef;
137                         $bref //= $eidx->git->cat_file($oid) // do {
138                                 warn "W: $oid (#$docid) <$mid> not found\n";
139                                 next;
140                         };
141                         local $self->{current_info} = $oid;
142                         my $x = PublicInbox::Eml->new($bref);
143                         $docids{$docid} = $docid if content_hash($x) eq $chash;
144                 }
145         }
146         sort { $a <=> $b } values %docids;
147 }
148
149 sub set_eml_keywords {
150         my ($self, $eml, @kw) = @_;
151         my $eidx = eidx_init($self);
152         my @docids = _docids_for($self, $eml);
153         for my $docid (@docids) {
154                 $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
155         }
156         \@docids;
157 }
158
159 sub add_eml_keywords {
160         my ($self, $eml, @kw) = @_;
161         my $eidx = eidx_init($self);
162         my @docids = _docids_for($self, $eml);
163         for my $docid (@docids) {
164                 $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
165         }
166         \@docids;
167 }
168
169 sub remove_eml_keywords {
170         my ($self, $eml, @kw) = @_;
171         my $eidx = eidx_init($self);
172         my @docids = _docids_for($self, $eml);
173         for my $docid (@docids) {
174                 $eidx->idx_shard($docid)->shard_remove_keywords($docid, @kw);
175         }
176         \@docids;
177 }
178
179 # cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
180 my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
181 # O (old/non-recent), and D (deleted) aren't in JMAP,
182 # so probably won't be supported by us.
183 sub mbox_keywords {
184         my $eml = $_[-1];
185         my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
186         my %kw;
187         $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
188         sort(keys %kw);
189 }
190
191 # cf: https://cr.yp.to/proto/maildir.html
192 my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
193 sub maildir_keywords {
194         $_[-1] =~ /:2,([A-Z]+)\z/i ?
195                 sort(map { $c2kw{$_} // () } split(//, $1)) : ();
196 }
197
198 sub add_eml {
199         my ($self, $eml, @kw) = @_;
200         my $eidx = eidx_init($self);
201         my $oidx = $eidx->{oidx};
202         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
203         my $im = $self->importer;
204         $im->add($eml, undef, $smsg) or return; # duplicate returns undef
205         my $msgref = delete $smsg->{-raw_email};
206         $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
207
208         local $self->{current_info} = $smsg->{blob};
209         if (my @docids = _docids_for($self, $eml)) {
210                 for my $docid (@docids) {
211                         my $idx = $eidx->idx_shard($docid);
212                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
213                         $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
214                         $idx->shard_add_keywords($docid, @kw) if @kw;
215                 }
216                 \@docids;
217         } else {
218                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
219                 $oidx->add_overview($eml, $smsg);
220                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
221                 my $idx = $eidx->idx_shard($smsg->{num});
222                 $idx->index_raw($msgref, $eml, $smsg);
223                 $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
224                 $smsg;
225         }
226 }
227
228 sub set_eml {
229         my ($self, $eml, @kw) = @_;
230         add_eml($self, $eml, @kw) // set_eml_keywords($self, $eml, @kw);
231 }
232
233 sub done {
234         my ($self) = @_;
235         my $err = '';
236         if (my $im = delete($self->{im})) {
237                 eval { $im->done };
238                 if ($@) {
239                         $err .= "import done: $@\n";
240                         warn $err;
241                 }
242         }
243         $self->{priv_eidx}->done;
244         die $err if $err;
245 }
246
247 1;