1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
8 use Fcntl qw(:flock :DEFAULT);
9 use PublicInbox::SearchIdxPart;
10 use PublicInbox::SearchIdxThread;
11 use PublicInbox::MIME;
13 use PublicInbox::Import;
14 use Email::MIME::ContentType;
15 $Email::MIME::ContentType::STRICT_PARAMS = 0;
17 # an estimate of the post-packed size to the raw uncompressed size
18 my $PACKING_FACTOR = 0.4;
20 # assume 2 cores if GNU nproc(1) is not available
21 my $NPROC = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
24 my ($class, $v2ibx, $creat) = @_;
25 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
29 File::Path::mkpath($dir);
31 die "$dir does not exist\n";
36 im => undef, # PublicInbox::Import
37 xap_rw => undef, # PublicInbox::V2SearchIdx
41 # limit each repo to 1GB or so
42 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
47 # returns undef on duplicate or spam
48 # mimics Import::add and wraps it for v2
50 my ($self, $mime, $check_cb) = @_;
51 my $existing = $self->lookup_content($mime);
54 return undef if $existing->type eq 'mail'; # duplicate
57 my $im = $self->importer;
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}};
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)) {
80 my ($self, $part) = @_;
81 $self->{idx_parts}->[$part];
86 return if $self->{idx_parts};
88 # first time initialization, first we create the threader pipe:
89 my $all = $self->{all} = PublicInbox::SearchIdxThread->new($self);
91 # need to create all parts before initializing msgmap FD
92 my $max = $self->{partitions} - 1;
93 my $idx = $self->{idx_parts} = [];
95 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
98 # Now that all subprocesses are up, we can open the FD for SQLite:
99 $all->_msgmap_init->{dbh}->begin_work;
103 my ($self, $mime, $msg) = @_;
104 my $existing = $self->lookup_content($mime) or return;
106 # don't touch ghosts or already junked messages
107 return unless $existing->type eq 'mail';
109 # always write removals to the current (latest) git repo since
110 # we process chronologically
111 my $im = $self->importer;
112 my ($cmt, undef) = $im->remove($mime, $msg);
113 $cmt = $im->get_mark($cmt);
114 $self->unindex_msg($existing, $cmt);
119 my $im = $self->{im};
120 $im->done if $im; # PublicInbox::Import::done
121 $self->searchidx_checkpoint(0);
126 my $im = $self->{im};
127 $im->checkpoint if $im; # PublicInbox::Import::checkpoint
128 $self->searchidx_checkpoint(1);
131 sub searchidx_checkpoint {
132 my ($self, $more) = @_;
134 # order matters, we can only close {all} after all partitions
135 # are done because the partitions also write to {all}
137 if (my $parts = $self->{idx_parts}) {
138 foreach my $idx (@$parts) {
140 $idx->remote_close unless $more;
142 delete $self->{idx_parts} unless $more;
145 if (my $all = $self->{all}) {
146 $all->{mm}->{dbh}->commit;
148 $all->{mm}->{dbh}->begin_work;
151 $all->remote_close unless $more;
152 delete $self->{all} unless $more;
154 $self->{transact_bytes} = 0;
158 my ($self, $new) = @_;
159 my $pfx = "$self->{-inbox}->{mainrepo}/git";
160 my $git_dir = "$pfx/$new.git";
161 die "$git_dir exists\n" if -e $git_dir;
162 my @cmd = (qw(git init --bare -q), $git_dir);
163 PublicInbox::Import::run_die(\@cmd);
164 @cmd = (qw/git config/, "--file=$git_dir/config",
165 'repack.writeBitmaps', 'true');
166 PublicInbox::Import::run_die(\@cmd);
168 my $all = "$self->{-inbox}->{mainrepo}/all.git";
170 @cmd = (qw(git init --bare -q), $all);
171 PublicInbox::Import::run_die(\@cmd);
174 my $alt = "$all/objects/info/alternates";
175 my $new_obj_dir = "../../git/$new.git/objects";
178 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
179 %alts = map { chomp; $_ => 1 } (<$fh>);
181 return $git_dir if $alts{$new_obj_dir};
182 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
183 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
184 close $fh or die "close $alt: $!\n";
190 my $im = $self->{im};
192 if ($im->{bytes_added} < $self->{rotate_bytes}) {
197 $self->searchidx_checkpoint(1);
199 my $git_dir = $self->git_init(++$self->{max_git});
200 my $git = PublicInbox::Git->new($git_dir);
201 return $self->import_init($git, 0);
207 my $pfx = "$self->{-inbox}->{mainrepo}/git";
209 foreach my $git_dir (glob("$pfx/*.git")) {
210 $git_dir =~ m!/(\d+)\.git\z! or next;
218 if (defined $latest) {
219 my $git = PublicInbox::Git->new($latest);
220 my $packed_bytes = $git->packed_bytes;
221 if ($packed_bytes >= $self->{rotate_bytes}) {
224 $self->{max_git} = $max;
225 return $self->import_init($git, $packed_bytes);
228 $self->{max_git} = $new;
229 $latest = $self->git_init($new);
230 $self->import_init(PublicInbox::Git->new($latest), 0);
234 my ($self, $git, $packed_bytes) = @_;
235 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
236 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
237 $im->{want_object_id} = 1;
238 $im->{ssoma_lock} = 0;
239 $im->{path_type} = 'v2';
249 if (my $parts = $self->{idx_parts}) {
250 $_->atfork_child foreach @$parts;
252 if (my $im = $self->{im}) {