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((100 * 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 $self->index_msg($mime, $existing, $cmt, $oid);
62 sub index_msg { # TODO
66 my ($self, $mime, $msg) = @_;
67 my $existing = $self->lookup_content($mime) or return;
69 # don't touch ghosts or already junked messages
70 return unless $existing->type eq 'mail';
72 # always write removals to the current (latest) git repo since
73 # we process chronologically
74 my $im = $self->importer;
75 my ($cmt, undef) = $im->remove($mime, $msg);
76 $cmt = $im->get_mark($cmt);
77 $self->unindex_msg($existing, $cmt);
82 $self->{im}->done; # PublicInbox::Import::done
87 $self->{im}->checkpoint; # PublicInbox::Import::checkpoint
91 my ($self, $new) = @_;
92 my $pfx = "$self->{-inbox}->{mainrepo}/git";
93 my $git_dir = "$pfx/$new.git";
94 die "$git_dir exists\n" if -e $git_dir;
95 my @cmd = (qw(git init --bare -q), $git_dir);
96 PublicInbox::Import::run_die(\@cmd);
97 @cmd = (qw/git config/, "--file=$git_dir/config",
98 'repack.writeBitmaps', 'true');
99 PublicInbox::Import::run_die(\@cmd);
101 my $all = "$self->{-inbox}->{mainrepo}/all.git";
103 @cmd = (qw(git init --bare -q), $all);
104 PublicInbox::Import::run_die(\@cmd);
107 my $alt = "$all/objects/info/alternates";
108 my $new_obj_dir = "../../git/$new.git/objects";
111 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
112 %alts = map { chomp; $_ => 1 } (<$fh>);
114 return $git_dir if $alts{$new_obj_dir};
115 open my $fh, '>>', $alt or die "open >> $alt: $!\n";
116 print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
117 close $fh or die "close $alt: $!\n";
123 my $im = $self->{im};
125 if ($im->{bytes_added} < $self->{rotate_bytes}) {
131 my $git_dir = $self->git_init(++$self->{max_git});
132 my $git = PublicInbox::Git->new($git_dir);
133 return $self->import_init($git, 0);
139 my $pfx = "$self->{-inbox}->{mainrepo}/git";
141 foreach my $git_dir (glob("$pfx/*.git")) {
142 $git_dir =~ m!/(\d+)\.git\z! or next;
150 if (defined $latest) {
151 my $git = PublicInbox::Git->new($latest);
152 my $packed_bytes = $git->packed_bytes;
153 if ($packed_bytes >= $self->{rotate_bytes}) {
156 $self->{max_git} = $max;
157 return $self->import_init($git, $packed_bytes);
160 warn "latest not found in $pfx\n";
162 $self->{max_git} = $new;
163 $latest = $self->git_init($new);
164 $self->import_init(PublicInbox::Git->new($latest), 0);
168 my ($self, $git, $packed_bytes) = @_;
169 my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
170 $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
171 $im->{ssoma_lock} = 0;
172 $im->{path_type} = 'v2';