]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearchIdx.pm
extsearchidx: more compatibility with V2Writable callers
[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->{self};
109         my $docid = $smsg->{num};
110         my $idx = $self->idx_shard($docid);
111         my $oid = $req->{oid};
112         my $xibx = $req->{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 # called by V2Writable::sync_prepare
123 sub artnum_max { $_[0]->{oidx}->get_counter('eidx_docid') }
124
125 sub index_unseen ($) {
126         my ($req) = @_;
127         my $new_smsg = $req->{new_smsg} or die 'BUG: {new_smsg} unset';
128         my $eml = delete $req->{eml};
129         $new_smsg->populate($eml, $req);
130         my $self = $req->{self};
131         my $docid = $self->{oidx}->adj_counter('eidx_docid', '+');
132         $new_smsg->{num} = $docid;
133         my $idx = $self->idx_shard($docid);
134         $self->{oidx}->add_overview($eml, $new_smsg);
135         $idx->index_raw(undef, $eml, $new_smsg, $req->{ibx});
136 }
137
138 sub do_finalize ($) {
139         my ($req) = @_;
140         if (my $indexed = $req->{indexed}) {
141                 do_xpost($req, $_) for @$indexed;
142         } elsif (exists $req->{new_smsg}) { # totally unseen messsage
143                 index_unseen($req);
144         } else {
145                 warn "W: ignoring delete $req->{oid} (not found)\n";
146         }
147 }
148
149 sub do_step ($) { # main iterator for adding messages to the index
150         my ($req) = @_;
151         my $self = $req->{self};
152         while (1) {
153                 if (my $next_arg = $req->{next_arg}) {
154                         if (my $smsg = $self->{oidx}->next_by_mid(@$next_arg)) {
155                                 $req->{cur_smsg} = $smsg;
156                                 $self->git->cat_async($smsg->{blob},
157                                                         \&ck_existing, $req);
158                                 return; # ck_existing calls do_step
159                         }
160                         delete $req->{cur_smsg};
161                         delete $req->{next_arg};
162                 }
163                 my $mid = shift(@{$req->{mids}});
164                 last unless defined $mid;
165                 my ($id, $prev);
166                 $req->{next_arg} = [ $mid, \$id, \$prev ];
167                 # loop again
168         }
169         do_finalize($req);
170 }
171
172 sub ck_existing { # git->cat_async callback
173         my ($bref, $oid, $type, $size, $req) = @_;
174         my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
175         return if is_bad_blob($oid, $type, $size, $smsg->{blob});
176         my $cur = PublicInbox::Eml->new($bref);
177         if (content_digest($cur) eq $req->{chash}) {
178                 push @{$req->{indexed}}, $smsg; # for do_xpost
179         } # else { index_unseen later }
180         do_step($req);
181 }
182
183 # is the messages visible in the inbox currently being indexed?
184 # return the number if so
185 sub cur_ibx_xnum ($$) {
186         my ($req, $bref) = @_;
187         my $ibx = $req->{ibx} or die 'BUG: current {ibx} missing';
188
189         # XXX overkill?
190         git_blob_digest($bref)->hexdigest eq $req->{oid} or die
191                 "BUG: blob mismatch $req->{oid}";
192
193         $req->{eml} = PublicInbox::Eml->new($bref);
194         $req->{chash} = content_hash($req->{eml});
195         $req->{mids} = mids($req->{eml});
196         my @q = @{$req->{mids}}; # copy
197         while (defined(my $mid = shift @q)) {
198                 my ($id, $prev);
199                 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
200                         return $x->{num} if $x->{blob} eq $req->{oid};
201                 }
202         }
203         undef;
204 }
205
206 sub index_oid { # git->cat_async callback for 'm'
207         my ($bref, $oid, $type, $size, $req) = @_;
208         return if is_bad_blob($oid, $type, $size, $req->{oid});
209         my $new_smsg = $req->{new_smsg} = bless {
210                 blob => $oid,
211         }, 'PublicInbox::Smsg';
212         $new_smsg->{bytes} = $size + crlf_adjust($$bref);
213         defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
214         do_step($req);
215 }
216
217 sub unindex_oid { # git->cat_async callback for 'd'
218         my ($bref, $oid, $type, $size, $req) = @_;
219         return if is_bad_blob($oid, $type, $size, $req->{oid});
220         return if defined(cur_ibx_xnum($req, $bref)); # was re-added
221         do_step($req);
222 }
223
224 # overrides V2Writable::last_commits, called by sync_ranges via sync_prepare
225 sub last_commits {
226         my ($self, $sync) = @_;
227         my $heads = [];
228         my $ekey = $sync->{ibx}->eidx_key;
229         my $uv = $sync->{ibx}->uidvalidity;
230         for my $i (0..$sync->{epoch_max}) {
231                 $heads->[$i] = $self->{oidx}->eidx_meta("lc-v2:$ekey//$uv;$i");
232         }
233         $heads;
234 }
235
236 sub _sync_inbox ($$$) {
237         my ($self, $opt, $ibx) = @_;
238         my $sync = {
239                 need_checkpoint => \(my $bool = 0),
240                 unindex_range => {}, # EPOCH => oid_old..oid_new
241                 reindex => $opt->{reindex},
242                 -opt => $opt,
243                 self => $self,
244                 ibx => $ibx,
245         };
246         my $v = $ibx->version;
247         my $ekey = $ibx->eidx_key;
248         if ($v == 2) {
249                 my $epoch_max;
250                 defined($ibx->git_dir_latest(\$epoch_max)) or return;
251                 $sync->{epoch_max} = $epoch_max;
252                 sync_prepare($self, $sync) or return;
253                 index_epoch($self, $sync, $_) for (0..$epoch_max);
254         } elsif ($v == 1) {
255                 my $uv = $ibx->uidvalidity;
256                 my $lc = $self->{oidx}->eidx_meta("lc-v1:$ekey//$uv");
257                 prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
258         } else {
259                 warn "E: $ekey unsupported inbox version (v$v)\n";
260                 return;
261         }
262 }
263
264 sub eidx_sync { # main entry point
265         my ($self, $opt) = @_;
266         $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
267         $self->{oidx}->rethread_prepare($opt);
268
269         _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
270 }
271
272 sub idx_init { # similar to V2Writable
273         my ($self, $opt) = @_;
274         return if $self->{idx_shards};
275
276         $self->git->cleanup;
277
278         my $ALL = $self->git->{git_dir}; # ALL.git
279         PublicInbox::Import::init_bare($ALL) unless -d $ALL;
280         my $info_dir = "$ALL/objects/info";
281         my $alt = "$info_dir/alternates";
282         my $mode = 0644;
283         my (%old, @old, %new, @new);
284         if (-e $alt) {
285                 open(my $fh, '<', $alt) or die "open $alt: $!";
286                 $mode = (stat($fh))[2] & 07777;
287                 while (<$fh>) {
288                         push @old, $_ if !$old{$_}++;
289                 }
290         }
291         for my $ibx (@{$self->{ibx_list}}) {
292                 my $line = $ibx->git->{git_dir} . "/objects\n";
293                 next if $old{$line};
294                 $new{$line} = 1;
295                 push @new, $line;
296         }
297         push @old, @new;
298         PublicInbox::V2Writable::write_alternates($info_dir, $mode, \@old);
299         $self->parallel_init($self->{indexlevel});
300         $self->umask_prepare;
301         $self->with_umask(\&PublicInbox::V2Writable::_idx_init, $self, $opt);
302         $self->{oidx}->begin_lazy;
303         $self->{oidx}->eidx_prep;
304 }
305
306 no warnings 'once';
307 *done = \&PublicInbox::V2Writable::done;
308 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
309 *with_umask = \&PublicInbox::InboxWritable::with_umask;
310 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
311 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
312 *sync_prepare = \&PublicInbox::V2Writable::sync_prepare;
313 *index_epoch = \&PublicInbox::V2Writable::index_epoch;
314
315 1;