]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2: parallelize Xapian indexing
[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::SearchIdxPart;
10 use PublicInbox::SearchIdxThread;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14 use Email::MIME::ContentType;
15 $Email::MIME::ContentType::STRICT_PARAMS = 0;
16
17 # an estimate of the post-packed size to the raw uncompressed size
18 my $PACKING_FACTOR = 0.4;
19
20 sub new {
21         my ($class, $v2ibx, $creat) = @_;
22         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
23         unless (-d $dir) {
24                 if ($creat) {
25                         require File::Path;
26                         File::Path::mkpath($dir);
27                 } else {
28                         die "$dir does not exist\n";
29                 }
30         }
31         my $self = {
32                 -inbox => $v2ibx,
33                 im => undef, #  PublicInbox::Import
34                 xap_rw => undef, # PublicInbox::V2SearchIdx
35                 xap_ro => undef,
36                 partitions => 4,
37                 transact_bytes => 0,
38                 # limit each repo to 1GB or so
39                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
40         };
41         bless $self, $class
42 }
43
44 # returns undef on duplicate or spam
45 # mimics Import::add and wraps it for v2
46 sub add {
47         my ($self, $mime, $check_cb) = @_;
48         my $existing = $self->lookup_content($mime);
49
50         if ($existing) {
51                 return undef if $existing->type eq 'mail'; # duplicate
52         }
53
54         my $im = $self->importer;
55
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}};
61
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)) {
70                 $self->checkpoint;
71         }
72
73         $mime;
74 }
75
76 sub idx_part {
77         my ($self, $part) = @_;
78         my $idx = $self->{idx_parts};
79         return $idx->[$part] if $idx; # fast path
80
81         # first time initialization:
82         my $all = $self->{all} = 
83                 PublicInbox::SearchIdxThread->new($self->{-inbox});
84
85         # need to create all parts before initializing msgmap FD
86         my $max = $self->{partitions} - 1;
87         $idx = $self->{idx_parts} = [];
88         for my $i (0..$max) {
89                 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $all);
90         }
91         $all->_msgmap_init->{dbh}->begin_work;
92         $idx->[$part];
93 }
94
95 sub remove {
96         my ($self, $mime, $msg) = @_;
97         my $existing = $self->lookup_content($mime) or return;
98
99         # don't touch ghosts or already junked messages
100         return unless $existing->type eq 'mail';
101
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);
108 }
109
110 sub done {
111         my ($self) = @_;
112         my $im = $self->{im};
113         $im->done if $im; # PublicInbox::Import::done
114         $self->searchidx_checkpoint(0);
115 }
116
117 sub checkpoint {
118         my ($self) = @_;
119         my $im = $self->{im};
120         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
121         $self->searchidx_checkpoint(1);
122 }
123
124 sub searchidx_checkpoint {
125         my ($self, $more) = @_;
126
127         # order matters, we can only close {all} after all partitions
128         # are done because the partitions also write to {all}
129
130         my $parts = $self->{idx_parts};
131         foreach my $idx (@$parts) {
132                 $idx->remote_commit;
133                 $idx->remote_close unless $more;
134         }
135
136         if (my $all = $self->{all}) {
137                 $all->{mm}->{dbh}->commit;
138                 if ($more) {
139                         $all->{mm}->{dbh}->begin_work;
140                 }
141                 $all->remote_commit;
142                 $all->remote_close unless $more;
143         }
144         $self->{transact_bytes} = 0;
145 }
146
147 sub git_init {
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);
157
158         my $all = "$self->{-inbox}->{mainrepo}/all.git";
159         unless (-d $all) {
160                 @cmd = (qw(git init --bare -q), $all);
161                 PublicInbox::Import::run_die(\@cmd);
162         }
163
164         my $alt = "$all/objects/info/alternates";
165         my $new_obj_dir = "../../git/$new.git/objects";
166         my %alts;
167         if (-e $alt) {
168                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
169                 %alts = map { chomp; $_ => 1 } (<$fh>);
170         }
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";
175         $git_dir
176 }
177
178 sub importer {
179         my ($self) = @_;
180         my $im = $self->{im};
181         if ($im) {
182                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
183                         return $im;
184                 } else {
185                         $self->{im} = undef;
186                         $im->done;
187                         $self->searchidx_checkpoint(1);
188                         $im = undef;
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);
192                 }
193         }
194         my $latest;
195         my $max = -1;
196         my $new = 0;
197         my $pfx = "$self->{-inbox}->{mainrepo}/git";
198         if (-d $pfx) {
199                 foreach my $git_dir (glob("$pfx/*.git")) {
200                         $git_dir =~ m!/(\d+)\.git\z! or next;
201                         my $n = $1;
202                         if ($n > $max) {
203                                 $max = $n;
204                                 $latest = $git_dir;
205                         }
206                 }
207         }
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}) {
212                         $new = $max + 1;
213                 } else {
214                         $self->{max_git} = $max;
215                         return $self->import_init($git, $packed_bytes);
216                 }
217         }
218         $self->{max_git} = $new;
219         $latest = $self->git_init($new);
220         $self->import_init(PublicInbox::Git->new($latest), 0);
221 }
222
223 sub import_init {
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';
230         $self->{im} = $im;
231 }
232
233 sub lookup_content {
234         undef # TODO
235 }
236
237 1;