]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2/ui: get nntpd and init tests running on v2
[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::SearchIdxSkeleton;
11 use PublicInbox::MIME;
12 use PublicInbox::Git;
13 use PublicInbox::Import;
14
15 # an estimate of the post-packed size to the raw uncompressed size
16 my $PACKING_FACTOR = 0.4;
17
18 # assume 2 cores if GNU nproc(1) is not available
19 my $NPROC = int($ENV{NPROC} || `nproc 2>/dev/null` || 2);
20
21 sub new {
22         my ($class, $v2ibx, $creat) = @_;
23         my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
24         unless (-d $dir) {
25                 if ($creat) {
26                         require File::Path;
27                         File::Path::mkpath($dir);
28                 } else {
29                         die "$dir does not exist\n";
30                 }
31         }
32         my $self = {
33                 -inbox => $v2ibx,
34                 im => undef, #  PublicInbox::Import
35                 xap_rw => undef, # PublicInbox::V2SearchIdx
36                 xap_ro => undef,
37                 partitions => $NPROC,
38                 transact_bytes => 0,
39                 # limit each repo to 1GB or so
40                 rotate_bytes => int((1024 * 1024 * 1024) / $PACKING_FACTOR),
41         };
42         bless $self, $class
43 }
44
45 # returns undef on duplicate or spam
46 # mimics Import::add and wraps it for v2
47 sub add {
48         my ($self, $mime, $check_cb) = @_;
49         my $existing = $self->lookup_content($mime);
50
51         if ($existing) {
52                 return undef if $existing->type eq 'mail'; # duplicate
53         }
54
55         my $im = $self->importer;
56
57         # im->add returns undef if check_cb fails
58         my $cmt = $im->add($mime, $check_cb) or return;
59         $cmt = $im->get_mark($cmt);
60         my $oid = $im->{last_object_id};
61         my ($len, $msgref) = @{$im->{last_object}};
62
63         $self->idx_init;
64         my $num = $self->{skel}->index_mm($mime, 1);
65         my $nparts = $self->{partitions};
66         my $part = $num % $nparts;
67         my $idx = $self->idx_part($part);
68         $idx->index_raw($len, $msgref, $num, $oid);
69         my $n = $self->{transact_bytes} += $len;
70         if ($n > (PublicInbox::SearchIdx::BATCH_BYTES * $nparts)) {
71                 $self->checkpoint;
72         }
73
74         $mime;
75 }
76
77 sub idx_part {
78         my ($self, $part) = @_;
79         $self->{idx_parts}->[$part];
80 }
81
82 # idempotent
83 sub idx_init {
84         my ($self) = @_;
85         return if $self->{idx_parts};
86
87         # first time initialization, first we create the skeleton pipe:
88         my $skel = $self->{skel} = PublicInbox::SearchIdxSkeleton->new($self);
89
90         # need to create all parts before initializing msgmap FD
91         my $max = $self->{partitions} - 1;
92         my $idx = $self->{idx_parts} = [];
93         for my $i (0..$max) {
94                 push @$idx, PublicInbox::SearchIdxPart->new($self, $i, $skel);
95         }
96
97         # Now that all subprocesses are up, we can open the FD for SQLite:
98         $skel->_msgmap_init->{dbh}->begin_work;
99 }
100
101 sub remove {
102         my ($self, $mime, $msg) = @_;
103         my $existing = $self->lookup_content($mime) or return;
104
105         # don't touch ghosts or already junked messages
106         return unless $existing->type eq 'mail';
107
108         # always write removals to the current (latest) git repo since
109         # we process chronologically
110         my $im = $self->importer;
111         my ($cmt, undef) = $im->remove($mime, $msg);
112         $cmt = $im->get_mark($cmt);
113         $self->unindex_msg($existing, $cmt);
114 }
115
116 sub done {
117         my ($self) = @_;
118         my $im = $self->{im};
119         $im->done if $im; # PublicInbox::Import::done
120         $self->searchidx_checkpoint(0);
121 }
122
123 sub checkpoint {
124         my ($self) = @_;
125         my $im = $self->{im};
126         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
127         $self->searchidx_checkpoint(1);
128 }
129
130 sub searchidx_checkpoint {
131         my ($self, $more) = @_;
132
133         # order matters, we can only close {skel} after all partitions
134         # are done because the partitions also write to {skel}
135         if (my $parts = $self->{idx_parts}) {
136                 foreach my $idx (@$parts) {
137                         $idx->remote_commit; # propagates commit to skel
138                         $idx->remote_close unless $more;
139                 }
140                 delete $self->{idx_parts} unless $more;
141         }
142
143         if (my $skel = $self->{skel}) {
144                 my $dbh = $skel->{mm}->{dbh};
145                 $dbh->commit;
146                 if ($more) {
147                         $dbh->begin_work;
148                 } else {
149                         $skel->remote_commit; # XXX should be unnecessary...
150                         $skel->remote_close;
151                         delete $self->{skel};
152                 }
153         }
154         $self->{transact_bytes} = 0;
155 }
156
157 sub git_init {
158         my ($self, $new) = @_;
159         my $pfx = "$self->{-inbox}->{mainrepo}/git";
160         my $git_dir = "$pfx/$new.git";
161         die "$git_dir exists\n" if -e $git_dir;
162         my @cmd = (qw(git init --bare -q), $git_dir);
163         PublicInbox::Import::run_die(\@cmd);
164         @cmd = (qw/git config/, "--file=$git_dir/config",
165                         'repack.writeBitmaps', 'true');
166         PublicInbox::Import::run_die(\@cmd);
167
168         my $all = "$self->{-inbox}->{mainrepo}/all.git";
169         unless (-d $all) {
170                 @cmd = (qw(git init --bare -q), $all);
171                 PublicInbox::Import::run_die(\@cmd);
172         }
173
174         my $alt = "$all/objects/info/alternates";
175         my $new_obj_dir = "../../git/$new.git/objects";
176         my %alts;
177         if (-e $alt) {
178                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
179                 %alts = map { chomp; $_ => 1 } (<$fh>);
180         }
181         return $git_dir if $alts{$new_obj_dir};
182         open my $fh, '>>', $alt or die "open >> $alt: $!\n";
183         print $fh "$new_obj_dir\n" or die "print >> $alt: $!\n";
184         close $fh or die "close $alt: $!\n";
185         $git_dir
186 }
187
188 sub importer {
189         my ($self) = @_;
190         my $im = $self->{im};
191         if ($im) {
192                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
193                         return $im;
194                 } else {
195                         $self->{im} = undef;
196                         $im->done;
197                         $self->searchidx_checkpoint(1);
198                         $im = undef;
199                         my $git_dir = $self->git_init(++$self->{max_git});
200                         my $git = PublicInbox::Git->new($git_dir);
201                         return $self->import_init($git, 0);
202                 }
203         }
204         my $latest;
205         my $max = -1;
206         my $new = 0;
207         my $pfx = "$self->{-inbox}->{mainrepo}/git";
208         if (-d $pfx) {
209                 foreach my $git_dir (glob("$pfx/*.git")) {
210                         $git_dir =~ m!/(\d+)\.git\z! or next;
211                         my $n = $1;
212                         if ($n > $max) {
213                                 $max = $n;
214                                 $latest = $git_dir;
215                         }
216                 }
217         }
218         if (defined $latest) {
219                 my $git = PublicInbox::Git->new($latest);
220                 my $packed_bytes = $git->packed_bytes;
221                 if ($packed_bytes >= $self->{rotate_bytes}) {
222                         $new = $max + 1;
223                 } else {
224                         $self->{max_git} = $max;
225                         return $self->import_init($git, $packed_bytes);
226                 }
227         }
228         $self->{max_git} = $new;
229         $latest = $self->git_init($new);
230         $self->import_init(PublicInbox::Git->new($latest), 0);
231 }
232
233 sub import_init {
234         my ($self, $git, $packed_bytes) = @_;
235         my $im = PublicInbox::Import->new($git, undef, undef, $self->{-inbox});
236         $im->{bytes_added} = int($packed_bytes / $PACKING_FACTOR);
237         $im->{want_object_id} = 1;
238         $im->{ssoma_lock} = 0;
239         $im->{path_type} = 'v2';
240         $self->{im} = $im;
241 }
242
243 sub lookup_content {
244         undef # TODO
245 }
246
247 sub atfork_child {
248         my ($self) = @_;
249         if (my $parts = $self->{idx_parts}) {
250                 $_->atfork_child foreach @$parts;
251         }
252         if (my $im = $self->{im}) {
253                 $im->atfork_child;
254         }
255 }
256
257 1;