1 # Copyright (C) 2015-2018 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 id_compress mid_mime mids references/;
16 use PublicInbox::MsgIter;
18 use POSIX qw(strftime);
19 require PublicInbox::Git;
22 BATCH_BYTES => 1_000_000,
23 DEBUG => !!$ENV{DEBUG},
38 return $s unless ($s =~ /\A"(.*)"\z/);
40 $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
41 $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
46 my ($class, $ibx, $creat, $part) = @_;
47 my $mainrepo = $ibx; # for "public-inbox-index" w/o entry in config
48 my $git_dir = $mainrepo;
52 $mainrepo = $ibx->{mainrepo};
53 $altid = $ibx->{altid};
54 $version = $ibx->{version} || 1;
56 require PublicInbox::AltId;
58 PublicInbox::AltId->new($ibx, $_);
62 $ibx = { mainrepo => $git_dir, version => 1 };
64 $ibx = PublicInbox::InboxWritable->new($ibx);
65 require Search::Xapian::WritableDatabase;
67 mainrepo => $mainrepo,
75 $self->{lock_path} = "$mainrepo/ssoma.lock";
76 } elsif ($version == 2) {
77 defined $part or die "partition is required for v2\n";
78 # partition is a number or "all"
79 $self->{partition} = $part;
80 $self->{lock_path} = undef;
81 $self->{msgmap_path} = "$mainrepo/msgmap.sqlite3";
83 die "unsupported inbox version=$version\n";
85 $self->{creat} = ($creat || 0) == 1;
91 my $xdb = delete $self->{xdb} or croak 'not acquired';
93 $self->lock_release if $self->{creat};
99 croak 'already acquired' if $self->{xdb};
100 my $dir = $self->xdir;
101 my $flag = Search::Xapian::DB_OPEN;
102 if ($self->{creat}) {
105 File::Path::mkpath($dir);
106 $flag = Search::Xapian::DB_CREATE_OR_OPEN;
108 $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
112 my ($doc, $col, $num) = @_;
113 $num = Search::Xapian::sortable_serialise($num);
114 $doc->add_value($col, $num);
118 my ($doc, $ts, $ds, $num) = @_;
119 add_val($doc, PublicInbox::Search::TS, $ts);
120 my $yyyymmdd = strftime('%Y%m%d', gmtime($ds));
121 add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd);
122 defined($num) and add_val($doc, PublicInbox::Search::NUM, $num);
125 sub index_users ($$) {
126 my ($tg, $smsg) = @_;
128 my $from = $smsg->from;
132 $tg->index_text($from, 1, 'A'); # A - author
133 $tg->increase_termpos;
134 $tg->index_text($to, 1, 'XTO') if $to ne '';
135 $tg->increase_termpos;
136 $tg->index_text($cc, 1, 'XCC') if $cc ne '';
137 $tg->increase_termpos;
140 sub index_diff_inc ($$$$) {
141 my ($tg, $text, $pfx, $xnq) = @_;
143 $tg->index_text(join("\n", @$xnq), 1, 'XNQ');
144 $tg->increase_termpos;
147 $tg->index_text($text, 1, $pfx);
148 $tg->increase_termpos;
151 sub index_old_diff_fn {
152 my ($tg, $seen, $fa, $fb, $xnq) = @_;
154 # no renames or space support for traditional diffs,
155 # find the number of leading common paths to strip:
156 my @fa = split('/', $fa);
157 my @fb = split('/', $fb);
158 while (scalar(@fa) && scalar(@fb)) {
159 $fa = join('/', @fa);
160 $fb = join('/', @fb);
162 unless ($seen->{$fa}++) {
163 index_diff_inc($tg, $fa, 'XDFN', $xnq);
173 sub index_diff ($$$) {
174 my ($tg, $lines, $doc) = @_;
180 if ($in_diff && s/^ //) { # diff context
181 index_diff_inc($tg, $_, 'XDFCTX', $xnq);
182 } elsif (/^-- $/) { # email signature begins
184 } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
185 my ($fa, $fb) = ($1, $2);
186 my $fn = (split('/', git_unquote($fa), 2))[1];
187 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
188 $fn = (split('/', git_unquote($fb), 2))[1];
189 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
192 } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
193 my ($opt, $fa, $fb) = ($1, $2, $3);
195 # only support unified:
196 next unless $opt =~ /[uU]/;
197 $in_diff = index_old_diff_fn($tg, \%seen, $fa, $fb,
199 } elsif (m!^--- ("?a/.+)!) {
200 my $fn = (split('/', git_unquote($1), 2))[1];
201 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
203 } elsif (m!^\+\+\+ ("?b/.+)!) {
204 my $fn = (split('/', git_unquote($1), 2))[1];
205 $seen{$fn}++ or index_diff_inc($tg, $fn, 'XDFN', $xnq);
207 } elsif (/^--- (\S+)/) {
210 } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
211 $in_diff = index_old_diff_fn($tg, \%seen, $in_diff, $1,
213 } elsif ($in_diff && s/^\+//) { # diff added
214 index_diff_inc($tg, $_, 'XDFB', $xnq);
215 } elsif ($in_diff && s/^-//) { # diff removed
216 index_diff_inc($tg, $_, 'XDFA', $xnq);
217 } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
218 my ($ba, $bb) = ($1, $2);
219 index_git_blob_id($doc, 'XDFPRE', $ba);
220 index_git_blob_id($doc, 'XDFPOST', $bb);
222 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
223 # traditional diff w/o -p
224 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
225 # hunk header context
226 index_diff_inc($tg, $1, 'XDFHH', $xnq);
227 # ignore the following lines:
228 } elsif (/^(?:dis)similarity index/ ||
229 /^(?:old|new) mode/ ||
230 /^(?:deleted|new) file mode/ ||
231 /^(?:copy|rename) (?:from|to) / ||
232 /^(?:dis)?similarity index / ||
233 /^\\ No newline at end of file/ ||
234 /^Binary files .* differ/) {
240 warn "non-diff line: $_\n" if DEBUG && $_ ne '';
245 $tg->index_text(join("\n", @xnq), 1, 'XNQ');
246 $tg->increase_termpos;
249 sub index_body ($$$) {
250 my ($tg, $lines, $doc) = @_;
251 my $txt = join("\n", @$lines);
253 # does it look like a diff?
254 if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
256 index_diff($tg, $lines, $doc);
258 $tg->index_text($txt, 1, 'XNQ');
261 $tg->index_text($txt, 0, 'XQUOT');
263 $tg->increase_termpos;
268 # mime = Email::MIME object
269 my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
271 my $mids = mids($mime->header_obj);
272 my $skel = $self->{skeleton};
275 my $smsg = PublicInbox::SearchMsg->new($mime);
276 my $doc = $smsg->{doc};
277 my $subj = $smsg->subject;
280 $xpath = $self->subject_path($subj);
281 $xpath = id_compress($xpath);
284 my $lines = $mime->body_raw =~ tr!\n!\n!;
285 $smsg->{lines} = $mime->body_raw =~ tr!\n!\n!;
286 defined $bytes or $bytes = length($mime->as_string);
287 $smsg->{bytes} = $bytes;
288 add_values($doc, $smsg->ts, $smsg->ds, $num);
290 my $tg = $self->term_generator;
292 $tg->set_document($doc);
293 $tg->index_text($subj, 1, 'S') if $subj;
294 $tg->increase_termpos;
296 index_users($tg, $smsg);
298 msg_iter($mime, sub {
299 my ($part, $depth, @idx) = @{$_[0]};
300 my $ct = $part->content_type || 'text/plain';
301 my $fn = $part->filename;
302 if (defined $fn && $fn ne '') {
303 $tg->index_text($fn, 1, 'XFN');
306 return if $ct =~ m!\btext/x?html\b!i;
308 my $s = eval { $part->body_str };
310 if ($ct =~ m!\btext/plain\b!i) {
311 # Try to assume UTF-8 because Alpine
312 # seems to do wacky things and set
314 $part->charset_set('UTF-8');
315 $s = eval { $part->body_str };
316 $s = $part->body if $@;
319 defined $s or return;
322 my $body = $part->body;
323 my @lines = split(/\n/, $body);
324 while (defined(my $l = shift @lines)) {
326 index_body($tg, \@orig, $doc) if @orig;
329 index_body($tg, \@quot, 0) if @quot;
333 index_body($tg, \@quot, 0) if @quot;
334 index_body($tg, \@orig, $doc) if @orig;
337 # populates smsg->references for smsg->to_doc_data
338 my $refs = parse_references($smsg);
339 $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility
340 my $data = $smsg->to_doc_data($oid, $mid0);
341 foreach my $mid (@$mids) {
342 $tg->index_text($mid, 1, 'XM');
344 $doc->set_data($data);
345 if (my $altid = $self->{-altid}) {
346 foreach my $alt (@$altid) {
347 my $pfx = $alt->{xprefix};
348 foreach my $mid (@$mids) {
349 my $id = $alt->mid2alt($mid);
350 next unless defined $id;
351 $doc->add_boolean_term($pfx . $id);
356 $self->delete_article($num) if defined $num; # for reindexing
358 my @vals = ($smsg->ts, $num, $mids, $xpath, $data);
359 $skel->index_skeleton(\@vals);
360 $doc->add_boolean_term('Q' . $_) foreach @$mids;
361 $doc->add_boolean_term('XNUM' . $num) if defined $num;
362 $doc_id = $self->{xdb}->add_document($doc);
364 $doc_id = link_and_save($self, $doc, $mids, $refs,
370 warn "failed to index message <".join('> <',@$mids).">: $@\n";
376 # returns begin and end PostingIterator
378 my ($self, $termval) = @_;
379 my $db = $self->{xdb};
381 ($db->postlist_begin($termval), $db->postlist_end($termval));
385 my ($self, $termval, $cb) = @_;
386 my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
388 my ($head, $tail) = $self->find_doc_ids($termval);
389 return if $head == $tail;
391 for (; $head != $tail && @ids < $batch_size; $head->inc) {
392 push @ids, $head->get_docid;
399 my ($self, $mid) = @_;
400 my $db = $self->{xdb};
402 $mid = mid_clean($mid);
405 batch_do($self, 'Q' . $mid, sub {
407 $db->delete_document($_) for @$ids;
412 warn "failed to remove message <$mid>: $@\n";
414 warn "cannot remove non-existent <$mid>\n";
419 my ($self, $num) = @_;
421 batch_do($self, 'XNUM' . $num, sub {
423 $ndel += scalar @$ids;
424 $self->{xdb}->delete_document($_) for @$ids;
428 # MID is a hint in V2
430 my ($self, $oid, $mid) = @_;
431 my $db = $self->{xdb};
433 # XXX careful, we cannot use batch_do here since we conditionally
434 # delete documents based on other factors, so we cannot call
435 # find_doc_ids twice.
436 my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
437 return if $head == $tail;
439 # there is only ONE element in @delete unless we
440 # have bugs in our v2writable deduplication check
442 for (; $head != $tail; $head->inc) {
443 my $docid = $head->get_docid;
444 my $doc = $db->get_document($docid);
445 my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
447 push(@delete, $docid) if $smsg->{blob} eq $oid;
449 $db->delete_document($_) foreach @delete;
453 sub term_generator { # write-only
456 my $tg = $self->{term_generator};
459 $tg = Search::Xapian::TermGenerator->new;
460 $tg->set_stemmer($self->stemmer);
462 $self->{term_generator} = $tg;
465 # increments last_thread_id counter
466 # returns a 64-bit integer represented as a decimal string
469 my $db = $self->{xdb};
470 my $last_thread_id = int($db->get_metadata('last_thread_id') || 0);
472 $db->set_metadata('last_thread_id', ++$last_thread_id);
477 sub parse_references ($) {
479 my $mime = $smsg->{mime};
480 my $hdr = $mime->header_obj;
481 my $refs = references($hdr);
482 return $refs if scalar(@$refs) == 0;
484 # prevent circular references via References here:
485 my %mids = map { $_ => 1 } @{mids($hdr)};
487 foreach my $ref (@$refs) {
488 if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
489 warn "References: <$ref> too long, ignoring\n";
495 $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
500 my ($self, $doc, $refs, $old_tid) = @_;
504 # first ref *should* be the thread root,
505 # but we can never trust clients to do the right thing
506 my $ref = shift @$refs;
507 $tid = resolve_mid_to_tid($self, $ref);
508 merge_threads($self, $tid, $old_tid) if defined $old_tid;
510 # the rest of the refs should point to this tid:
511 foreach $ref (@$refs) {
512 my $ptid = resolve_mid_to_tid($self, $ref);
513 merge_threads($self, $tid, $ptid);
516 $tid = defined $old_tid ? $old_tid : $self->next_thread_id;
518 $doc->add_boolean_term('G' . $tid);
523 my ($self, $doc, $mids, $refs, $num, $xpath) = @_;
524 my $db = $self->{xdb};
527 $doc->add_boolean_term('XNUM' . $num) if defined $num;
528 $doc->add_boolean_term('XPATH' . $xpath) if defined $xpath;
529 $doc->add_boolean_term('Q' . $_) foreach @$mids;
531 $self->{skel} and die "Should not have read-only skel here\n";;
532 foreach my $mid (@$mids) {
534 $self->each_smsg_by_mid($mid, sub {
536 my $type = $cur->type;
537 my $cur_tid = $cur->thread_id;
538 $old_tid = $cur_tid unless defined $old_tid;
539 if ($type eq 'mail') {
540 # do not break existing mail messages,
541 # just merge the threads
542 merge_threads($self, $old_tid, $cur_tid);
545 if ($type ne 'ghost') {
546 die "<$mid> has a bad type: $type\n";
548 my $tid = link_doc($self, $doc, $refs, $old_tid);
549 $old_tid = $tid unless defined $old_tid;
550 $doc_id = $cur->{doc_id};
551 $self->{xdb}->replace_document($doc_id, $doc);
555 $vivified > 1 and warn
556 "BUG: vivified multiple ($vivified) ghosts for $mid\n";
558 # not really important, but we return any vivified ghost docid, here:
559 return $doc_id if defined $doc_id;
560 link_doc($self, $doc, $refs, $old_tid);
561 $self->{xdb}->add_document($doc);
564 sub index_git_blob_id {
565 my ($doc, $pfx, $objid) = @_;
567 my $len = length($objid);
568 for (my $len = length($objid); $len >= 7; ) {
569 $doc->add_term($pfx.$objid);
570 $objid = substr($objid, 0, --$len);
575 my ($self, $mime) = @_;
576 my $mid = eval { mid_clean(mid_mime($mime)) };
577 $self->remove_message($mid) if defined $mid;
581 my ($self, $mime) = @_;
582 my $mid = mid_clean(mid_mime($mime));
583 my $mm = $self->{mm};
584 my $num = $mm->mid_insert($mid);
585 return $num if defined $num;
587 # fallback to num_for since filters like RubyLang set the number
592 my ($self, $mime) = @_;
593 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
597 my ($self, $mime, $bytes, $blob) = @_;
598 my $num = $self->{mm}->num_for(mid_clean(mid_mime($mime)));
599 add_message($self, $mime, $bytes, $num, $blob);
603 my ($self, $mime) = @_;
604 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
605 unindex_blob($self, $mime);
609 my ($self, $mime, $bytes, $blob) = @_;
610 my $num = index_mm($self, $mime);
611 add_message($self, $mime, $bytes, $num, $blob);
615 my ($self, $mime) = @_;
616 unindex_blob($self, $mime);
617 unindex_mm($self, $mime);
621 my ($git, $blob, $sizeref) = @_;
623 my $str = $git->cat_file($blob, $sizeref);
624 # fixup bugs from import:
625 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
626 PublicInbox::MIME->new($str);
632 my ($self, $opts) = @_;
633 $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
636 sub batch_adjust ($$$$) {
637 my ($max, $bytes, $batch_cb, $latest) = @_;
641 $batch_cb->($latest, 1);
647 my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
648 my $hex = '[a-f0-9]';
649 my $h40 = $hex .'{40}';
650 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
651 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
652 my $git = $self->{git};
655 my $max = BATCH_BYTES;
658 while (defined($line = <$log>)) {
659 if ($line =~ /$addmsg/o) {
661 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
662 batch_adjust(\$max, $bytes, $batch_cb, $latest);
663 $add_cb->($self, $mime, $bytes, $blob);
664 } elsif ($line =~ /$delmsg/o) {
666 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
667 batch_adjust(\$max, $bytes, $batch_cb, $latest);
668 $del_cb->($self, $mime);
669 } elsif ($line =~ /^commit ($h40)/o) {
673 $batch_cb->($latest, 0);
678 $self->{mm} ||= eval {
679 require PublicInbox::Msgmap;
680 my $msgmap_path = $self->{msgmap_path};
681 if (defined $msgmap_path) { # v2
682 PublicInbox::Msgmap->new_file($msgmap_path, 1);
684 PublicInbox::Msgmap->new($self->{mainrepo}, 1);
690 my ($self, $range) = @_;
691 $self->{git}->popen(qw/log --reverse --no-notes --no-color
692 --raw -r --no-abbrev/, $range);
695 # indexes all unindexed messages
697 my ($self, $opts) = @_;
698 my $tip = $opts->{ref} || 'HEAD';
699 my $reindex = $opts->{reindex};
700 my ($mkey, $last_commit, $lx, $xlog);
701 $self->{git}->batch_prepare;
702 my $xdb = _xdb_acquire($self);
703 $xdb->begin_transaction;
706 $mkey = 'last_commit';
707 $last_commit = $xdb->get_metadata('last_commit');
711 $mkey = undef if $last_commit ne '';
713 $xdb->cancel_transaction;
714 $xdb = _xdb_release($self);
716 # ensure we leak no FDs to "git log"
717 my $range = $lx eq '' ? $tip : "$lx..$tip";
718 $xlog = _git_log($self, $range);
720 $xdb = _xdb_acquire($self);
721 $xdb->begin_transaction;
722 } while ($xdb->get_metadata('last_commit') ne $last_commit);
724 my $mm = _msgmap_init($self);
725 my $dbh = $mm->{dbh} if $mm;
728 my ($commit, $more) = @_;
730 $mm->last_commit($commit) if $commit;
734 $xdb->set_metadata($mkey, $commit) if $mkey && $commit;
735 $xdb->commit_transaction;
736 $xdb = _xdb_release($self);
738 # let another process do some work... <
741 $xdb = _xdb_acquire($self);
742 $xdb->begin_transaction;
744 $dbh->begin_work if $dbh;
750 my $lm = $mm->last_commit || '';
752 # Common case is the indexes are synced,
753 # we only need to run git-log once:
754 rlog($self, $xlog, *index_both, *unindex_both, $cb);
756 # Uncommon case, msgmap and xapian are out-of-sync
757 # do not care for performance (but git is fast :>)
758 # This happens if we have to reindex Xapian since
759 # msgmap is a frozen format and our Xapian format
761 my $r = $lm eq '' ? $tip : "$lm..$tip";
763 # first, ensure msgmap is up-to-date:
764 my $mkey_prev = $mkey;
765 $mkey = undef; # ignore xapian, for now
766 my $mlog = _git_log($self, $r);
768 rlog($self, $mlog, *index_mm, *unindex_mm, $cb);
769 $mm_only = $mlog = undef;
771 # now deal with Xapian
774 rlog($self, $xlog, *index_mm2, *unindex_mm2, $cb);
777 # user didn't install DBD::SQLite and DBI
778 rlog($self, $xlog, *add_message, *unindex_blob, $cb);
782 # this will create a ghost as necessary
783 sub resolve_mid_to_tid {
784 my ($self, $mid) = @_;
786 $self->each_smsg_by_mid($mid, sub {
788 my $cur_tid = $smsg->thread_id;
790 merge_threads($self, $tid, $cur_tid);
792 $tid = $smsg->thread_id;
796 return $tid if defined $tid;
798 $self->create_ghost($mid)->thread_id;
802 my ($self, $mid) = @_;
804 my $tid = $self->next_thread_id;
805 my $doc = Search::Xapian::Document->new;
806 $doc->add_boolean_term('Q' . $mid);
807 $doc->add_boolean_term('G' . $tid);
808 $doc->add_boolean_term('T' . 'ghost');
810 my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
811 $self->{xdb}->add_document($doc);
817 my ($self, $winner_tid, $loser_tid) = @_;
818 return if $winner_tid == $loser_tid;
819 my $db = $self->{xdb};
820 batch_do($self, 'G' . $loser_tid, sub {
822 foreach my $docid (@$ids) {
823 my $doc = $db->get_document($docid);
824 $doc->remove_term('G' . $loser_tid);
825 $doc->add_boolean_term('G' . $winner_tid);
826 $db->replace_document($docid, $doc);
832 # order matters for unlocking
833 $_[0]->{xdb} = undef;
834 $_[0]->{lockfh} = undef;
837 # remote_* subs are only used by SearchIdxPart and SearchIdxSkeleton
840 if (my $w = $self->{w}) {
841 print $w "commit\n" or die "failed to write commit: $!";
843 $self->commit_txn_lazy;
844 if (my $skel = $self->{skeleton}) {
845 $skel->commit_txn_lazy;
852 if (my $w = delete $self->{w}) {
853 my $pid = delete $self->{pid} or die "no process to wait on\n";
854 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
855 close $w or die "failed to close pipe for pid:$pid: $!\n";
856 waitpid($pid, 0) == $pid or die "remote process did not finish";
857 $? == 0 or die ref($self)." pid:$pid exited with: $?";
859 die "transaction in progress $self\n" if $self->{txn};
860 $self->_xdb_release if $self->{xdb};
865 my ($self, $oid, $mid) = @_;
866 if (my $w = $self->{w}) {
867 # triggers remove_by_oid in partition or skeleton
868 print $w "D $oid $mid\n" or die "failed to write remove $!";
870 $self->begin_txn_lazy;
871 $self->remove_by_oid($oid, $mid);
877 return if $self->{txn};
878 my $xdb = $self->{xdb} || $self->_xdb_acquire;
879 $xdb->begin_transaction;
883 sub commit_txn_lazy {
885 delete $self->{txn} or return;
886 $self->{xdb}->commit_transaction;
891 die "$$ $0 xdb not released\n" if $self->{xdb};
892 die "$$ $0 still in transaction\n" if $self->{txn};