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