1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Detached/external index cross inbox search indexing support
5 # read-write counterpart to PublicInbox::ExtSearch
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.
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)
16 package PublicInbox::ExtSearchIdx;
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;
30 my (undef, $dir, $opt) = @_;
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";
36 xpfx => "$dir/ei".PublicInbox::Search::SCHEMA_VERSION,
38 creat => $opt->{creat},
39 ibx_map => {}, # (newsgroup//inboxdir) => $ibx
46 lock_path => "$dir/ei.lock",
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;
56 my ($self, $ibx) = @_;
57 my $key = $ibx->eidx_key;
58 if (!$ibx->over || !$ibx->mm) {
59 warn "W: skipping $key (unindexed)\n";
62 if (!defined($ibx->uidvalidity)) {
63 warn "W: skipping $key (no UIDVALIDITY)\n";
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;
71 $ibx = PublicInbox::InboxWritable->new($ibx);
72 $self->{ibx_map}->{$key} //= do {
73 push @{$self->{ibx_list}}, $ibx;
78 sub _ibx_attach { # each_inbox callback
79 my ($ibx, $self) = @_;
80 attach_inbox($self, $ibx);
84 my ($self, $cfg) = @_;
85 $cfg->each_inbox(\&_ibx_attach, $self);
88 sub git_blob_digest ($) {
90 my $dig = Digest::SHA->new(1); # XXX SHA256 later
91 $dig->add('blob '.length($$bref)."\0");
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)";
102 croak "BUG: $oid != $expect_oid" if $oid ne $expect_oid;
103 $size == 0 ? 1 : 0; # size == 0 means purged
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);
118 $idx->shard_remove_xref3($docid, $oid, $xibx, $eml);
122 # called by V2Writable::sync_prepare
123 sub artnum_max { $_[0]->{oidx}->get_counter('eidx_docid') }
125 sub index_unseen ($) {
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});
138 sub do_finalize ($) {
140 if (my $indexed = $req->{indexed}) {
141 do_xpost($req, $_) for @$indexed;
142 } elsif (exists $req->{new_smsg}) { # totally unseen messsage
145 warn "W: ignoring delete $req->{oid} (not found)\n";
149 sub do_step ($) { # main iterator for adding messages to the index
151 my $self = $req->{self};
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
160 delete $req->{cur_smsg};
161 delete $req->{next_arg};
163 my $mid = shift(@{$req->{mids}});
164 last unless defined $mid;
166 $req->{next_arg} = [ $mid, \$id, \$prev ];
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 }
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';
190 git_blob_digest($bref)->hexdigest eq $req->{oid} or die
191 "BUG: blob mismatch $req->{oid}";
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)) {
199 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
200 return $x->{num} if $x->{blob} eq $req->{oid};
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 {
211 }, 'PublicInbox::Smsg';
212 $new_smsg->{bytes} = $size + crlf_adjust($$bref);
213 defined($req->{xnum} = cur_ibx_xnum($req, $bref)) or return;
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
224 # overrides V2Writable::last_commits, called by sync_ranges via sync_prepare
226 my ($self, $sync) = @_;
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");
236 sub _sync_inbox ($$$) {
237 my ($self, $opt, $ibx) = @_;
239 need_checkpoint => \(my $bool = 0),
240 reindex => $opt->{reindex},
245 my $v = $ibx->version;
246 my $ekey = $ibx->eidx_key;
249 defined($ibx->git_dir_latest(\$epoch_max)) or return;
250 $sync->{epoch_max} = $epoch_max;
251 sync_prepare($self, $sync) or return;
253 my $uv = $ibx->uidvalidity;
254 my $lc = $self->{oidx}->eidx_meta("lc-v1:$ekey//$uv");
255 my $stk = prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
256 my $unit = { stack => $stk, git => $ibx->git };
258 warn "E: $ekey unsupported inbox version (v$v)\n";
261 index_todo($self, $sync, $_) for @{$sync->{todo}};
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);
269 _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
270 $self->{oidx}->rethread_done($opt);
271 PublicInbox::V2Writable::done($self);
274 sub idx_init { # similar to V2Writable
275 my ($self, $opt) = @_;
276 return if $self->{idx_shards};
280 my $ALL = $self->git->{git_dir}; # ALL.git
281 PublicInbox::Import::init_bare($ALL) unless -d $ALL;
282 my $info_dir = "$ALL/objects/info";
283 my $alt = "$info_dir/alternates";
285 my (%old, @old, %new, @new);
287 open(my $fh, '<', $alt) or die "open $alt: $!";
288 $mode = (stat($fh))[2] & 07777;
290 push @old, $_ if !$old{$_}++;
293 for my $ibx (@{$self->{ibx_list}}) {
294 my $line = $ibx->git->{git_dir} . "/objects\n";
300 PublicInbox::V2Writable::write_alternates($info_dir, $mode, \@old);
301 $self->parallel_init($self->{indexlevel});
302 $self->umask_prepare;
303 $self->with_umask(\&PublicInbox::V2Writable::_idx_init, $self, $opt);
304 $self->{oidx}->begin_lazy;
305 $self->{oidx}->eidx_prep;
309 *done = \&PublicInbox::V2Writable::done;
310 *umask_prepare = \&PublicInbox::InboxWritable::umask_prepare;
311 *with_umask = \&PublicInbox::InboxWritable::with_umask;
312 *parallel_init = \&PublicInbox::V2Writable::parallel_init;
313 *nproc_shards = \&PublicInbox::V2Writable::nproc_shards;
314 *sync_prepare = \&PublicInbox::V2Writable::sync_prepare;
315 *index_todo = \&PublicInbox::V2Writable::index_todo;
316 *count_shards = \&PublicInbox::V2Writable::count_shards;
317 *atfork_child = \&PublicInbox::V2Writable::atfork_child;