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::SearchIdx;
10 use PublicInbox::MIME;
12 use PublicInbox::Import;
13 use Email::MIME::ContentType;
14 $Email::MIME::ContentType::STRICT_PARAMS = 0;
16 # an estimate of the post-packed size to the raw uncompressed size
17 my $PACKING_FACTOR = 0.4;
20 my ($class, $v2ibx, $creat) = @_;
21 my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
25 File::Path::mkpath($dir);
27 die "$dir does not exist\n";
32 im => undef, # PublicInbox::Import
33 xap_rw => undef, # PublicInbox::V2SearchIdx
36 # limit each repo to 1GB or so
37 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
42 # returns undef on duplicate or spam
43 # mimics Import::add and wraps it for v2
45 my ($self, $mime, $check_cb) = @_;
46 my $existing = $self->lookup_content($mime);
49 return undef if $existing->type eq 'mail'; # duplicate
52 my $im = $self->importer;
54 # im->add returns undef if check_cb fails
55 my $cmt = $im->add($mime, $check_cb) or return;
56 $cmt = $im->get_mark($cmt);
57 my $oid = $im->{last_object_id};
58 my $size = $im->{last_object_size};
60 my $idx = $self->search_idx;
61 $idx->index_both($mime, $size, $oid);
62 $idx->{xdb}->set_metadata('last_commit', $cmt);
63 my $n = $self->{transact_bytes} += $size;
64 if ($n > PublicInbox::SearchIdx::BATCH_BYTES) {
73 $self->{idx} ||= eval {
74 my $idx = PublicInbox::SearchIdx->new($self->{-inbox}, 1);
75 my $mm = $idx->_msgmap_init;
76 $idx->_xdb_acquire->begin_transaction;
77 $self->{transact_bytes} = 0;
78 $mm->{dbh}->begin_work;
84 my ($self, $mime, $msg) = @_;
85 my $existing = $self->lookup_content($mime) or return;
87 # don't touch ghosts or already junked messages
88 return unless $existing->type eq 'mail';
90 # always write removals to the current (latest) git repo since
91 # we process chronologically
92 my $im = $self->importer;
93 my ($cmt, undef) = $im->remove($mime, $msg);
94 $cmt = $im->get_mark($cmt);
95 $self->unindex_msg($existing, $cmt);
100 my $im = $self->{im};
101 $im->done if $im; # PublicInbox::Import::done
102 $self->searchidx_checkpoint;
107 my $im = $self->{im};
108 $im->checkpoint if $im; # PublicInbox::Import::checkpoint
109 $self->searchidx_checkpoint;
112 sub searchidx_checkpoint {
114 my $idx = delete $self->{idx} or return;
116 $idx->{mm}->{dbh}->commit;
117 $idx->{xdb}->commit_transaction;
122 my ($self, $new) = @_;
123 my $pfx = "$self->{-inbox}->{mainrepo}/git";
124 my $git_dir = "$pfx/$new.git";
125 die "$git_dir exists\n" if -e $git_dir;
126 my @cmd = (qw(git init --bare -q), $git_dir);
127 PublicInbox::Import::run_die(\@cmd);
128 @cmd = (qw/git config/, "--file=$git_dir/config",
129 'repack.writeBitmaps', 'true');
130 PublicInbox::Import::run_die(\@cmd);
132 my $all = "$self->{-inbox}->{mainrepo}/all.git";
134 @cmd = (qw(git init --bare -q), $all);
135 PublicInbox::Import::run_die(\@cmd);
138 my $alt = "$all/objects/info/alternates";
139 my $new_obj_dir = "../../git/$new.git/objects";
142 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
143 %alts = map { chomp; $_ => 1 } (<$fh>);
145 return $git_dir if $alts{$new_obj_dir};
146 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
147 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
148 close $fh or die "close $alt: $!\n";
154 my $im = $self->{im};
156 if ($im->{bytes_added} < $self->{rotate_bytes}) {
161 $self->searchidx_checkpoint;
163 my $git_dir = $self->git_init(++$self->{max_git});
164 my $git = PublicInbox::Git->new($git_dir);
165 return $self->import_init($git, 0);
171 my $pfx = "$self->{-inbox}->{mainrepo}/git";
173 foreach my $git_dir (glob("$pfx/*.git")) {
174 $git_dir =~ m!/(\d+)\.git\z! or next;
182 if (defined $latest) {
183 my $git = PublicInbox::Git->new($latest);
184 my $packed_bytes = $git->packed_bytes;
185 if ($packed_bytes >= $self->{rotate_bytes}) {
188 $self->{max_git} = $max;
189 return $self->import_init($git, $packed_bytes);
192 $self->{max_git} = $new;
193 $latest = $self->git_init($new);
194 $self->import_init(PublicInbox::Git->new($latest), 0);
198 my ($self, $git, $packed_bytes) = @_;
199 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
200 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
201 $im->{want_object_id} = 1;
202 $im->{ssoma_lock} = 0;
203 $im->{path_type} = 'v2';