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