]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
Merge remote-tracking branch 'origin/master' into lorelei
[public-inbox.git] / lib / PublicInbox / LeiStore.pm
1 # Copyright (C) 2020 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);
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);
19 use PublicInbox::MID qw(mids);
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                 my $git = PublicInbox::Git->new($latest);
82                 PublicInbox::Import::init_bare({ git => $git });
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 sub _docids_for ($$) {
111         my ($self, $eml) = @_;
112         my %docids;
113         my $chash = content_hash($eml);
114         my $eidx = eidx_init($self);
115         my $oidx = $eidx->{oidx};
116         my $im = $self->{im};
117         for my $mid (@{mids($eml)}) {
118                 my ($id, $prev);
119                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
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";
125                                 next;
126                         };
127                         local $self->{current_info} = $oid;
128                         my $x = PublicInbox::Eml->new($bref);
129                         $docids{$docid} = $docid if content_hash($x) eq $chash;
130                 }
131         }
132         sort { $a <=> $b } values %docids;
133 }
134
135 sub set_eml_keywords {
136         my ($self, $eml, @kw) = @_;
137         my $eidx = eidx_init($self);
138         my @docids = _docids_for($self, $eml);
139         for my $docid (@docids) {
140                 $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
141         }
142         \@docids;
143 }
144
145 sub add_eml_keywords {
146         my ($self, $eml, @kw) = @_;
147         my $eidx = eidx_init($self);
148         my @docids = _docids_for($self, $eml);
149         for my $docid (@docids) {
150                 $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
151         }
152         \@docids;
153 }
154
155 sub remove_eml_keywords {
156         my ($self, $eml, @kw) = @_;
157         my $eidx = eidx_init($self);
158         my @docids = _docids_for($self, $eml);
159         for my $docid (@docids) {
160                 $eidx->idx_shard($docid)->shard_remove_keywords($docid, @kw);
161         }
162         \@docids;
163 }
164
165 # cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
166 my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
167 # O (old/non-recent), and D (deleted) aren't in JMAP,
168 # so probably won't be supported by us.
169 sub mbox_keywords {
170         my $eml = $_[-1];
171         my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
172         my %kw;
173         $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
174         sort(keys %kw);
175 }
176
177 # cf: https://cr.yp.to/proto/maildir.html
178 my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
179 sub maildir_keywords {
180         $_[-1] =~ /:2,([A-Z]+)\z/i ?
181                 sort(map { $c2kw{$_} // () } split(//, $1)) : ();
182 }
183
184 sub add_eml {
185         my ($self, $eml, @kw) = @_;
186         my $eidx = eidx_init($self);
187         my $oidx = $eidx->{oidx};
188         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
189         my $im = $self->importer;
190         $im->add($eml, undef, $smsg) or return; # duplicate returns undef
191         my $msgref = delete $smsg->{-raw_email};
192         $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
193
194         local $self->{current_info} = $smsg->{blob};
195         if (my @docids = _docids_for($self, $eml)) {
196                 for my $docid (@docids) {
197                         my $idx = $eidx->idx_shard($docid);
198                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
199                         $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
200                         $idx->shard_add_keywords($docid, @kw) if @kw;
201                 }
202         } else {
203                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
204                 $oidx->add_overview($eml, $smsg);
205                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
206                 my $idx = $eidx->idx_shard($smsg->{num});
207                 $idx->index_raw($msgref, $eml, $smsg);
208                 $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
209         }
210         $smsg->{blob}
211 }
212
213 sub done {
214         my ($self) = @_;
215         my $err = '';
216         if (my $im = delete($self->{im})) {
217                 eval { $im->done };
218                 if ($@) {
219                         $err .= "import done: $@\n";
220                         warn $err;
221                 }
222         }
223         $self->{priv_eidx}->done;
224         die $err if $err;
225 }
226
227 1;