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