]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearchIdx.pm
searchidx: remove xref3 support for Xapian
[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 prepare_stack);
23 use PublicInbox::OverIdx;
24 use PublicInbox::MID qw(mids);
25 use PublicInbox::V2Writable;
26 use PublicInbox::InboxWritable;
27 use PublicInbox::ContentHash qw(content_hash);
28 use PublicInbox::Eml;
29 use File::Spec;
30
31 sub new {
32         my (undef, $dir, $opt) = @_;
33         my $l = $opt->{indexlevel} // 'full';
34         $l !~ $PublicInbox::SearchIdx::INDEXLEVELS and
35                 die "invalid indexlevel=$l\n";
36         $l eq 'basic' and die "E: indexlevel=basic not yet supported\n";
37         my $self = bless {
38                 xpfx => "$dir/ei".PublicInbox::Search::SCHEMA_VERSION,
39                 topdir => $dir,
40                 creat => $opt->{creat},
41                 ibx_map => {}, # (newsgroup//inboxdir) => $ibx
42                 ibx_list => [],
43                 indexlevel => $l,
44                 transact_bytes => 0,
45                 total_bytes => 0,
46                 current_info => '',
47                 parallel => 1,
48                 lock_path => "$dir/ei.lock",
49         }, __PACKAGE__;
50         $self->{shards} = $self->count_shards || nproc_shards($opt->{creat});
51         my $oidx = PublicInbox::OverIdx->new("$self->{xpfx}/over.sqlite3");
52         $oidx->{-no_fsync} = 1 if $opt->{-no_fsync};
53         $self->{oidx} = $oidx;
54         $self
55 }
56
57 sub attach_inbox {
58         my ($self, $ibx) = @_;
59         $ibx = PublicInbox::InboxWritable->new($ibx);
60         my $key = $ibx->eidx_key;
61         if (!$ibx->over || !$ibx->mm) {
62                 warn "W: skipping $key (unindexed)\n";
63                 return;
64         }
65         if (!defined($ibx->uidvalidity)) {
66                 warn "W: skipping $key (no UIDVALIDITY)\n";
67                 return;
68         }
69         my $ibxdir = File::Spec->canonpath($ibx->{inboxdir});
70         if ($ibxdir ne $ibx->{inboxdir}) {
71                 warn "W: `$ibx->{inboxdir}' canonicalized to `$ibxdir'\n";
72                 $ibx->{inboxdir} = $ibxdir;
73         }
74         $ibx = PublicInbox::InboxWritable->new($ibx);
75         $self->{ibx_map}->{$key} //= do {
76                 push @{$self->{ibx_list}}, $ibx;
77                 $ibx;
78         }
79 }
80
81 sub _ibx_attach { # each_inbox callback
82         my ($ibx, $self) = @_;
83         attach_inbox($self, $ibx);
84 }
85
86 sub attach_config {
87         my ($self, $cfg) = @_;
88         $cfg->each_inbox(\&_ibx_attach, $self);
89 }
90
91 sub git_blob_digest ($) {
92         my ($bref) = @_;
93         my $dig = Digest::SHA->new(1); # XXX SHA256 later
94         $dig->add('blob '.length($$bref)."\0");
95         $dig->add($$bref);
96         $dig;
97 }
98
99 sub is_bad_blob ($$$$) {
100         my ($oid, $type, $size, $expect_oid) = @_;
101         if ($type ne 'blob') {
102                 carp "W: $expect_oid is not a blob (type=$type)";
103                 return 1;
104         }
105         croak "BUG: $oid != $expect_oid" if $oid ne $expect_oid;
106         $size == 0 ? 1 : 0; # size == 0 means purged
107 }
108
109 sub do_xpost ($$) {
110         my ($req, $smsg) = @_;
111         my $self = $req->{self};
112         my $docid = $smsg->{num};
113         my $idx = $self->idx_shard($docid);
114         my $oid = $req->{oid};
115         my $xibx = $req->{ibx};
116         my $eml = $req->{eml};
117         if (my $new_smsg = $req->{new_smsg}) { # 'm' on cross-posted message
118                 my $xnum = $req->{xnum};
119                 $self->{oidx}->add_xref3($docid, $xnum, $oid, $xibx->eidx_key);
120                 $idx->shard_add_eidx_info($docid, $oid, $xibx, $eml);
121         } else { # 'd'
122                 $idx->shard_remove_eidx_info($docid, $oid, $xibx, $eml);
123         }
124 }
125
126 # called by V2Writable::sync_prepare
127 sub artnum_max { $_[0]->{oidx}->eidx_max }
128
129 sub index_unseen ($) {
130         my ($req) = @_;
131         my $new_smsg = $req->{new_smsg} or die 'BUG: {new_smsg} unset';
132         my $eml = delete $req->{eml};
133         $new_smsg->populate($eml, $req);
134         my $self = $req->{self};
135         my $docid = $self->{oidx}->adj_counter('eidx_docid', '+');
136         $new_smsg->{num} = $docid;
137         my $idx = $self->idx_shard($docid);
138         $self->{oidx}->add_overview($eml, $new_smsg);
139         my $oid = $new_smsg->{blob};
140         my $ibx = delete $req->{ibx} or die 'BUG: {ibx} unset';
141         $self->{oidx}->add_xref3($docid, $req->{xnum}, $oid, $ibx->eidx_key);
142         $idx->index_raw(undef, $eml, $new_smsg, $ibx);
143 }
144
145 sub do_finalize ($) {
146         my ($req) = @_;
147         if (my $indexed = $req->{indexed}) {
148                 do_xpost($req, $_) for @$indexed;
149         } elsif (exists $req->{new_smsg}) { # totally unseen messsage
150                 index_unseen($req);
151         } else {
152                 warn "W: ignoring delete $req->{oid} (not found)\n";
153         }
154 }
155
156 sub do_step ($) { # main iterator for adding messages to the index
157         my ($req) = @_;
158         my $self = $req->{self};
159         while (1) {
160                 if (my $next_arg = $req->{next_arg}) {
161                         if (my $smsg = $self->{oidx}->next_by_mid(@$next_arg)) {
162                                 $req->{cur_smsg} = $smsg;
163                                 $self->git->cat_async($smsg->{blob},
164                                                         \&ck_existing, $req);
165                                 return; # ck_existing calls do_step
166                         }
167                         delete $req->{cur_smsg};
168                         delete $req->{next_arg};
169                 }
170                 my $mid = shift(@{$req->{mids}});
171                 last unless defined $mid;
172                 my ($id, $prev);
173                 $req->{next_arg} = [ $mid, \$id, \$prev ];
174                 # loop again
175         }
176         do_finalize($req);
177 }
178
179 sub ck_existing { # git->cat_async callback
180         my ($bref, $oid, $type, $size, $req) = @_;
181         my $smsg = $req->{cur_smsg} or die 'BUG: {cur_smsg} missing';
182         return if is_bad_blob($oid, $type, $size, $smsg->{blob});
183         my $cur = PublicInbox::Eml->new($bref);
184         if (content_hash($cur) eq $req->{chash}) {
185                 push @{$req->{indexed}}, $smsg; # for do_xpost
186         } # else { index_unseen later }
187         do_step($req);
188 }
189
190 # is the messages visible in the inbox currently being indexed?
191 # return the number if so
192 sub cur_ibx_xnum ($$) {
193         my ($req, $bref) = @_;
194         my $ibx = $req->{ibx} or die 'BUG: current {ibx} missing';
195
196         # XXX overkill?
197         git_blob_digest($bref)->hexdigest eq $req->{oid} or die
198                 "BUG: blob mismatch $req->{oid}";
199
200         $req->{eml} = PublicInbox::Eml->new($bref);
201         $req->{chash} = content_hash($req->{eml});
202         $req->{mids} = mids($req->{eml});
203         my @q = @{$req->{mids}}; # copy
204         while (defined(my $mid = shift @q)) {
205                 my ($id, $prev);
206                 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
207                         return $x->{num} if $x->{blob} eq $req->{oid};
208                 }
209         }
210         undef;
211 }
212
213 sub index_oid { # git->cat_async callback for 'm'
214         my ($bref, $oid, $type, $size, $req) = @_;
215         return if is_bad_blob($oid, $type, $size, $req->{oid});
216         my $new_smsg = $req->{new_smsg} = bless {
217                 blob => $oid,
218         }, 'PublicInbox::Smsg';
219         $new_smsg->{bytes} = $size + crlf_adjust($$bref);
220         defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
221         do_step($req);
222 }
223
224 sub unindex_oid { # git->cat_async callback for 'd'
225         my ($bref, $oid, $type, $size, $req) = @_;
226         return if is_bad_blob($oid, $type, $size, $req->{oid});
227         return if defined(cur_ibx_xnum($req, $bref)); # was re-added
228         do_step($req);
229 }
230
231 # overrides V2Writable::last_commits, called by sync_ranges via sync_prepare
232 sub last_commits {
233         my ($self, $sync) = @_;
234         my $heads = [];
235         my $ekey = $sync->{ibx}->eidx_key;
236         my $uv = $sync->{ibx}->uidvalidity;
237         for my $i (0..$sync->{epoch_max}) {
238                 $heads->[$i] = $self->{oidx}->eidx_meta("lc-v2:$ekey//$uv;$i");
239         }
240         $heads;
241 }
242
243 sub _sync_inbox ($$$) {
244         my ($self, $opt, $ibx) = @_;
245         my $sync = {
246                 need_checkpoint => \(my $bool = 0),
247                 reindex => $opt->{reindex},
248                 -opt => $opt,
249                 self => $self,
250                 ibx => $ibx,
251         };
252         my $v = $ibx->version;
253         my $ekey = $ibx->eidx_key;
254         if ($v == 2) {
255                 my $epoch_max;
256                 defined($ibx->git_dir_latest(\$epoch_max)) or return;
257                 $sync->{epoch_max} = $epoch_max;
258                 sync_prepare($self, $sync) or return; # fills $sync->{todo}
259         } elsif ($v == 1) {
260                 my $uv = $ibx->uidvalidity;
261                 my $lc = $self->{oidx}->eidx_meta("lc-v1:$ekey//$uv");
262                 my $stk = prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
263                 my $unit = { stack => $stk, git => $ibx->git };
264                 push @{$sync->{todo}}, $unit;
265         } else {
266                 warn "E: $ekey unsupported inbox version (v$v)\n";
267                 return;
268         }
269         index_todo($self, $sync, $_) for @{$sync->{todo}};
270 }
271
272 sub eidx_sync { # main entry point
273         my ($self, $opt) = @_;
274         $self->idx_init($opt); # acquire lock via V2Writable::_idx_init
275         $self->{oidx}->rethread_prepare($opt);
276
277         _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
278
279         $self->{oidx}->rethread_done($opt);
280
281         PublicInbox::V2Writable::done($self);
282 }
283
284 sub update_last_commit {
285         my ($self, $sync, $unit, $latest_cmt) = @_;
286
287         my $ALL = $self->git;
288         # while (scalar(@{$ALL->{inflight_c}}) || scalar(@{$ALL->{inflight}})) {
289                 # $ALL->check_async_wait;
290                 # $ALL->cat_async_wait;
291         # }
292         # TODO
293 }
294
295 sub idx_init { # similar to V2Writable
296         my ($self, $opt) = @_;
297         return if $self->{idx_shards};
298
299         $self->git->cleanup;
300
301         my $ALL = $self->git->{git_dir}; # ALL.git
302         PublicInbox::Import::init_bare($ALL) unless -d $ALL;
303         my $info_dir = "$ALL/objects/info";
304         my $alt = "$info_dir/alternates";
305         my $mode = 0644;
306         my (%old, @old, %new, @new);
307         if (-e $alt) {
308                 open(my $fh, '<', $alt) or die "open $alt: $!";
309                 $mode = (stat($fh))[2] & 07777;
310                 while (<$fh>) {
311                         push @old, $_ if !$old{$_}++;
312                 }
313         }
314         for my $ibx (@{$self->{ibx_list}}) {
315                 my $line = $ibx->git->{git_dir} . "/objects\n";
316                 next if $old{$line};
317                 $new{$line} = 1;
318                 push @new, $line;
319         }
320         push @old, @new;
321         PublicInbox::V2Writable::write_alternates($info_dir, $mode, \@old);
322         $self->parallel_init($self->{indexlevel});
323         $self->umask_prepare;
324         $self->with_umask(\&PublicInbox::V2Writable::_idx_init, $self, $opt);
325         $self->{oidx}->begin_lazy;
326         $self->{oidx}->eidx_prep;
327 }
328
329 no warnings 'once';
330 *done = \&PublicInbox::V2Writable::done;
331 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
332 *with_umask = \&PublicInbox::InboxWritable::with_umask;
333 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
334 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
335 *sync_prepare = \&PublicInbox::V2Writable::sync_prepare;
336 *index_todo = \&PublicInbox::V2Writable::index_todo;
337 *count_shards = \&PublicInbox::V2Writable::count_shards;
338 *atfork_child = \&PublicInbox::V2Writable::atfork_child;
339 *idx_shard = \&PublicInbox::V2Writable::idx_shard;
340
341 1;