1 # Copyright (C) 2015-2020 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 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
23 my $X = \%PublicInbox::Search::X;
24 my ($DB_CREATE_OR_OPEN, $DB_OPEN);
26 BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
27 0x7fffffff : 1_000_000,
28 DEBUG => !!$ENV{DEBUG},
31 my $xapianlevels = qr/\A(?:full|medium)\z/;
34 my ($class, $ibx, $creat, $shard) = @_;
35 ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
36 my $levels = qr/\A(?:full|medium|basic)\z/;
37 my $inboxdir = $ibx->{inboxdir};
38 my $version = $ibx->version;
39 my $indexlevel = 'full';
40 my $altid = $ibx->{altid};
42 require PublicInbox::AltId;
43 $altid = [ map { PublicInbox::AltId->new($ibx, $_); } @$altid ];
45 if ($ibx->{indexlevel}) {
46 if ($ibx->{indexlevel} =~ $levels) {
47 $indexlevel = $ibx->{indexlevel};
49 die("Invalid indexlevel $ibx->{indexlevel}\n");
52 $ibx = PublicInbox::InboxWritable->new($ibx);
54 inboxdir => $inboxdir,
59 indexlevel => $indexlevel,
61 $self->{-set_indexlevel_once} = 1 if $indexlevel eq 'medium';
64 $self->{lock_path} = "$inboxdir/ssoma.lock";
65 my $dir = $self->xdir;
66 $self->{over} = PublicInbox::OverIdx->new("$dir/over.sqlite3");
67 $self->{index_max_size} = $ibx->{index_max_size};
68 } elsif ($version == 2) {
69 defined $shard or die "shard is required for v2\n";
71 $self->{shard} = $shard;
72 $self->{lock_path} = undef;
74 die "unsupported inbox version=$version\n";
76 $self->{creat} = ($creat || 0) == 1;
80 sub need_xapian ($) { $_[0]->{indexlevel} =~ $xapianlevels }
84 if (need_xapian($self)) {
85 my $xdb = delete $self->{xdb} or croak 'not acquired';
88 $self->lock_release if $self->{creat};
92 sub load_xapian_writable () {
93 return 1 if $X->{WritableDatabase};
94 PublicInbox::Search::load_xapian() or return;
95 my $xap = $PublicInbox::Search::Xap;
96 for (qw(Document TermGenerator WritableDatabase)) {
97 $X->{$_} = $xap.'::'.$_;
99 eval 'require '.$X->{WritableDatabase} or die;
100 *sortable_serialise = $xap.'::sortable_serialise';
101 $DB_CREATE_OR_OPEN = eval($xap.'::DB_CREATE_OR_OPEN()');
102 $DB_OPEN = eval($xap.'::DB_OPEN()');
109 my $dir = $self->xdir;
110 if (need_xapian($self)) {
111 croak 'already acquired' if $self->{xdb};
112 load_xapian_writable();
113 $flag = $self->{creat} ? $DB_CREATE_OR_OPEN : $DB_OPEN;
115 if ($self->{creat}) {
119 # don't create empty Xapian directories if we don't need Xapian
120 my $is_shard = defined($self->{shard});
121 if (!$is_shard || ($is_shard && need_xapian($self))) {
122 File::Path::mkpath($dir);
125 return unless defined $flag;
126 my $xdb = eval { ($X->{WritableDatabase})->new($dir, $flag) };
128 die "Failed opening $dir: ", $@;
134 my ($doc, $col, $num) = @_;
135 $num = sortable_serialise($num);
136 $doc->add_value($col, $num);
139 sub term_generator ($) { # write-only
142 $self->{term_generator} //= do {
143 my $tg = $X->{TermGenerator}->new;
144 $tg->set_stemmer($self->stemmer);
149 sub index_text ($$$$) {
150 my ($self, $text, $wdf_inc, $prefix) = @_;
151 my $tg = term_generator($self); # man Search::Xapian::TermGenerator
153 if ($self->{indexlevel} eq 'full') {
154 $tg->index_text($text, $wdf_inc, $prefix);
155 $tg->increase_termpos;
157 $tg->index_text_without_positions($text, $wdf_inc, $prefix);
161 sub index_users ($$) {
162 my ($self, $smsg) = @_;
164 my $from = $smsg->from;
168 index_text($self, $from, 1, 'A'); # A - author
169 index_text($self, $to, 1, 'XTO') if $to ne '';
170 index_text($self, $cc, 1, 'XCC') if $cc ne '';
173 sub index_diff_inc ($$$$) {
174 my ($self, $text, $pfx, $xnq) = @_;
176 index_text($self, join("\n", @$xnq), 1, 'XNQ');
179 index_text($self, $text, 1, $pfx);
182 sub index_old_diff_fn {
183 my ($self, $seen, $fa, $fb, $xnq) = @_;
185 # no renames or space support for traditional diffs,
186 # find the number of leading common paths to strip:
187 my @fa = split('/', $fa);
188 my @fb = split('/', $fb);
189 while (scalar(@fa) && scalar(@fb)) {
190 $fa = join('/', @fa);
191 $fb = join('/', @fb);
193 unless ($seen->{$fa}++) {
194 index_diff_inc($self, $fa, 'XDFN', $xnq);
204 sub index_diff ($$$) {
205 my ($self, $txt, $doc) = @_;
210 foreach (split(/\n/, $txt)) {
211 if ($in_diff && s/^ //) { # diff context
212 index_diff_inc($self, $_, 'XDFCTX', $xnq);
213 } elsif (/^-- $/) { # email signature begins
215 } elsif (m!^diff --git "?[^/]+/.+ "?[^/]+/.+\z!) {
216 # wait until "---" and "+++" to capture filenames
219 } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
220 my ($opt, $fa, $fb) = ($1, $2, $3);
222 # only support unified:
223 next unless $opt =~ /[uU]/;
224 $in_diff = index_old_diff_fn($self, \%seen, $fa, $fb,
226 } elsif (m!^--- ("?[^/]+/.+)!) {
228 $fn = (split('/', git_unquote($fn), 2))[1];
229 $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
231 } elsif (m!^\+\+\+ ("?[^/]+/.+)!) {
233 $fn = (split('/', git_unquote($fn), 2))[1];
234 $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
236 } elsif (/^--- (\S+)/) {
239 } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
240 $in_diff = index_old_diff_fn($self, \%seen, $in_diff,
242 } elsif ($in_diff && s/^\+//) { # diff added
243 index_diff_inc($self, $_, 'XDFB', $xnq);
244 } elsif ($in_diff && s/^-//) { # diff removed
245 index_diff_inc($self, $_, 'XDFA', $xnq);
246 } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
247 my ($ba, $bb) = ($1, $2);
248 index_git_blob_id($doc, 'XDFPRE', $ba);
249 index_git_blob_id($doc, 'XDFPOST', $bb);
251 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
252 # traditional diff w/o -p
253 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
254 # hunk header context
255 index_diff_inc($self, $1, 'XDFHH', $xnq);
256 # ignore the following lines:
257 } elsif (/^(?:dis)similarity index/ ||
258 /^(?:old|new) mode/ ||
259 /^(?:deleted|new) file mode/ ||
260 /^(?:copy|rename) (?:from|to) / ||
261 /^(?:dis)?similarity index / ||
262 /^\\ No newline at end of file/ ||
263 /^Binary files .* differ/) {
266 # possible to be in diff context, some mail may be
267 # stripped by MUA or even GNU diff(1). "git apply"
268 # treats a bare "\n" as diff context, too
271 warn "non-diff line: $_\n" if DEBUG && $_ ne '';
276 index_text($self, join("\n", @xnq), 1, 'XNQ');
279 sub index_xapian { # msg_iter callback
280 my $part = $_[0]->[0]; # ignore $depth and @idx
281 my ($self, $doc) = @{$_[1]};
282 my $ct = $part->content_type || 'text/plain';
283 my $fn = $part->filename;
284 if (defined $fn && $fn ne '') {
285 index_text($self, $fn, 1, 'XFN');
288 my ($s, undef) = msg_part_text($part, $ct);
289 defined $s or return;
290 $_[0]->[0] = $part = undef; # free memory
292 # split off quoted and unquoted blocks:
293 my @sections = PublicInbox::MsgIter::split_quotes($s);
294 undef $s; # free memory
295 for my $txt (@sections) {
297 index_text($self, $txt, 0, 'XQUOT');
299 # does it look like a diff?
300 if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
301 index_diff($self, $txt, $doc);
303 index_text($self, $txt, 1, 'XNQ');
306 undef $txt; # free memory
310 sub add_xapian ($$$$) {
311 my ($self, $mime, $smsg, $mids) = @_;
312 $smsg->{mime} = $mime; # XXX dangerous
313 my $hdr = $mime->header_obj;
314 my $doc = $X->{Document}->new;
315 my $subj = $smsg->subject;
316 add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
317 my @ds = gmtime($smsg->{ds});
318 my $yyyymmdd = strftime('%Y%m%d', @ds);
319 add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
320 my $dt = strftime('%Y%m%d%H%M%S', @ds);
321 add_val($doc, PublicInbox::Search::DT(), $dt);
323 my $tg = term_generator($self);
325 $tg->set_document($doc);
326 index_text($self, $subj, 1, 'S') if $subj;
327 index_users($self, $smsg);
329 msg_iter($mime, \&index_xapian, [ $self, $doc ]);
330 foreach my $mid (@$mids) {
331 index_text($self, $mid, 1, 'XM');
333 # because too many Message-IDs are prefixed with
335 if ($mid =~ /\w{12,}/) {
336 my @long = ($mid =~ /(\w{3,}+)/g);
337 index_text($self, join(' ', @long), 1, 'XM');
340 $smsg->{to} = $smsg->{cc} = ''; # WWW doesn't need these, only NNTP
341 PublicInbox::OverIdx::parse_references($smsg, $hdr, $mids);
342 my $data = $smsg->to_doc_data;
343 $doc->set_data($data);
344 if (my $altid = $self->{-altid}) {
345 foreach my $alt (@$altid) {
346 my $pfx = $alt->{xprefix};
347 foreach my $mid (@$mids) {
348 my $id = $alt->mid2alt($mid);
349 next unless defined $id;
350 $doc->add_boolean_term($pfx . $id);
354 $doc->add_boolean_term('Q' . $_) foreach @$mids;
355 $self->{xdb}->replace_document($smsg->{num}, $doc);
358 sub _msgmap_init ($) {
360 die "BUG: _msgmap_init is only for v1\n" if $self->{ibx_ver} != 1;
361 $self->{mm} //= eval {
362 require PublicInbox::Msgmap;
363 PublicInbox::Msgmap->new($self->{inboxdir}, 1);
368 # mime = Email::MIME object
369 my ($self, $mime, $smsg) = @_;
370 my $hdr = $mime->header_obj;
371 my $mids = mids_for_index($hdr);
372 $smsg //= bless { blob => '' }, 'PublicInbox::Smsg'; # test-only compat
373 $smsg->{mid} //= $mids->[0]; # v1 compatibility
374 $smsg->{num} //= do { # v1
376 index_mm($self, $mime);
380 $smsg->{ds} //= msg_datestamp($hdr, $self->{autime});
381 $smsg->{ts} //= msg_timestamp($hdr, $self->{cotime});
384 # order matters, overview stores every possible piece of
385 # data in doc_data (deflated). Xapian only stores a subset
386 # of the fields which exist in over.sqlite3. We may stop
387 # storing doc_data in Xapian sometime after we get multi-inbox
389 if (my $over = $self->{over}) { # v1 only
390 $over->add_overview($mime, $smsg);
392 if (need_xapian($self)) {
393 add_xapian($self, $mime, $smsg, $mids);
398 warn "failed to index message <".join('> <',@$mids).">: $@\n";
404 # returns begin and end PostingIterator
406 my ($self, $termval) = @_;
407 my $db = $self->{xdb};
409 ($db->postlist_begin($termval), $db->postlist_end($termval));
414 my ($self, $termval, $cb) = @_;
415 my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
417 my ($head, $tail) = $self->find_doc_ids($termval);
418 return if $head == $tail;
420 for (; $head != $tail && @ids < $batch_size; $head++) {
421 push @ids, $head->get_docid;
427 # v1 only, where $mid is unique
429 my ($self, $mid) = @_;
430 $mid = mid_clean($mid);
432 if (my $over = $self->{over}) {
433 my $nr = eval { $over->remove_oid(undef, $mid) };
435 warn "failed to remove <$mid> from overview: $@\n";
437 warn "<$mid> missing for removal from overview\n";
440 return unless need_xapian($self);
441 my $db = $self->{xdb};
444 batch_do($self, 'Q' . $mid, sub {
446 $db->delete_document($_) for @$ids;
451 warn "failed to remove <$mid> from Xapian: $@\n";
453 warn "<$mid> missing for removal from Xapian\n";
457 # MID is a hint in V2
459 my ($self, $oid, $mid) = @_;
461 $self->{over}->remove_oid($oid, $mid) if $self->{over};
463 return unless need_xapian($self);
464 my $db = $self->{xdb};
466 # XXX careful, we cannot use batch_do here since we conditionally
467 # delete documents based on other factors, so we cannot call
468 # find_doc_ids twice.
469 my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
470 return if $head == $tail;
472 # there is only ONE element in @delete unless we
473 # have bugs in our v2writable deduplication check
475 for (; $head != $tail; $head++) {
476 my $docid = $head->get_docid;
477 my $doc = $db->get_document($docid);
478 my $smsg = PublicInbox::Smsg->wrap($mid);
479 $smsg->load_expand($doc);
480 if ($smsg->{blob} eq $oid) {
481 push(@delete, $docid);
484 $db->delete_document($_) foreach @delete;
488 sub index_git_blob_id {
489 my ($doc, $pfx, $objid) = @_;
491 my $len = length($objid);
492 for (my $len = length($objid); $len >= 7; ) {
493 $doc->add_term($pfx.$objid);
494 $objid = substr($objid, 0, --$len);
499 my ($self, $mime) = @_;
500 my $mid = eval { mid_mime($mime) };
501 $self->remove_message($mid) if defined $mid;
505 my ($self, $mime) = @_;
506 my $mid = mid_mime($mime);
507 my $mm = $self->{mm};
510 if (defined $self->{regen_down}) {
511 $num = $mm->num_for($mid) and return $num;
513 while (($num = $self->{regen_down}--) > 0) {
514 if ($mm->mid_set($num, $mid) != 0) {
518 } elsif (defined $self->{regen_up}) {
519 $num = $mm->num_for($mid) and return $num;
521 # this is to fixup old bugs due to add-remove-add
522 while (($num = ++$self->{regen_up})) {
523 if ($mm->mid_set($num, $mid) != 0) {
529 $num = $mm->mid_insert($mid) and return $num;
531 # fallback to num_for since filters like RubyLang set the number
536 my ($self, $mime) = @_;
537 $self->{mm}->mid_delete(mid_mime($mime));
541 my ($self, $mime, $smsg) = @_;
542 my $num = index_mm($self, $mime);
544 add_message($self, $mime, $smsg);
548 my ($self, $mime) = @_;
549 unindex_blob($self, $mime);
550 unindex_mm($self, $mime);
554 my ($git, $blob, $sizeref) = @_;
555 my $str = $git->cat_file($blob, $sizeref) or
556 die "BUG: $blob not found in $git->{git_dir}";
557 PublicInbox::MIME->new($str);
560 # called by public-inbox-index
562 my ($self, $opts) = @_;
563 delete $self->{lock_path} if $opts->{-skip_lock};
564 $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
567 sub batch_adjust ($$$$$) {
568 my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
572 $batch_cb->($nr, $latest);
577 my ($self, $git, $oid) = @_;
578 my $max_size = $self->{index_max_size} or return;
579 my (undef, undef, $size) = $git->check($oid);
580 die "E: bad $oid in $git->{git_dir}\n" if !defined($size);
581 return if $size <= $max_size;
582 warn "W: skipping $oid ($size > $max_size)\n";
588 my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
589 my $hex = '[a-f0-9]';
590 my $h40 = $hex .'{40}';
591 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
592 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
593 my $git = $self->{git};
596 my $max = BATCH_BYTES;
602 while (defined($line = <$log>)) {
603 if ($line =~ /$addmsg/o) {
605 if (delete $D{$blob}) {
606 if (defined $self->{regen_down}) {
607 my $num = $self->{regen_down}--;
608 $self->{mm}->num_highwater($num);
612 next if too_big($self, $git, $blob);
613 my $mime = do_cat_mail($git, $blob, \$bytes);
614 my $smsg = bless {}, 'PublicInbox::Smsg';
615 batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
616 $smsg->{blob} = $blob;
617 $smsg->{bytes} = $bytes;
618 $add_cb->($self, $mime, $smsg);
619 } elsif ($line =~ /$delmsg/o) {
621 $D{$blob} = 1 unless too_big($self, $git, $blob);
622 } elsif ($line =~ /^commit ($h40)/o) {
625 } elsif ($line =~ /^author .*? ([0-9]+) [\-\+][0-9]+$/) {
626 $self->{autime} = $1;
627 } elsif ($line =~ /^committer .*? ([0-9]+) [\-\+][0-9]+$/) {
628 $self->{cotime} = $1;
631 close($log) or die "git log failed: \$?=$?";
633 foreach my $blob (keys %D) {
634 my $mime = do_cat_mail($git, $blob, \$bytes);
635 $del_cb->($self, $mime);
637 $batch_cb->($nr, $latest, $newest);
641 my ($self, $opts, $range) = @_;
642 my $git = $self->{git};
644 if (index($range, '..') < 0) {
645 # don't show annoying git errors to users who run -index
647 $git->qx(qw(rev-parse -q --verify), "$range^0");
649 open my $fh, '<', '/dev/null' or
650 die "failed to open /dev/null: $!\n";
655 # Count the new files so they can be added newest to oldest
656 # and still have numbers increasing from oldest to newest
658 my $pr = $opts->{-progress};
659 $pr->("counting changes\n\t$range ... ") if $pr;
660 # can't use 'rev-list --count' if we use --diff-filter
661 my $fh = $git->popen(qw(log --pretty=tformat:%h
662 --no-notes --no-color --no-renames
663 --diff-filter=AM), $range);
664 ++$fcount while <$fh>;
665 close $fh or die "git log failed: \$?=$?";
666 my $high = $self->{mm}->num_highwater;
667 $pr->("$fcount\n") if $pr; # continue previous line
668 $self->{ntodo} = $fcount;
670 if (index($range, '..') < 0) {
671 if ($high && $high == $fcount) {
672 # fix up old bugs in full indexes which caused messages to
673 # not appear in Msgmap
674 $self->{regen_up} = $high;
676 # normal regen is for for fresh data
677 $self->{regen_down} = $fcount;
680 # Give oldest messages the smallest numbers
681 $self->{regen_down} = $high + $fcount;
684 $git->popen(qw/log --pretty=raw --no-notes --no-color --no-renames
685 --raw -r --no-abbrev/, $range);
688 # --is-ancestor requires git 1.8.0+
689 sub is_ancestor ($$$) {
690 my ($git, $cur, $tip) = @_;
691 return 0 unless $git->check($cur);
692 my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
693 qw(merge-base --is-ancestor), $cur, $tip ];
694 my $pid = spawn($cmd);
695 waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
699 sub need_update ($$$) {
700 my ($self, $cur, $new) = @_;
701 my $git = $self->{git};
702 return 1 if $cur && !is_ancestor($git, $cur, $new);
703 my $range = $cur eq '' ? $new : "$cur..$new";
704 chomp(my $n = $git->qx(qw(rev-list --count), $range));
705 ($n eq '' || $n > 0);
708 # The last git commit we indexed with Xapian or SQLite (msgmap)
709 # This needs to account for cases where Xapian or SQLite is
710 # out-of-date with respect to the other.
712 my ($self, $mm) = @_;
713 my $lm = $mm->last_commit || '';
715 if (need_xapian($self)) {
716 $lx = $self->{xdb}->get_metadata('last_commit') || '';
720 # Use last_commit from msgmap if it is older or unset
721 if (!$lm || ($lx && $lm && is_ancestor($self->{git}, $lm, $lx))) {
727 sub reindex_from ($$) {
728 my ($reindex, $last_commit) = @_;
729 return $last_commit unless $reindex;
730 ref($reindex) eq 'HASH' ? $reindex->{from} : '';
733 # indexes all unindexed messages (v1 only)
735 my ($self, $opts) = @_;
736 my $tip = $opts->{ref} || 'HEAD';
737 my ($last_commit, $lx, $xlog);
738 my $git = $self->{git};
740 my $pr = $opts->{-progress};
742 my $xdb = $self->begin_txn_lazy;
743 my $mm = _msgmap_init($self);
746 close($xlog) or die "git log failed: \$?=$?";
749 $last_commit = _last_x_commit($self, $mm);
750 $lx = reindex_from($opts->{reindex}, $last_commit);
752 $self->{over}->rollback_lazy;
753 $self->{over}->disconnect;
756 $xdb->cancel_transaction if $xdb;
757 $xdb = _xdb_release($self);
759 # ensure we leak no FDs to "git log" with Xapian <= 1.2
760 my $range = $lx eq '' ? $tip : "$lx..$tip";
761 $xlog = _git_log($self, $opts, $range);
763 $xdb = $self->begin_txn_lazy;
764 } while (_last_x_commit($self, $mm) ne $last_commit);
766 my $dbh = $mm->{dbh} if $mm;
768 my ($nr, $commit, $newest) = @_;
771 my $cur = $mm->last_commit || '';
772 if (need_update($self, $cur, $newest)) {
773 $mm->last_commit($newest);
778 if ($newest && need_xapian($self)) {
779 my $cur = $xdb->get_metadata('last_commit');
780 if (need_update($self, $cur, $newest)) {
781 $xdb->set_metadata('last_commit', $newest);
784 $self->commit_txn_lazy;
786 $xdb = _xdb_release($self);
787 # let another process do some work... <
788 $pr->("indexed $nr/$self->{ntodo}\n") if $pr && $nr;
790 $xdb = $self->begin_txn_lazy;
791 $dbh->begin_work if $dbh;
796 read_log($self, $xlog, *index_both, *unindex_both, $cb);
800 # order matters for unlocking
801 $_[0]->{xdb} = undef;
802 $_[0]->{lockfh} = undef;
805 # remote_* subs are only used by SearchIdxPart
808 if (my $w = $self->{w}) {
809 print $w "commit\n" or die "failed to write commit: $!";
811 $self->commit_txn_lazy;
817 if (my $w = delete $self->{w}) {
818 my $pid = delete $self->{pid} or die "no process to wait on\n";
819 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
820 close $w or die "failed to close pipe for pid:$pid: $!\n";
821 waitpid($pid, 0) == $pid or die "remote process did not finish";
822 $? == 0 or die ref($self)." pid:$pid exited with: $?";
824 die "transaction in progress $self\n" if $self->{txn};
825 $self->_xdb_release if $self->{xdb};
830 my ($self, $oid, $mid) = @_;
831 if (my $w = $self->{w}) {
832 # triggers remove_by_oid in a shard
833 print $w "D $oid $mid\n" or die "failed to write remove $!";
835 $self->begin_txn_lazy;
836 $self->remove_by_oid($oid, $mid);
842 return if $self->{txn};
844 $self->{-inbox}->with_umask(sub {
845 my $xdb = $self->{xdb} || $self->_xdb_acquire;
846 $self->{over}->begin_lazy if $self->{over};
847 $xdb->begin_transaction if $xdb;
853 # store 'indexlevel=medium' in v2 shard=0 and v1 (only one shard)
854 # This metadata is read by Admin::detect_indexlevel:
858 if (!$self->{shard} && # undef or 0, not >0
859 delete($self->{-set_indexlevel_once})) {
860 my $xdb = $self->{xdb};
861 my $level = $xdb->get_metadata('indexlevel');
862 if (!$level || $level ne 'medium') {
863 $xdb->set_metadata('indexlevel', 'medium');
868 sub commit_txn_lazy {
870 delete $self->{txn} or return;
871 $self->{-inbox}->with_umask(sub {
872 if (my $xdb = $self->{xdb}) {
873 set_indexlevel($self);
874 $xdb->commit_transaction;
876 $self->{over}->commit_lazy if $self->{over};
882 if (need_xapian($self)) {
883 die "$$ $0 xdb not released\n" if $self->{xdb};
885 die "$$ $0 still in transaction\n" if $self->{txn};