]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/V2Writable.pm
v2: support Xapian + SQLite 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::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((1024 * 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         my $size = $im->{last_object_size};
59
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) {
65                 $self->checkpoint;
66         }
67
68         $mime;
69 }
70
71 sub search_idx {
72         my ($self) = @_;
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;
79                 $idx
80         };
81 }
82
83 sub remove {
84         my ($self, $mime, $msg) = @_;
85         my $existing = $self->lookup_content($mime) or return;
86
87         # don't touch ghosts or already junked messages
88         return unless $existing->type eq 'mail';
89
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);
96 }
97
98 sub done {
99         my ($self) = @_;
100         my $im = $self->{im};
101         $im->done if $im; # PublicInbox::Import::done
102         $self->searchidx_checkpoint;
103 }
104
105 sub checkpoint {
106         my ($self) = @_;
107         my $im = $self->{im};
108         $im->checkpoint if $im; # PublicInbox::Import::checkpoint
109         $self->searchidx_checkpoint;
110 }
111
112 sub searchidx_checkpoint {
113         my ($self) = @_;
114         my $idx = delete $self->{idx} or return;
115
116         $idx->{mm}->{dbh}->commit;
117         $idx->{xdb}->commit_transaction;
118         $idx->_xdb_release;
119 }
120
121 sub git_init {
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);
131
132         my $all = "$self->{-inbox}->{mainrepo}/all.git";
133         unless (-d $all) {
134                 @cmd = (qw(git init --bare -q), $all);
135                 PublicInbox::Import::run_die(\@cmd);
136         }
137
138         my $alt = "$all/objects/info/alternates";
139         my $new_obj_dir = "../../git/$new.git/objects";
140         my %alts;
141         if (-e $alt) {
142                 open(my $fh, '<', $alt) or die "open < $alt: $!\n";
143                 %alts = map { chomp; $_ => 1 } (<$fh>);
144         }
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";
149         $git_dir
150 }
151
152 sub importer {
153         my ($self) = @_;
154         my $im = $self->{im};
155         if ($im) {
156                 if ($im->{bytes_added} < $self->{rotate_bytes}) {
157                         return $im;
158                 } else {
159                         $self->{im} = undef;
160                         $im->done;
161                         $self->searchidx_checkpoint;
162                         $im = undef;
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);
166                 }
167         }
168         my $latest;
169         my $max = -1;
170         my $new = 0;
171         my $pfx = "$self->{-inbox}->{mainrepo}/git";
172         if (-d $pfx) {
173                 foreach my $git_dir (glob("$pfx/*.git")) {
174                         $git_dir =~ m!/(\d+)\.git\z! or next;
175                         my $n = $1;
176                         if ($n > $max) {
177                                 $max = $n;
178                                 $latest = $git_dir;
179                         }
180                 }
181         }
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}) {
186                         $new = $max + 1;
187                 } else {
188                         $self->{max_git} = $max;
189                         return $self->import_init($git, $packed_bytes);
190                 }
191         }
192         $self->{max_git} = $new;
193         $latest = $self->git_init($new);
194         $self->import_init(PublicInbox::Git->new($latest), 0);
195 }
196
197 sub import_init {
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';
204         $self->{im} = $im;
205 }
206
207 sub lookup_content {
208         undef # TODO
209 }
210
211 1;