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;
21 my ($class, $v2ibx, $creat) = @_;
22 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
26 File::Path::mkpath($dir);
28 die "$dir does not exist\n";
33 im => undef, # PublicInbox::Import
34 xap_rw => undef, # PublicInbox::V2SearchIdx
38 # limit each repo to 1GB or so
39 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
44 # returns undef on duplicate or spam
45 # mimics Import::add and wraps it for v2
47 my ($self, $mime, $check_cb) = @_;
48 my $existing = $self->lookup_content($mime);
51 return undef if $existing->type eq 'mail'; # duplicate
54 my $im = $self->importer;
56 # im->add returns undef if check_cb fails
57 my $cmt = $im->add($mime, $check_cb) or return;
58 $cmt = $im->get_mark($cmt);
59 my $oid = $im->{last_object_id};
60 my ($len, $msgref) = @{$im->{last_object}};
62 my $nparts = $self->{partitions};
63 my $part = hex(substr($oid, 0, 8)) % $nparts;
64 my $idx = $self->idx_part($part);
65 my $all = $self->{all};
66 my $num = $all->index_mm($mime);
67 $idx->index_raw($len, $msgref, $num, $oid);
68 my $n = $self->{transact_bytes} += $len;
69 if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
77 my ($self, $part) = @_;
78 my $idx = $self->{idx_parts};
79 return $idx->[$part] if $idx; # fast path
81 # first time initialization:
82 my $all = $self->{all} =
83 PublicInbox::SearchIdxThread->new($self->{-inbox});
85 # need to create all parts before initializing msgmap FD
86 my $max = $self->{partitions} - 1;
87 $idx = $self->{idx_parts} = [];
89 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
91 $all->_msgmap_init->{dbh}->begin_work;
96 my ($self, $mime, $msg) = @_;
97 my $existing = $self->lookup_content($mime) or return;
99 # don't touch ghosts or already junked messages
100 return unless $existing->type eq 'mail';
102 # always write removals to the current (latest) git repo since
103 # we process chronologically
104 my $im = $self->importer;
105 my ($cmt, undef) = $im->remove($mime, $msg);
106 $cmt = $im->get_mark($cmt);
107 $self->unindex_msg($existing, $cmt);
112 my $im = $self->{im};
113 $im->done if $im; # PublicInbox::Import::done
114 $self->searchidx_checkpoint(0);
119 my $im = $self->{im};
120 $im->checkpoint if $im; # PublicInbox::Import::checkpoint
121 $self->searchidx_checkpoint(1);
124 sub searchidx_checkpoint {
125 my ($self, $more) = @_;
127 # order matters, we can only close {all} after all partitions
128 # are done because the partitions also write to {all}
130 my $parts = $self->{idx_parts};
131 foreach my $idx (@$parts) {
133 $idx->remote_close unless $more;
136 if (my $all = $self->{all}) {
137 $all->{mm}->{dbh}->commit;
139 $all->{mm}->{dbh}->begin_work;
142 $all->remote_close unless $more;
144 $self->{transact_bytes} = 0;
148 my ($self, $new) = @_;
149 my $pfx = "$self->{-inbox}->{mainrepo}/git";
150 my $git_dir = "$pfx/$new.git";
151 die "$git_dir exists\n" if -e $git_dir;
152 my @cmd = (qw(git init --bare -q), $git_dir);
153 PublicInbox::Import::run_die(\@cmd);
154 @cmd = (qw/git config/, "--file=$git_dir/config",
155 'repack.writeBitmaps', 'true');
156 PublicInbox::Import::run_die(\@cmd);
158 my $all = "$self->{-inbox}->{mainrepo}/all.git";
160 @cmd = (qw(git init --bare -q), $all);
161 PublicInbox::Import::run_die(\@cmd);
164 my $alt = "$all/objects/info/alternates";
165 my $new_obj_dir = "../../git/$new.git/objects";
168 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
169 %alts = map { chomp; $_ => 1 } (<$fh>);
171 return $git_dir if $alts{$new_obj_dir};
172 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
173 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
174 close $fh or die "close $alt: $!\n";
180 my $im = $self->{im};
182 if ($im->{bytes_added} < $self->{rotate_bytes}) {
187 $self->searchidx_checkpoint(1);
189 my $git_dir = $self->git_init(++$self->{max_git});
190 my $git = PublicInbox::Git->new($git_dir);
191 return $self->import_init($git, 0);
197 my $pfx = "$self->{-inbox}->{mainrepo}/git";
199 foreach my $git_dir (glob("$pfx/*.git")) {
200 $git_dir =~ m!/(\d+)\.git\z! or next;
208 if (defined $latest) {
209 my $git = PublicInbox::Git->new($latest);
210 my $packed_bytes = $git->packed_bytes;
211 if ($packed_bytes >= $self->{rotate_bytes}) {
214 $self->{max_git} = $max;
215 return $self->import_init($git, $packed_bytes);
218 $self->{max_git} = $new;
219 $latest = $self->git_init($new);
220 $self->import_init(PublicInbox::Git->new($latest), 0);
224 my ($self, $git, $packed_bytes) = @_;
225 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
226 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
227 $im->{want_object_id} = 1;
228 $im->{ssoma_lock} = 0;
229 $im->{path_type} = 'v2';