1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # based on notmuch, but with no concept of folders, files or flags
5 # Indexes mail with Xapian and our (SQLite-based) ::Msgmap for use
6 # with the web and NNTP interfaces. This index maintains thread
7 # relationships for use by PublicInbox::SearchThread.
8 # This writes to the search index.
9 package PublicInbox::SearchIdx;
12 use base qw(PublicInbox::Search PublicInbox::Lock);
13 use PublicInbox::MIME;
14 use PublicInbox::InboxWritable;
15 use PublicInbox::MID qw/mid_clean mid_mime mids_for_index/;
16 use PublicInbox::MsgIter;
18 use POSIX qw(strftime);
19 use PublicInbox::OverIdx;
20 use PublicInbox::Spawn qw(spawn);
21 use PublicInbox::Git qw(git_unquote);
22 my $X = \%PublicInbox::Search::X;
23 my ($DB_CREATE_OR_OPEN, $DB_OPEN);
25 BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
26 0x7fffffff : 1_000_000,
27 DEBUG => !!$ENV{DEBUG},
30 my $xapianlevels = qr/\A(?:full|medium)\z/;
33 my ($class, $ibx, $creat, $shard) = @_;
34 ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
35 my $levels = qr/\A(?:full|medium|basic)\z/;
36 my $inboxdir = $ibx->{inboxdir};
37 my $version = $ibx->version;
38 my $indexlevel = 'full';
39 my $altid = $ibx->{altid};
41 require PublicInbox::AltId;
42 $altid = [ map { PublicInbox::AltId->new($ibx, $_); } @$altid ];
44 if ($ibx->{indexlevel}) {
45 if ($ibx->{indexlevel} =~ $levels) {
46 $indexlevel = $ibx->{indexlevel};
48 die("Invalid indexlevel $ibx->{indexlevel}\n");
51 $ibx = PublicInbox::InboxWritable->new($ibx);
53 inboxdir => $inboxdir,
58 indexlevel => $indexlevel,
62 $self->{lock_path} = "$inboxdir/ssoma.lock";
63 my $dir = $self->xdir;
64 $self->{over} = PublicInbox::OverIdx->new("$dir/over.sqlite3");
65 } elsif ($version == 2) {
66 defined $shard or die "shard is required for v2\n";
68 $self->{shard} = $shard;
69 $self->{lock_path} = undef;
71 die "unsupported inbox version=$version\n";
73 $self->{creat} = ($creat || 0) == 1;
77 sub need_xapian ($) { $_[0]->{indexlevel} =~ $xapianlevels }
81 if (need_xapian($self)) {
82 my $xdb = delete $self->{xdb} or croak 'not acquired';
85 $self->lock_release if $self->{creat};
89 sub load_xapian_writable () {
90 return 1 if $X->{WritableDatabase};
91 PublicInbox::Search::load_xapian() or return;
92 my $xap = $PublicInbox::Search::Xap;
93 for (qw(Document TermGenerator WritableDatabase)) {
94 $X->{$_} = $xap.'::'.$_;
96 eval 'require '.$X->{WritableDatabase} or die;
97 *sortable_serialise = $xap.'::sortable_serialise';
98 $DB_CREATE_OR_OPEN = eval($xap.'::DB_CREATE_OR_OPEN()');
99 $DB_OPEN = eval($xap.'::DB_OPEN()');
106 my $dir = $self->xdir;
107 if (need_xapian($self)) {
108 croak 'already acquired' if $self->{xdb};
109 load_xapian_writable();
110 $flag = $self->{creat} ? $DB_CREATE_OR_OPEN : $DB_OPEN;
112 if ($self->{creat}) {
116 # don't create empty Xapian directories if we don't need Xapian
117 my $is_shard = defined($self->{shard});
118 if (!$is_shard || ($is_shard && need_xapian($self))) {
119 File::Path::mkpath($dir);
122 return unless defined $flag;
123 my $xdb = eval { ($X->{WritableDatabase})->new($dir, $flag) };
125 die "Failed opening $dir: ", $@;
131 my ($doc, $col, $num) = @_;
132 $num = sortable_serialise($num);
133 $doc->add_value($col, $num);
136 sub term_generator ($) { # write-only
139 $self->{term_generator} //= do {
140 my $tg = $X->{TermGenerator}->new;
141 $tg->set_stemmer($self->stemmer);
146 sub index_text ($$$$) {
147 my ($self, $text, $wdf_inc, $prefix) = @_;
148 my $tg = term_generator($self); # man Search::Xapian::TermGenerator
150 if ($self->{indexlevel} eq 'full') {
151 $tg->index_text($text, $wdf_inc, $prefix);
152 $tg->increase_termpos;
154 $tg->index_text_without_positions($text, $wdf_inc, $prefix);
158 sub index_users ($$) {
159 my ($self, $smsg) = @_;
161 my $from = $smsg->from;
165 index_text($self, $from, 1, 'A'); # A - author
166 index_text($self, $to, 1, 'XTO') if $to ne '';
167 index_text($self, $cc, 1, 'XCC') if $cc ne '';
170 sub index_diff_inc ($$$$) {
171 my ($self, $text, $pfx, $xnq) = @_;
173 index_text($self, join("\n", @$xnq), 1, 'XNQ');
176 index_text($self, $text, 1, $pfx);
179 sub index_old_diff_fn {
180 my ($self, $seen, $fa, $fb, $xnq) = @_;
182 # no renames or space support for traditional diffs,
183 # find the number of leading common paths to strip:
184 my @fa = split('/', $fa);
185 my @fb = split('/', $fb);
186 while (scalar(@fa) && scalar(@fb)) {
187 $fa = join('/', @fa);
188 $fb = join('/', @fb);
190 unless ($seen->{$fa}++) {
191 index_diff_inc($self, $fa, 'XDFN', $xnq);
201 sub index_diff ($$$) {
202 my ($self, $txt, $doc) = @_;
207 foreach (split(/\n/, $txt)) {
208 if ($in_diff && s/^ //) { # diff context
209 index_diff_inc($self, $_, 'XDFCTX', $xnq);
210 } elsif (/^-- $/) { # email signature begins
212 } elsif (m!^diff --git "?[^/]+/.+ "?[^/]+/.+\z!) {
213 # wait until "---" and "+++" to capture filenames
216 } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
217 my ($opt, $fa, $fb) = ($1, $2, $3);
219 # only support unified:
220 next unless $opt =~ /[uU]/;
221 $in_diff = index_old_diff_fn($self, \%seen, $fa, $fb,
223 } elsif (m!^--- ("?[^/]+/.+)!) {
225 $fn = (split('/', git_unquote($fn), 2))[1];
226 $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
228 } elsif (m!^\+\+\+ ("?[^/]+/.+)!) {
230 $fn = (split('/', git_unquote($fn), 2))[1];
231 $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
233 } elsif (/^--- (\S+)/) {
236 } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
237 $in_diff = index_old_diff_fn($self, \%seen, $in_diff,
239 } elsif ($in_diff && s/^\+//) { # diff added
240 index_diff_inc($self, $_, 'XDFB', $xnq);
241 } elsif ($in_diff && s/^-//) { # diff removed
242 index_diff_inc($self, $_, 'XDFA', $xnq);
243 } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
244 my ($ba, $bb) = ($1, $2);
245 index_git_blob_id($doc, 'XDFPRE', $ba);
246 index_git_blob_id($doc, 'XDFPOST', $bb);
248 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
249 # traditional diff w/o -p
250 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
251 # hunk header context
252 index_diff_inc($self, $1, 'XDFHH', $xnq);
253 # ignore the following lines:
254 } elsif (/^(?:dis)similarity index/ ||
255 /^(?:old|new) mode/ ||
256 /^(?:deleted|new) file mode/ ||
257 /^(?:copy|rename) (?:from|to) / ||
258 /^(?:dis)?similarity index / ||
259 /^\\ No newline at end of file/ ||
260 /^Binary files .* differ/) {
263 # possible to be in diff context, some mail may be
264 # stripped by MUA or even GNU diff(1). "git apply"
265 # treats a bare "\n" as diff context, too
268 warn "non-diff line: $_\n" if DEBUG && $_ ne '';
273 index_text($self, join("\n", @xnq), 1, 'XNQ');
276 sub index_body ($$$) {
277 my ($self, $txt, $doc) = @_;
279 # does it look like a diff?
280 if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
281 index_diff($self, $txt, $doc);
283 index_text($self, $txt, 1, 'XNQ');
286 index_text($self, $txt, 0, 'XQUOT');
290 sub index_xapian { # msg_iter callback
291 my ($part, $depth, @idx) = @{$_[0]};
292 my ($self, $doc) = @{$_[1]};
293 my $ct = $part->content_type || 'text/plain';
294 my $fn = $part->filename;
295 if (defined $fn && $fn ne '') {
296 index_text($self, $fn, 1, 'XFN');
299 my ($s, undef) = msg_part_text($part, $ct);
300 defined $s or return;
302 # split off quoted and unquoted blocks:
303 my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
305 index_body($self, $_, /\A>/ ? 0 : $doc) for @sections;
308 sub add_xapian ($$$$$$) {
309 my ($self, $mime, $num, $oid, $mids, $mid0) = @_;
310 my $smsg = PublicInbox::SearchMsg->new($mime);
311 my $doc = $X->{Document}->new;
312 my $subj = $smsg->subject;
313 add_val($doc, PublicInbox::Search::TS(), $smsg->ts);
314 my @ds = gmtime($smsg->ds);
315 my $yyyymmdd = strftime('%Y%m%d', @ds);
316 add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
317 my $dt = strftime('%Y%m%d%H%M%S', @ds);
318 add_val($doc, PublicInbox::Search::DT(), $dt);
320 my $tg = term_generator($self);
322 $tg->set_document($doc);
323 index_text($self, $subj, 1, 'S') if $subj;
324 index_users($self, $smsg);
326 msg_iter($mime, \&index_xapian, [ $self, $doc ]);
327 foreach my $mid (@$mids) {
328 index_text($self, $mid, 1, 'XM');
330 # because too many Message-IDs are prefixed with
332 if ($mid =~ /\w{12,}/) {
333 my @long = ($mid =~ /(\w{3,}+)/g);
334 index_text($self, join(' ', @long), 1, 'XM');
337 $smsg->{to} = $smsg->{cc} = '';
338 PublicInbox::OverIdx::parse_references($smsg, $mid0, $mids);
339 my $data = $smsg->to_doc_data($oid, $mid0);
340 $doc->set_data($data);
341 if (my $altid = $self->{-altid}) {
342 foreach my $alt (@$altid) {
343 my $pfx = $alt->{xprefix};
344 foreach my $mid (@$mids) {
345 my $id = $alt->mid2alt($mid);
346 next unless defined $id;
347 $doc->add_boolean_term($pfx . $id);
351 $doc->add_boolean_term('Q' . $_) foreach @$mids;
352 $self->{xdb}->replace_document($num, $doc);
355 sub _msgmap_init ($) {
357 die "BUG: _msgmap_init is only for v1\n" if $self->{ibx_ver} != 1;
358 $self->{mm} //= eval {
359 require PublicInbox::Msgmap;
360 PublicInbox::Msgmap->new($self->{inboxdir}, 1);
365 # mime = Email::MIME object
366 my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
367 my $mids = mids_for_index($mime->header_obj);
368 $mid0 //= $mids->[0]; # v1 compatibility
371 index_mm($self, $mime);
374 if (need_xapian($self)) {
375 add_xapian($self, $mime, $num, $oid, $mids, $mid0);
377 if (my $over = $self->{over}) {
378 $over->add_overview($mime, $bytes, $num, $oid, $mid0);
383 warn "failed to index message <".join('> <',@$mids).">: $@\n";
389 # returns begin and end PostingIterator
391 my ($self, $termval) = @_;
392 my $db = $self->{xdb};
394 ($db->postlist_begin($termval), $db->postlist_end($termval));
399 my ($self, $termval, $cb) = @_;
400 my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
402 my ($head, $tail) = $self->find_doc_ids($termval);
403 return if $head == $tail;
405 for (; $head != $tail && @ids < $batch_size; $head++) {
406 push @ids, $head->get_docid;
412 # v1 only, where $mid is unique
414 my ($self, $mid) = @_;
415 $mid = mid_clean($mid);
417 if (my $over = $self->{over}) {
418 my $nr = eval { $over->remove_oid(undef, $mid) };
420 warn "failed to remove <$mid> from overview: $@\n";
422 warn "<$mid> missing for removal from overview\n";
425 return unless need_xapian($self);
426 my $db = $self->{xdb};
429 batch_do($self, 'Q' . $mid, sub {
431 $db->delete_document($_) for @$ids;
436 warn "failed to remove <$mid> from Xapian: $@\n";
438 warn "<$mid> missing for removal from Xapian\n";
442 # MID is a hint in V2
444 my ($self, $oid, $mid) = @_;
446 $self->{over}->remove_oid($oid, $mid) if $self->{over};
448 return unless need_xapian($self);
449 my $db = $self->{xdb};
451 # XXX careful, we cannot use batch_do here since we conditionally
452 # delete documents based on other factors, so we cannot call
453 # find_doc_ids twice.
454 my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
455 return if $head == $tail;
457 # there is only ONE element in @delete unless we
458 # have bugs in our v2writable deduplication check
460 for (; $head != $tail; $head++) {
461 my $docid = $head->get_docid;
462 my $doc = $db->get_document($docid);
463 my $smsg = PublicInbox::SearchMsg->wrap($mid);
464 $smsg->load_expand($doc);
465 if ($smsg->{blob} eq $oid) {
466 push(@delete, $docid);
469 $db->delete_document($_) foreach @delete;
473 sub index_git_blob_id {
474 my ($doc, $pfx, $objid) = @_;
476 my $len = length($objid);
477 for (my $len = length($objid); $len >= 7; ) {
478 $doc->add_term($pfx.$objid);
479 $objid = substr($objid, 0, --$len);
484 my ($self, $mime) = @_;
485 my $mid = eval { mid_clean(mid_mime($mime)) };
486 $self->remove_message($mid) if defined $mid;
490 my ($self, $mime) = @_;
491 my $mid = mid_clean(mid_mime($mime));
492 my $mm = $self->{mm};
495 if (defined $self->{regen_down}) {
496 $num = $mm->num_for($mid) and return $num;
498 while (($num = $self->{regen_down}--) > 0) {
499 if ($mm->mid_set($num, $mid) != 0) {
503 } elsif (defined $self->{regen_up}) {
504 $num = $mm->num_for($mid) and return $num;
506 # this is to fixup old bugs due to add-remove-add
507 while (($num = ++$self->{regen_up})) {
508 if ($mm->mid_set($num, $mid) != 0) {
514 $num = $mm->mid_insert($mid) and return $num;
516 # fallback to num_for since filters like RubyLang set the number
521 my ($self, $mime) = @_;
522 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
526 my ($self, $mime, $bytes, $blob) = @_;
527 my $num = index_mm($self, $mime);
528 add_message($self, $mime, $bytes, $num, $blob);
532 my ($self, $mime) = @_;
533 unindex_blob($self, $mime);
534 unindex_mm($self, $mime);
538 my ($git, $blob, $sizeref) = @_;
540 my $str = $git->cat_file($blob, $sizeref);
541 # fixup bugs from import:
542 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
543 PublicInbox::MIME->new($str);
548 # called by public-inbox-index
550 my ($self, $opts) = @_;
551 delete $self->{lock_path} if $opts->{-skip_lock};
552 $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
555 sub batch_adjust ($$$$$) {
556 my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
560 $batch_cb->($nr, $latest);
566 my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
567 my $hex = '[a-f0-9]';
568 my $h40 = $hex .'{40}';
569 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
570 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
571 my $git = $self->{git};
574 my $max = BATCH_BYTES;
580 while (defined($line = <$log>)) {
581 if ($line =~ /$addmsg/o) {
583 if (delete $D{$blob}) {
584 if (defined $self->{regen_down}) {
585 my $num = $self->{regen_down}--;
586 $self->{mm}->num_highwater($num);
590 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
591 batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
592 $add_cb->($self, $mime, $bytes, $blob);
593 } elsif ($line =~ /$delmsg/o) {
596 } elsif ($line =~ /^commit ($h40)/o) {
601 close($log) or die "git log failed: \$?=$?";
603 foreach my $blob (keys %D) {
604 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
605 $del_cb->($self, $mime);
607 $batch_cb->($nr, $latest, $newest);
611 my ($self, $opts, $range) = @_;
612 my $git = $self->{git};
614 if (index($range, '..') < 0) {
615 # don't show annoying git errrors to users who run -index
617 $git->qx(qw(rev-parse -q --verify), "$range^0");
619 open my $fh, '<', '/dev/null' or
620 die "failed to open /dev/null: $!\n";
625 # Count the new files so they can be added newest to oldest
626 # and still have numbers increasing from oldest to newest
628 my $pr = $opts->{-progress};
629 $pr->("counting changes\n\t$range ... ") if $pr;
630 # can't use 'rev-list --count' if we use --diff-filter
631 my $fh = $git->popen(qw(log --pretty=tformat:%h
632 --no-notes --no-color --no-renames
633 --diff-filter=AM), $range);
634 ++$fcount while <$fh>;
635 close $fh or die "git log failed: \$?=$?";
636 my $high = $self->{mm}->num_highwater;
637 $pr->("$fcount\n") if $pr; # continue previous line
638 $self->{ntodo} = $fcount;
640 if (index($range, '..') < 0) {
641 if ($high && $high == $fcount) {
642 # fix up old bugs in full indexes which caused messages to
643 # not appear in Msgmap
644 $self->{regen_up} = $high;
646 # normal regen is for for fresh data
647 $self->{regen_down} = $fcount;
650 # Give oldest messages the smallest numbers
651 $self->{regen_down} = $high + $fcount;
654 $git->popen(qw/log --no-notes --no-color --no-renames
655 --raw -r --no-abbrev/, $range);
658 # --is-ancestor requires git 1.8.0+
659 sub is_ancestor ($$$) {
660 my ($git, $cur, $tip) = @_;
661 return 0 unless $git->check($cur);
662 my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
663 qw(merge-base --is-ancestor), $cur, $tip ];
664 my $pid = spawn($cmd);
665 waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
669 sub need_update ($$$) {
670 my ($self, $cur, $new) = @_;
671 my $git = $self->{git};
672 return 1 if $cur && !is_ancestor($git, $cur, $new);
673 my $range = $cur eq '' ? $new : "$cur..$new";
674 chomp(my $n = $git->qx(qw(rev-list --count), $range));
675 ($n eq '' || $n > 0);
678 # The last git commit we indexed with Xapian or SQLite (msgmap)
679 # This needs to account for cases where Xapian or SQLite is
680 # out-of-date with respect to the other.
682 my ($self, $mm) = @_;
683 my $lm = $mm->last_commit || '';
685 if (need_xapian($self)) {
686 $lx = $self->{xdb}->get_metadata('last_commit') || '';
690 # Use last_commit from msgmap if it is older or unset
691 if (!$lm || ($lx && $lm && is_ancestor($self->{git}, $lm, $lx))) {
697 sub reindex_from ($$) {
698 my ($reindex, $last_commit) = @_;
699 return $last_commit unless $reindex;
700 ref($reindex) eq 'HASH' ? $reindex->{from} : '';
703 # indexes all unindexed messages (v1 only)
705 my ($self, $opts) = @_;
706 my $tip = $opts->{ref} || 'HEAD';
707 my ($last_commit, $lx, $xlog);
708 my $git = $self->{git};
710 my $pr = $opts->{-progress};
712 my $xdb = $self->begin_txn_lazy;
713 my $mm = _msgmap_init($self);
716 close($xlog) or die "git log failed: \$?=$?";
719 $last_commit = _last_x_commit($self, $mm);
720 $lx = reindex_from($opts->{reindex}, $last_commit);
722 $self->{over}->rollback_lazy;
723 $self->{over}->disconnect;
726 $xdb->cancel_transaction if $xdb;
727 $xdb = _xdb_release($self);
729 # ensure we leak no FDs to "git log" with Xapian <= 1.2
730 my $range = $lx eq '' ? $tip : "$lx..$tip";
731 $xlog = _git_log($self, $opts, $range);
733 $xdb = $self->begin_txn_lazy;
734 } while (_last_x_commit($self, $mm) ne $last_commit);
736 my $dbh = $mm->{dbh} if $mm;
738 my ($nr, $commit, $newest) = @_;
741 my $cur = $mm->last_commit || '';
742 if (need_update($self, $cur, $newest)) {
743 $mm->last_commit($newest);
748 if ($newest && need_xapian($self)) {
749 my $cur = $xdb->get_metadata('last_commit');
750 if (need_update($self, $cur, $newest)) {
751 $xdb->set_metadata('last_commit', $newest);
754 $self->commit_txn_lazy;
756 $xdb = _xdb_release($self);
757 # let another process do some work... <
758 $pr->("indexed $nr/$self->{ntodo}\n") if $pr && $nr;
760 $xdb = $self->begin_txn_lazy;
761 $dbh->begin_work if $dbh;
766 read_log($self, $xlog, *index_both, *unindex_both, $cb);
770 # order matters for unlocking
771 $_[0]->{xdb} = undef;
772 $_[0]->{lockfh} = undef;
775 # remote_* subs are only used by SearchIdxPart
778 if (my $w = $self->{w}) {
779 print $w "commit\n" or die "failed to write commit: $!";
781 $self->commit_txn_lazy;
787 if (my $w = delete $self->{w}) {
788 my $pid = delete $self->{pid} or die "no process to wait on\n";
789 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
790 close $w or die "failed to close pipe for pid:$pid: $!\n";
791 waitpid($pid, 0) == $pid or die "remote process did not finish";
792 $? == 0 or die ref($self)." pid:$pid exited with: $?";
794 die "transaction in progress $self\n" if $self->{txn};
795 $self->_xdb_release if $self->{xdb};
800 my ($self, $oid, $mid) = @_;
801 if (my $w = $self->{w}) {
802 # triggers remove_by_oid in a shard
803 print $w "D $oid $mid\n" or die "failed to write remove $!";
805 $self->begin_txn_lazy;
806 $self->remove_by_oid($oid, $mid);
812 return if $self->{txn};
814 $self->{-inbox}->with_umask(sub {
815 my $xdb = $self->{xdb} || $self->_xdb_acquire;
816 $self->{over}->begin_lazy if $self->{over};
817 $xdb->begin_transaction if $xdb;
823 sub commit_txn_lazy {
825 delete $self->{txn} or return;
826 $self->{-inbox}->with_umask(sub {
827 if (my $xdb = $self->{xdb}) {
829 # store 'indexlevel=medium' in v2 shard=0 and
830 # v1 (only one shard)
831 # This metadata is read by Admin::detect_indexlevel:
832 if (!$self->{shard} # undef or 0, not >0
833 && $self->{indexlevel} eq 'medium') {
834 $xdb->set_metadata('indexlevel', 'medium');
837 $xdb->commit_transaction;
839 $self->{over}->commit_lazy if $self->{over};
845 if (need_xapian($self)) {
846 die "$$ $0 xdb not released\n" if $self->{xdb};
848 die "$$ $0 still in transaction\n" if $self->{txn};