1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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
8 # for xref3, the following are constant: $eidx_key = '.', $xnum = -1
9 package PublicInbox::LeiStore;
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;
23 my (undef, $dir, $opt) = @_;
24 my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
25 bless { priv_eidx => $eidx }, __PACKAGE__;
28 sub git { $_[0]->{priv_eidx}->git } # read-only
30 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
33 $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
36 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
40 my $pfx = $self->git_pfx;
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;
56 return $im if $im->{bytes_added} < $self->rotate_bytes;
62 $max = $self->git_epoch_max + 1;
64 my $pfx = $self->git_pfx;
65 $max //= $self->git_epoch_max;
67 my $latest = "$pfx/$max.git";
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) {
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';
91 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
96 my $eidx = $self->{priv_eidx};
97 $eidx->idx_init({-private => 1});
101 sub _docids_for ($$) {
102 my ($self, $eml) = @_;
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)}) {
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";
118 local $self->{current_info} = $oid;
119 my $x = PublicInbox::Eml->new($bref);
120 $docids{$docid} = $docid if content_hash($x) eq $chash;
123 sort { $a <=> $b } values %docids;
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);
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);
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);
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);
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
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);
186 if (my $im = delete($self->{im})) {
189 $err .= "import done: $@\n";
193 $self->{priv_eidx}->done;