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;
15 # an estimate of the post-packed size to the raw uncompressed size
16 my $PACKING_FACTOR = 0.4;
18 # assume 2 cores if GNU nproc(1) is not available
19 my $NPROC = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
22 my ($class, $v2ibx, $creat) = @_;
23 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
27 File::Path::mkpath($dir);
29 die "$dir does not exist\n";
34 im => undef, # PublicInbox::Import
35 xap_rw => undef, # PublicInbox::V2SearchIdx
39 # limit each repo to 1GB or so
40 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
45 # returns undef on duplicate or spam
46 # mimics Import::add and wraps it for v2
48 my ($self, $mime, $check_cb) = @_;
49 my $existing = $self->lookup_content($mime);
52 return undef if $existing->type eq 'mail'; # duplicate
55 my $im = $self->importer;
57 # im->add returns undef if check_cb fails
58 my $cmt = $im->add($mime, $check_cb) or return;
59 $cmt = $im->get_mark($cmt);
60 my $oid = $im->{last_object_id};
61 my ($len, $msgref) = @{$im->{last_object}};
64 my $num = $self->{all}->index_mm($mime, 1);
65 my $nparts = $self->{partitions};
66 my $part = $num % $nparts;
67 my $idx = $self->idx_part($part);
68 $idx->index_raw($len, $msgref, $num, $oid);
69 my $n = $self->{transact_bytes} += $len;
70 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
78 my ($self, $part) = @_;
79 $self->{idx_parts}->[$part];
84 return if $self->{idx_parts};
86 # first time initialization, first we create the threader pipe:
87 my $all = $self->{all} = PublicInbox::SearchIdxThread->new($self);
89 # need to create all parts before initializing msgmap FD
90 my $max = $self->{partitions} - 1;
91 my $idx = $self->{idx_parts} = [];
93 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
96 # Now that all subprocesses are up, we can open the FD for SQLite:
97 $all->_msgmap_init->{dbh}->begin_work;
101 my ($self, $mime, $msg) = @_;
102 my $existing = $self->lookup_content($mime) or return;
104 # don't touch ghosts or already junked messages
105 return unless $existing->type eq 'mail';
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);
117 my $im = $self->{im};
118 $im->done if $im; # PublicInbox::Import::done
119 $self->searchidx_checkpoint(0);
124 my $im = $self->{im};
125 $im->checkpoint if $im; # PublicInbox::Import::checkpoint
126 $self->searchidx_checkpoint(1);
129 sub searchidx_checkpoint {
130 my ($self, $more) = @_;
132 # order matters, we can only close {all} after all partitions
133 # are done because the partitions also write to {all}
135 if (my $parts = $self->{idx_parts}) {
136 foreach my $idx (@$parts) {
138 $idx->remote_close unless $more;
140 delete $self->{idx_parts} unless $more;
143 if (my $all = $self->{all}) {
144 $all->{mm}->{dbh}->commit;
146 $all->{mm}->{dbh}->begin_work;
149 $all->remote_close unless $more;
150 delete $self->{all} unless $more;
152 $self->{transact_bytes} = 0;
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);
166 my $all = "$self->{-inbox}->{mainrepo}/all.git";
168 @cmd = (qw(git init --bare -q), $all);
169 PublicInbox::Import::run_die(\@cmd);
172 my $alt = "$all/objects/info/alternates";
173 my $new_obj_dir = "../../git/$new.git/objects";
176 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
177 %alts = map { chomp; $_ => 1 } (<$fh>);
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";
188 my $im = $self->{im};
190 if ($im->{bytes_added} < $self->{rotate_bytes}) {
195 $self->searchidx_checkpoint(1);
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);
205 my $pfx = "$self->{-inbox}->{mainrepo}/git";
207 foreach my $git_dir (glob("$pfx/*.git")) {
208 $git_dir =~ m!/(\d+)\.git\z! or next;
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}) {
222 $self->{max_git} = $max;
223 return $self->import_init($git, $packed_bytes);
226 $self->{max_git} = $new;
227 $latest = $self->git_init($new);
228 $self->import_init(PublicInbox::Git->new($latest), 0);
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';
247 if (my $parts = $self->{idx_parts}) {
248 $_->atfork_child foreach @$parts;
250 if (my $im = $self->{im}) {