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, $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";
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->{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);
118 $idx->shard_remove_xref3($docid, $oid, $xibx, $eml);
122 sub index_unseen ($) {
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});
135 sub do_finalize ($) {
137 if (my $indexed = $req->{indexed}) {
138 do_xpost($req, $_) for @$indexed;
139 } elsif (exists $req->{new_smsg}) { # totally unseen messsage
142 warn "W: ignoring delete $req->{oid} (not found)\n";
146 sub do_step ($) { # main iterator for adding messages to the index
148 my $self = $req->{eidx};
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
157 delete $req->{cur_smsg};
158 delete $req->{next_arg};
160 my $mid = shift(@{$req->{mids}});
161 last unless defined $mid;
163 $req->{next_arg} = [ $mid, \$id, \$prev ];
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 }
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';
187 git_blob_digest($bref)->hexdigest eq $req->{oid} or die
188 "BUG: blob mismatch $req->{oid}";
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)) {
196 while (my $x = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
197 return $x->{num} if $x->{blob} eq $req->{oid};
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 {
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};
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
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");
230 sub _sync_inbox ($$$) {
231 my ($self, $opt, $ibx) = @_;
233 need_checkpoint => \(my $bool = 0),
234 unindex_range => {}, # EPOCH => oid_old..oid_new
235 reindex => $opt->{reindex},
240 my $key = $ibx->eidx_key;
241 my $u = $ibx->uidvalidity;
242 my $oidx = $self->{oidx};
243 my $v = $ibx->version;
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");
254 my $lc = $oidx->eidx_meta("lc-v1:$key//$u");
255 prepare_stack($sync, $lc ? "$lc..HEAD" : 'HEAD');
257 warn "E: $key unsupported inbox version (v$v)\n";
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);
267 _sync_inbox($self, $opt, $_) for (@{$self->{ibx_list}});
270 sub idx_init { # similar to V2Writable
271 my ($self, $opt) = @_;
272 return if $self->{idx_shards};
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";
281 my (%old, @old, %new, @new);
283 open(my $fh, '<', $alt) or die "open $alt: $!";
284 $mode = (stat($fh))[2] & 07777;
286 push @old, $_ if !$old{$_}++;
289 for my $ibx (@{$self->{ibx_list}}) {
290 my $line = $ibx->git->{git_dir} . "/objects\n";
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;
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;