]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearchIdx.pm
extsearchidx: initial implementation
[public-inbox.git] / lib / PublicInbox / ExtSearchIdx.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Detached/external index cross inbox search indexing support
5 # read-write counterpart to PublicInbox::ExtSearch
6 #
7 # It's based on the same ideas as public-inbox-v2-format(5) using
8 # over.sqlite3 for dedupe and sharded Xapian.  msgmap.sqlite3 is
9 # missing, so there is no Message-ID conflict resolution, meaning
10 # no NNTP support for now.
11 #
12 # v2 has a 1:1 mapping of index:inbox or msgmap for NNTP support.
13 # This is intended to be an M:N index:inbox mapping, but it'll likely
14 # be 1:N in common practice (M==1)
15
16 package PublicInbox::ExtSearchIdx;
17 use strict;
18 use v5.10.1;
19 use parent qw(PublicInbox::ExtSearch PublicInbox::Lock);
20 use Carp qw(croak carp);
21 use PublicInbox::Search;
22 use PublicInbox::SearchIdx qw(crlf_adjust);
23 use PublicInbox::OverIdx;
24 use PublicInbox::V2Writable;
25 use PublicInbox::InboxWritable;
26 use PublicInbox::Eml;
27 use File::Spec;
28
29 sub new {
30         my (undef, $dir, $opt, $shard) = @_;
31         my $l = $opt->{indexlevel} // 'full';
32         $l !~ $PublicInbox::SearchIdx::INDEXLEVELS and
33                 die "invalid indexlevel=$l\n";
34         $l eq 'basic' and die "E: indexlevel=basic not yet supported\n";
35         my $self = bless {
36                 xpfx => "$dir/ei".PublicInbox::Search::SCHEMA_VERSION,
37                 topdir => $dir,
38                 creat => $opt->{creat},
39                 ibx_map => {}, # (newsgroup//inboxdir) => $ibx
40                 ibx_list => [],
41                 indexlevel => $l,
42                 transact_bytes => 0,
43                 total_bytes => 0,
44                 current_info => '',
45                 parallel => 1,
46                 lock_path => "$dir/ei.lock",
47         }, __PACKAGE__;
48         $self->{shards} = $self->count_shards || nproc_shards($opt->{creat});
49         my $oidx = PublicInbox::OverIdx->new("$self->{xpfx}/over.sqlite3");
50         $oidx->{-no_fsync} = 1 if $opt->{-no_fsync};
51         $self->{oidx} = $oidx;
52         $self
53 }
54
55 sub attach_inbox {
56         my ($self, $ibx) = @_;
57         my $key = $ibx->eidx_key;
58         if (!$ibx->over || !$ibx->mm) {
59                 warn "W: skipping $key (unindexed)\n";
60                 return;
61         }
62         if (!defined($ibx->uidvalidity)) {
63                 warn "W: skipping $key (no UIDVALIDITY)\n";
64                 return;
65         }
66         my $ibxdir = File::Spec->canonpath($ibx->{inboxdir});
67         if ($ibxdir ne $ibx->{inboxdir}) {
68                 warn "W: `$ibx->{inboxdir}' canonicalized to `$ibxdir'\n";
69                 $ibx->{inboxdir} = $ibxdir;
70         }
71         $ibx = PublicInbox::InboxWritable->new($ibx);
72         $self->{ibx_map}->{$key} //= do {
73                 push @{$self->{ibx_list}}, $ibx;
74                 $ibx;
75         }
76 }
77
78 sub _ibx_attach { # each_inbox callback
79         my ($ibx, $self) = @_;
80         attach_inbox($self, $ibx);
81 }
82
83 sub attach_config {
84         my ($self, $cfg) = @_;
85         $cfg->each_inbox(\&_ibx_attach, $self);
86 }
87
88 sub git_blob_digest ($) {
89         my ($bref) = @_;
90         my $dig = Digest::SHA->new(1); # XXX SHA256 later
91         $dig->add('blob '.length($$bref)."\0");
92         $dig->add($$bref);
93         $dig;
94 }
95
96 sub is_bad_blob ($$$$) {
97         my ($oid, $type, $size, $expect_oid) = @_;
98         if ($type ne 'blob') {
99                 carp "W: $expect_oid is not a blob (type=$type)";
100                 return 1;
101         }
102         croak "BUG: $oid != $expect_oid" if $oid ne $expect_oid;
103         $size == 0 ? 1 : 0; # size == 0 means purged
104 }
105
106 sub do_xpost ($$) {
107         my ($req, $smsg) = @_;
108         my $self = $req->{eidx};
109         my $docid = $smsg->{num};
110         my $idx = $self->idx_shard($docid);
111         my $oid = $req->{oid};
112         my $xibx = $self->{sync}->{ibx};
113         my $eml = $req->{eml};
114         if (my $new_smsg = $req->{new_smsg}) { # 'm' on cross-posted message
115                 my $xnum = $req->{xnum};
116                 $idx->shard_add_xref3($docid, $xnum, $oid, $xibx, $eml);
117         } else { # 'd'
118                 $idx->shard_remove_xref3($docid, $oid, $xibx, $eml);
119         }
120 }
121
122 sub index_unseen ($) {
123         my ($req) = @_;
124         my $new_smsg = $req->{new_smsg} or die 'BUG: {new_smsg} unset';
125         $new_smsg->populate($req->{eml}, $req);
126         my $self = $req->{eidx};
127         my $docid = $self->{oidx}->adj_counter('eidx_docid', '+');
128         $new_smsg->{num} = $docid;
129         my $idx = $self->idx_shard($docid);
130         my $eml = delete $req->{eml};
131         $self->{oidx}->add_overview($eml, $new_smsg);
132         $idx->index_raw(undef, $eml, $new_smsg, delete $new_smsg->{ibx});
133 }
134
135 sub do_finalize ($) {
136         my ($req) = @_;
137         if (my $indexed = $req->{indexed}) {
138                 do_xpost($req, $_) for @$indexed;
139         } elsif (exists $req->{new_smsg}) { # totally unseen messsage
140                 index_unseen($req);
141         } else {
142                 warn "W: ignoring delete $req->{oid} (not found)\n";
143         }
144 }
145
146 sub do_step ($) { # main iterator for adding messages to the index
147         my ($req) = @_;
148         my $self = $req->{eidx};
149         while (1) {
150                 if (my $next_arg = $req->{next_arg}) {
151                         if (my $smsg = $self->{oidx}->next_by_mid(@$next_arg)) {
152                                 $req->{cur_smsg} = $smsg;
153                                 $self->git->cat_async($smsg->{blob},
154                                                         \&ck_existing, $req);
155                                 return; # ck_existing calls do_step
156                         }
157                         delete $req->{cur_smsg};
158                         delete $req->{next_arg};
159                 }
160                 my $mid = shift(@{$req->{mids}});
161                 last unless defined $mid;
162                 my ($id, $prev);
163                 $req->{next_arg} = [ $mid, \$id, \$prev ];
164                 # loop again
165         }
166         do_finalize($req);
167 }
168
169 sub ck_existing { # git->cat_async callback
170         my ($bref, $oid, $type, $size, $req) = @_;
171         my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
172         return if is_bad_blob($oid, $type, $size, $smsg->{blob});
173         my $cur = PublicInbox::Eml->new($bref);
174         if (content_digest($cur) eq $req->{chash}) {
175                 push @{$req->{indexed}}, $smsg; # for do_xpost
176         } # else { index_unseen later }
177         do_step($req);
178 }
179
180 # is the messages visible in the inbox currently being indexed?
181 # return the number if so
182 sub cur_ibx_xnum ($$) {
183         my ($req, $bref) = @_;
184         my $ibx = $req->{sync}->{ibx} or die 'BUG: current {ibx} missing';
185
186         # XXX overkill?
187         git_blob_digest($bref)->hexdigest eq $req->{oid} or die
188                 "BUG: blob mismatch $req->{oid}";
189
190         $req->{eml} = PublicInbox::Eml->new($bref);
191         $req->{chash} = content_hash($req->{eml});
192         $req->{mids} = mids($req->{eml});
193         my @q = @{$req->{mids}}; # copy
194         while (defined(my $mid = shift @q)) {
195                 my ($id, $prev);
196                 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
197                         return $x->{num} if $x->{blob} eq $req->{oid};
198                 }
199         }
200         undef;
201 }
202
203 sub m_start { # git->cat_async callback for 'm'
204         my ($bref, $oid, $type, $size, $req) = @_;
205         return if is_bad_blob($oid, $type, $size, $req->{oid});
206         my $new_smsg = $req->{new_smsg} = bless {
207                 blob => $oid,
208                 raw_bytes => $size,
209         }, 'PublicInbox::Smsg';
210         $new_smsg->{bytes} = $new_smsg->{raw_bytes} + crlf_adjust($$bref);
211         defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
212         $new_smsg->{ibx} = $req->{sync}->{ibx};
213         do_step($req);
214 }
215
216 sub d_start { # git->cat_async callback for 'd'
217         my ($bref, $oid, $type, $size, $req) = @_;
218         return if is_bad_blob($oid, $type, $size, $req->{oid});
219         return if defined(cur_ibx_xnum($req, $bref)); # was re-added
220         do_step($req);
221 }
222
223 sub eidx_last_epoch_commit ($$$) {
224         my ($self, $sync, $epoch) = @_;
225         my $key = $sync->{ibx}->eidx_key;
226         my $uidvalidity = $sync->{ibx}->uidvalidity;
227         $self->{oidx}->eidx_meta("lc-v2:$key//$uidvalidity;$epoch");
228 }
229
230 sub _sync_inbox ($$$) {
231         my ($self, $opt, $ibx) = @_;
232         my $sync = {
233                 need_checkpoint => \(my $bool = 0),
234                 unindex_range => {}, # EPOCH => oid_old..oid_new
235                 reindex => $opt->{reindex},
236                 -opt => $opt,
237                 eidx => $self,
238                 ibx => $ibx,
239         };
240         my $key = $ibx->eidx_key;
241         my $u = $ibx->uidvalidity;
242         my $oidx = $self->{oidx};
243         my $v = $ibx->version;
244         if ($v == 2) {
245                 my $epoch_max;
246                 defined($ibx->git_dir_latest(\$epoch_max)) or return;
247                 my $heads = $sync->{ranges} = [];
248                 for my $i (0..$epoch_max) {
249                         $heads->[$i] = $oidx->eidx_meta("lc-v2:$key//$u;$i");
250                 }
251
252
253         } elsif ($v == 1) {
254                 my $lc = $oidx->eidx_meta("lc-v1:$key//$u");
255                 prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
256         } else {
257                 warn "E: $key unsupported inbox version (v$v)\n";
258                 return;
259         }
260 }
261
262 sub eidx_sync { # main entry point
263         my ($self, $opt) = @_;
264         $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
265         $self->{oidx}->rethread_prepare($opt);
266
267         _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
268 }
269
270 sub idx_init { # similar to V2Writable
271         my ($self, $opt) = @_;
272         return if $self->{idx_shards};
273
274         $self->git->cleanup;
275
276         my $ALL = $self->git->{git_dir}; # ALL.git
277         PublicInbox::Import::init_bare($ALL) unless -d $ALL;
278         my $info_dir = "$ALL/objects/info";
279         my $alt = "$info_dir/alternates";
280         my $mode = 0644;
281         my (%old, @old, %new, @new);
282         if (-e $alt) {
283                 open(my $fh, '<', $alt) or die "open $alt: $!";
284                 $mode = (stat($fh))[2] & 07777;
285                 while (<$fh>) {
286                         push @old, $_ if !$old{$_}++;
287                 }
288         }
289         for my $ibx (@{$self->{ibx_list}}) {
290                 my $line = $ibx->git->{git_dir} . "/objects\n";
291                 next if $old{$line};
292                 $new{$line} = 1;
293                 push @new, $line;
294         }
295         push @old, @new;
296         PublicInbox::V2Writable::write_alternates($info_dir, $mode, \@old);
297         $self->parallel_init($self->{indexlevel});
298         $self->umask_prepare;
299         $self->with_umask(\&PublicInbox::V2Writable::_idx_init, $self, $opt);
300         $self->{oidx}->begin_lazy;
301         $self->{oidx}->eidx_prep;
302 }
303
304 no warnings 'once';
305 *done = \&PublicInbox::V2Writable::done;
306 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
307 *with_umask = \&PublicInbox::InboxWritable::with_umask;
308 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
309 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
310
311 1;