]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2writable: warn on duplicate Message-IDs
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
6 use strict;
7 use warnings;
8 use Fcntl qw(:flock :DEFAULT);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::SearchIdxThread;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use Email::MIME::ContentType;
15 $Email::MIME::ContentType::STRICT_PARAMS = 0;
16
17 # an estimate of the post-packed size to the raw uncompressed size
18 my $PACKING_FACTOR = 0.4;
19
20 # assume 2 cores if GNU nproc(1) is not available
21 my $NPROC = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
22
23 sub new {
24         my ($class, $v2ibx, $creat) = @_;
25         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
26         unless (-d $dir) {
27                 if ($creat) {
28                         require File::Path;
29                         File::Path::mkpath($dir);
30                 } else {
31                         die "$dir does not exist\n";
32                 }
33         }
34         my $self = {
35                 -inbox => $v2ibx,
36                 im => undef, #  PublicInbox::Import
37                 xap_rw => undef, # PublicInbox::V2SearchIdx
38                 xap_ro => undef,
39                 partitions => $NPROC,
40                 transact_bytes => 0,
41                 # limit each repo to 1GB or so
42                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
43         };
44         bless $self, $class
45 }
46
47 # returns undef on duplicate or spam
48 # mimics Import::add and wraps it for v2
49 sub add {
50         my ($self, $mime, $check_cb) = @_;
51         my $existing = $self->lookup_content($mime);
52
53         if ($existing) {
54                 return undef if $existing->type eq 'mail'; # duplicate
55         }
56
57         my $im = $self->importer;
58
59         # im->add returns undef if check_cb fails
60         my $cmt = $im->add($mime, $check_cb) or return;
61         $cmt = $im->get_mark($cmt);
62         my $oid = $im->{last_object_id};
63         my ($len, $msgref) = @{$im->{last_object}};
64
65         $self->idx_init;
66         my $num = $self->{all}->index_mm($mime, 1);
67         my $nparts = $self->{partitions};
68         my $part = $num % $nparts;
69         my $idx = $self->idx_part($part);
70         $idx->index_raw($len, $msgref, $num, $oid);
71         my $n = $self->{transact_bytes} += $len;
72         if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
73                 $self->checkpoint;
74         }
75
76         $mime;
77 }
78
79 sub idx_part {
80         my ($self, $part) = @_;
81         $self->{idx_parts}->[$part];
82 }
83
84 sub idx_init {
85         my ($self) = @_;
86         return if $self->{idx_parts};
87         # first time initialization:
88         my $all = $self->{all} =
89                 PublicInbox::SearchIdxThread->new($self->{-inbox});
90
91         # need to create all parts before initializing msgmap FD
92         my $max = $self->{partitions} - 1;
93         my $idx = $self->{idx_parts} = [];
94         for my $i (0..$max) {
95                 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
96         }
97         $all->_msgmap_init->{dbh}->begin_work;
98 }
99
100 sub remove {
101         my ($self, $mime, $msg) = @_;
102         my $existing = $self->lookup_content($mime) or return;
103
104         # don't touch ghosts or already junked messages
105         return unless $existing->type eq 'mail';
106
107         # always write removals to the current (latest) git repo since
108         # we process chronologically
109         my $im = $self->importer;
110         my ($cmt, undef) = $im->remove($mime, $msg);
111         $cmt = $im->get_mark($cmt);
112         $self->unindex_msg($existing, $cmt);
113 }
114
115 sub done {
116         my ($self) = @_;
117         my $im = $self->{im};
118         $im->done if $im; # PublicInbox::Import::done
119         $self->searchidx_checkpoint(0);
120 }
121
122 sub checkpoint {
123         my ($self) = @_;
124         my $im = $self->{im};
125         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
126         $self->searchidx_checkpoint(1);
127 }
128
129 sub searchidx_checkpoint {
130         my ($self, $more) = @_;
131
132         # order matters, we can only close {all} after all partitions
133         # are done because the partitions also write to {all}
134
135         if (my $parts = $self->{idx_parts}) {
136                 foreach my $idx (@$parts) {
137                         $idx->remote_commit;
138                         $idx->remote_close unless $more;
139                 }
140                 delete $self->{idx_parts} unless $more;
141         }
142
143         if (my $all = $self->{all}) {
144                 $all->{mm}->{dbh}->commit;
145                 if ($more) {
146                         $all->{mm}->{dbh}->begin_work;
147                 }
148                 $all->remote_commit;
149                 $all->remote_close unless $more;
150                 delete $self->{all} unless $more;
151         }
152         $self->{transact_bytes} = 0;
153 }
154
155 sub git_init {
156         my ($self, $new) = @_;
157         my $pfx = "$self->{-inbox}->{mainrepo}/git";
158         my $git_dir = "$pfx/$new.git";
159         die "$git_dir exists\n" if -e $git_dir;
160         my @cmd = (qw(git init --bare -q), $git_dir);
161         PublicInbox::Import::run_die(\@cmd);
162         @cmd = (qw/git config/, "--file=$git_dir/config",
163                         'repack.writeBitmaps', 'true');
164         PublicInbox::Import::run_die(\@cmd);
165
166         my $all = "$self->{-inbox}->{mainrepo}/all.git";
167         unless (-d $all) {
168                 @cmd = (qw(git init --bare -q), $all);
169                 PublicInbox::Import::run_die(\@cmd);
170         }
171
172         my $alt = "$all/objects/info/alternates";
173         my $new_obj_dir = "../../git/$new.git/objects";
174         my %alts;
175         if (-e $alt) {
176                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
177                 %alts = map { chomp; $_ => 1 } (<$fh>);
178         }
179         return $git_dir if $alts{$new_obj_dir};
180         open my $fh, '>>', $alt or die "open >> $alt: $!\n";
181         print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
182         close $fh or die "close $alt: $!\n";
183         $git_dir
184 }
185
186 sub importer {
187         my ($self) = @_;
188         my $im = $self->{im};
189         if ($im) {
190                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
191                         return $im;
192                 } else {
193                         $self->{im} = undef;
194                         $im->done;
195                         $self->searchidx_checkpoint(1);
196                         $im = undef;
197                         my $git_dir = $self->git_init(++$self->{max_git});
198                         my $git = PublicInbox::Git->new($git_dir);
199                         return $self->import_init($git, 0);
200                 }
201         }
202         my $latest;
203         my $max = -1;
204         my $new = 0;
205         my $pfx = "$self->{-inbox}->{mainrepo}/git";
206         if (-d $pfx) {
207                 foreach my $git_dir (glob("$pfx/*.git")) {
208                         $git_dir =~ m!/(\d+)\.git\z! or next;
209                         my $n = $1;
210                         if ($n > $max) {
211                                 $max = $n;
212                                 $latest = $git_dir;
213                         }
214                 }
215         }
216         if (defined $latest) {
217                 my $git = PublicInbox::Git->new($latest);
218                 my $packed_bytes = $git->packed_bytes;
219                 if ($packed_bytes >= $self->{rotate_bytes}) {
220                         $new = $max + 1;
221                 } else {
222                         $self->{max_git} = $max;
223                         return $self->import_init($git, $packed_bytes);
224                 }
225         }
226         $self->{max_git} = $new;
227         $latest = $self->git_init($new);
228         $self->import_init(PublicInbox::Git->new($latest), 0);
229 }
230
231 sub import_init {
232         my ($self, $git, $packed_bytes) = @_;
233         my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
234         $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
235         $im->{want_object_id} = 1;
236         $im->{ssoma_lock} = 0;
237         $im->{path_type} = 'v2';
238         $self->{im} = $im;
239 }
240
241 sub lookup_content {
242         undef # TODO
243 }
244
245 1;