]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2writable: initial cut for repo-rotation
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # This interface wraps and mimics PublicInbox::Import
5 package PublicInbox::V2Writable;
6 use strict;
7 use warnings;
8 use Fcntl qw(:flock :DEFAULT);
9 use PublicInbox::SearchIdx;
10 use PublicInbox::MIME;
11 use PublicInbox::Git;
12 use PublicInbox::Import;
13 use Email::MIME::ContentType;
14 $Email::MIME::ContentType::STRICT_PARAMS = 0;
15
16 # an estimate of the post-packed size to the raw uncompressed size
17 my $PACKING_FACTOR = 0.4;
18
19 sub new {
20         my ($class, $v2ibx, $creat) = @_;
21         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
22         unless (-d $dir) {
23                 if ($creat) {
24                         require File::Path;
25                         File::Path::mkpath($dir);
26                 } else {
27                         die "$dir does not exist\n";
28                 }
29         }
30         my $self = {
31                 -inbox => $v2ibx,
32                 im => undef, #  PublicInbox::Import
33                 xap_rw => undef, # PublicInbox::V2SearchIdx
34                 xap_ro => undef,
35
36                 # limit each repo to 1GB or so
37                 rotate_bytes => int((100 * 1024 * 1024) / $PACKING_FACTOR),
38         };
39         bless $self, $class
40 }
41
42 # returns undef on duplicate or spam
43 # mimics Import::add and wraps it for v2
44 sub add {
45         my ($self, $mime, $check_cb) = @_;
46         my $existing = $self->lookup_content($mime);
47
48         if ($existing) {
49                 return undef if $existing->type eq 'mail'; # duplicate
50         }
51
52         my $im = $self->importer;
53
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);
59         $mime;
60 }
61
62 sub index_msg {  # TODO
63 }
64
65 sub remove {
66         my ($self, $mime, $msg) = @_;
67         my $existing = $self->lookup_content($mime) or return;
68
69         # don't touch ghosts or already junked messages
70         return unless $existing->type eq 'mail';
71
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);
78 }
79
80 sub done {
81         my ($self) = @_;
82         $self->{im}->done; # PublicInbox::Import::done
83 }
84
85 sub checkpoint {
86         my ($self) = @_;
87         $self->{im}->checkpoint; # PublicInbox::Import::checkpoint
88 }
89
90 sub git_init {
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);
100
101         my $all = "$self->{-inbox}->{mainrepo}/all.git";
102         unless (-d $all) {
103                 @cmd = (qw(git init --bare -q), $all);
104                 PublicInbox::Import::run_die(\@cmd);
105         }
106
107         my $alt = "$all/objects/info/alternates";
108         my $new_obj_dir = "../../git/$new.git/objects";
109         my %alts;
110         if (-e $alt) {
111                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
112                 %alts = map { chomp; $_ => 1 } (<$fh>);
113         }
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";
118         $git_dir
119 }
120
121 sub importer {
122         my ($self) = @_;
123         my $im = $self->{im};
124         if ($im) {
125                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
126                         return $im;
127                 } else {
128                         $self->{im} = undef;
129                         $im->done;
130                         $im = undef;
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);
134                 }
135         }
136         my $latest;
137         my $max = -1;
138         my $new = 0;
139         my $pfx = "$self->{-inbox}->{mainrepo}/git";
140         if (-d $pfx) {
141                 foreach my $git_dir (glob("$pfx/*.git")) {
142                         $git_dir =~ m!/(\d+)\.git\z! or next;
143                         my $n = $1;
144                         if ($n > $max) {
145                                 $max = $n;
146                                 $latest = $git_dir;
147                         }
148                 }
149         }
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}) {
154                         $new = $max + 1;
155                 } else {
156                         $self->{max_git} = $max;
157                         return $self->import_init($git, $packed_bytes);
158                 }
159         } else {
160                 warn "latest not found in $pfx\n";
161         }
162         $self->{max_git} = $new;
163         $latest = $self->git_init($new);
164         $self->import_init(PublicInbox::Git->new($latest), 0);
165 }
166
167 sub import_init {
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';
173         $self->{im} = $im;
174 }
175
176 sub lookup_content {
177         undef # TODO
178 }
179
180 1;