]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStore.pm
lei_store: local storage for Local Email Interface
[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
22 sub new {
23         my (undef, $dir, $opt) = @_;
24         my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
25         bless { priv_eidx => $eidx }, __PACKAGE__;
26 }
27
28 sub git { $_[0]->{priv_eidx}->git } # read-only
29
30 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
31
32 sub rotate_bytes {
33         $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
34 }
35
36 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
37
38 sub git_epoch_max  {
39         my ($self) = @_;
40         my $pfx = $self->git_pfx;
41         my $max = 0;
42         return $max unless -d $pfx ;
43         opendir my $dh, $pfx or die "opendir $pfx: $!\n";
44         while (defined(my $git_dir = readdir($dh))) {
45                 $git_dir =~ m!\A([0-9]+)\.git\z! or next;
46                 $max = $1 + 0 if $1 > $max;
47         }
48         $max;
49 }
50
51 sub importer {
52         my ($self) = @_;
53         my $max;
54         my $im = $self->{im};
55         if ($im) {
56                 return $im if $im->{bytes_added} < $self->rotate_bytes;
57
58                 delete $self->{im};
59                 $im->done;
60                 undef $im;
61                 $self->checkpoint;
62                 $max = $self->git_epoch_max + 1;
63         }
64         my $pfx = $self->git_pfx;
65         $max //= $self->git_epoch_max;
66         while (1) {
67                 my $latest = "$pfx/$max.git";
68                 my $old = -e $latest;
69                 my $git = PublicInbox::Git->new($latest);
70                 PublicInbox::Import::init_bare({ git => $git });
71                 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
72                 my $packed_bytes = $git->packed_bytes;
73                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
74                 if ($unpacked_bytes >= $self->rotate_bytes) {
75                         $max++;
76                         next;
77                 }
78                 chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT)));
79                 die "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
80                 my ($n, $e) = ($i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/g)
81                         or die "could not extract name/email from `$i'\n";
82                 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
83                 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
84                 $im->{lock_path} = undef;
85                 $im->{path_type} = 'v2';
86                 return $im;
87         }
88 }
89
90 sub search {
91         PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
92 }
93
94 sub eidx_init {
95         my ($self) = @_;
96         my $eidx = $self->{priv_eidx};
97         $eidx->idx_init({-private => 1});
98         $eidx;
99 }
100
101 sub _docids_for ($$) {
102         my ($self, $eml) = @_;
103         my %docids;
104         my $chash = content_hash($eml);
105         my $eidx = eidx_init($self);
106         my $oidx = $eidx->{oidx};
107         my $im = $self->{im};
108         for my $mid (@{mids($eml)}) {
109                 my ($id, $prev);
110                 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
111                         my $oid = $cur->{blob};
112                         my $docid = $cur->{num};
113                         my $bref = $im ? $im->cat_blob($oid) : undef;
114                         $bref //= $eidx->git->cat_file($oid) // do {
115                                 warn "W: $oid (#$docid) <$mid> not found\n";
116                                 next;
117                         };
118                         local $self->{current_info} = $oid;
119                         my $x = PublicInbox::Eml->new($bref);
120                         $docids{$docid} = $docid if content_hash($x) eq $chash;
121                 }
122         }
123         sort { $a <=> $b } values %docids;
124 }
125
126 sub set_eml_keywords {
127         my ($self, $eml, @kw) = @_;
128         my $eidx = eidx_init($self);
129         my @docids = _docids_for($self, $eml);
130         for my $docid (@docids) {
131                 $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
132         }
133         \@docids;
134 }
135
136 sub add_eml_keywords {
137         my ($self, $eml, @kw) = @_;
138         my $eidx = eidx_init($self);
139         my @docids = _docids_for($self, $eml);
140         for my $docid (@docids) {
141                 $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
142         }
143         \@docids;
144 }
145
146 sub remove_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_remove_keywords($docid, @kw);
152         }
153         \@docids;
154 }
155
156 sub add_eml {
157         my ($self, $eml) = @_;
158         my $eidx = eidx_init($self);
159         my $oidx = $eidx->{oidx};
160         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
161         my $im = $self->importer;
162         $im->add($eml, undef, $smsg) or return; # duplicate returns undef
163         my $msgref = delete $smsg->{-raw_email};
164         $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
165
166         local $self->{current_info} = $smsg->{blob};
167         if (my @docids = _docids_for($self, $eml)) {
168                 for my $docid (@docids) {
169                         my $idx = $eidx->idx_shard($docid);
170                         $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
171                         $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
172                 }
173         } else {
174                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
175                 $oidx->add_overview($eml, $smsg);
176                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
177                 my $idx = $eidx->idx_shard($smsg->{num});
178                 $idx->index_raw($msgref, $eml, $smsg);
179         }
180         $smsg->{blob}
181 }
182
183 sub done {
184         my ($self) = @_;
185         my $err = '';
186         if (my $im = delete($self->{im})) {
187                 eval { $im->done };
188                 if ($@) {
189                         $err .= "import done: $@\n";
190                         warn $err;
191                 }
192         }
193         $self->{priv_eidx}->done;
194         die $err if $err;
195 }
196
197 1;