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 id_compress mid_mime mids/;
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);
24 BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
25 0x7fffffff : 1_000_000,
26 DEBUG => !!$ENV{DEBUG},
29 my $xapianlevels = qr/\A(?:full|medium)\z/;
32 my ($class, $ibx, $creat, $shard) = @_;
33 ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
34 my $levels = qr/\A(?:full|medium|basic)\z/;
35 my $mainrepo = $ibx->{mainrepo};
36 my $version = $ibx->{version} || 1;
37 my $indexlevel = 'full';
38 my $altid = $ibx->{altid};
40 require PublicInbox::AltId;
41 $altid = [ map { PublicInbox::AltId->new($ibx, $_); } @$altid ];
43 if ($ibx->{indexlevel}) {
44 if ($ibx->{indexlevel} =~ $levels) {
45 $indexlevel = $ibx->{indexlevel};
47 die("Invalid indexlevel $ibx->{indexlevel}\n");
50 $ibx = PublicInbox::InboxWritable->new($ibx);
52 mainrepo => $mainrepo,
57 indexlevel => $indexlevel,
61 $self->{lock_path} = "$mainrepo/ssoma.lock";
62 my $dir = $self->xdir;
63 $self->{over} = PublicInbox::OverIdx->new("$dir/over.sqlite3");
64 } elsif ($version == 2) {
65 defined $shard or die "shard is required for v2\n";
67 $self->{shard} = $shard;
68 $self->{lock_path} = undef;
70 die "unsupported inbox version=$version\n";
72 $self->{creat} = ($creat || 0) == 1;
76 sub need_xapian ($) { $_[0]->{indexlevel} =~ $xapianlevels }
80 if (need_xapian($self)) {
81 my $xdb = delete $self->{xdb} or croak 'not acquired';
84 $self->lock_release if $self->{creat};
91 my $dir = $self->xdir;
92 if (need_xapian($self)) {
93 croak 'already acquired' if $self->{xdb};
94 PublicInbox::Search::load_xapian();
95 require Search::Xapian::WritableDatabase;
96 $flag = $self->{creat} ?
97 Search::Xapian::DB_CREATE_OR_OPEN() :
98 Search::Xapian::DB_OPEN();
100 if ($self->{creat}) {
104 # don't create empty Xapian directories if we don't need Xapian
105 my $is_shard = defined($self->{shard});
106 if (!$is_shard || ($is_shard && need_xapian($self))) {
107 File::Path::mkpath($dir);
110 return unless defined $flag;
111 my $xdb = eval { Search::Xapian::WritableDatabase->new($dir, $flag) };
113 die "Failed opening $dir: ", $@;
119 my ($doc, $col, $num) = @_;
120 $num = Search::Xapian::sortable_serialise($num);
121 $doc->add_value($col, $num);
124 sub index_text ($$$$)
126 my ($self, $field, $n, $text) = @_;
127 my $tg = $self->term_generator;
129 if ($self->{indexlevel} eq 'full') {
130 $tg->index_text($field, $n, $text);
131 $tg->increase_termpos;
133 $tg->index_text_without_positions($field, $n, $text);
137 sub index_users ($$) {
138 my ($self, $smsg) = @_;
140 my $from = $smsg->from;
144 $self->index_text($from, 1, 'A'); # A - author
145 $self->index_text($to, 1, 'XTO') if $to ne '';
146 $self->index_text($cc, 1, 'XCC') if $cc ne '';
149 sub index_diff_inc ($$$$) {
150 my ($self, $text, $pfx, $xnq) = @_;
152 $self->index_text(join("\n", @$xnq), 1, 'XNQ');
155 $self->index_text($text, 1, $pfx);
158 sub index_old_diff_fn {
159 my ($self, $seen, $fa, $fb, $xnq) = @_;
161 # no renames or space support for traditional diffs,
162 # find the number of leading common paths to strip:
163 my @fa = split('/', $fa);
164 my @fb = split('/', $fb);
165 while (scalar(@fa) && scalar(@fb)) {
166 $fa = join('/', @fa);
167 $fb = join('/', @fb);
169 unless ($seen->{$fa}++) {
170 $self->index_diff_inc($fa, 'XDFN', $xnq);
180 sub index_diff ($$$) {
181 my ($self, $lines, $doc) = @_;
187 if ($in_diff && s/^ //) { # diff context
188 $self->index_diff_inc($_, 'XDFCTX', $xnq);
189 } elsif (/^-- $/) { # email signature begins
191 } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
192 my ($fa, $fb) = ($1, $2);
193 my $fn = (split('/', git_unquote($fa), 2))[1];
194 $seen{$fn}++ or $self->index_diff_inc($fn, 'XDFN', $xnq);
195 $fn = (split('/', git_unquote($fb), 2))[1];
196 $seen{$fn}++ or $self->index_diff_inc($fn, 'XDFN', $xnq);
199 } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
200 my ($opt, $fa, $fb) = ($1, $2, $3);
202 # only support unified:
203 next unless $opt =~ /[uU]/;
204 $in_diff = $self->index_old_diff_fn(\%seen, $fa, $fb,
206 } elsif (m!^--- ("?a/.+)!) {
207 my $fn = (split('/', git_unquote($1), 2))[1];
208 $seen{$fn}++ or $self->index_diff_inc($fn, 'XDFN', $xnq);
210 } elsif (m!^\+\+\+ ("?b/.+)!) {
211 my $fn = (split('/', git_unquote($1), 2))[1];
212 $seen{$fn}++ or $self->index_diff_inc($fn, 'XDFN', $xnq);
214 } elsif (/^--- (\S+)/) {
217 } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
218 $in_diff = $self->index_old_diff_fn(\%seen, $in_diff, $1,
220 } elsif ($in_diff && s/^\+//) { # diff added
221 $self->index_diff_inc($_, 'XDFB', $xnq);
222 } elsif ($in_diff && s/^-//) { # diff removed
223 $self->index_diff_inc($_, 'XDFA', $xnq);
224 } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
225 my ($ba, $bb) = ($1, $2);
226 index_git_blob_id($doc, 'XDFPRE', $ba);
227 index_git_blob_id($doc, 'XDFPOST', $bb);
229 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
230 # traditional diff w/o -p
231 } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
232 # hunk header context
233 $self->index_diff_inc($1, 'XDFHH', $xnq);
234 # ignore the following lines:
235 } elsif (/^(?:dis)similarity index/ ||
236 /^(?:old|new) mode/ ||
237 /^(?:deleted|new) file mode/ ||
238 /^(?:copy|rename) (?:from|to) / ||
239 /^(?:dis)?similarity index / ||
240 /^\\ No newline at end of file/ ||
241 /^Binary files .* differ/) {
247 warn "non-diff line: $_\n" if DEBUG && $_ ne '';
252 $self->index_text(join("\n", @xnq), 1, 'XNQ');
255 sub index_body ($$$) {
256 my ($self, $lines, $doc) = @_;
257 my $txt = join("\n", @$lines);
259 # does it look like a diff?
260 if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
262 $self->index_diff($lines, $doc);
264 $self->index_text($txt, 1, 'XNQ');
267 $self->index_text($txt, 0, 'XQUOT');
272 sub add_xapian ($$$$$) {
273 my ($self, $mime, $num, $oid, $mids, $mid0) = @_;
274 my $smsg = PublicInbox::SearchMsg->new($mime);
275 my $doc = Search::Xapian::Document->new;
276 my $subj = $smsg->subject;
277 add_val($doc, PublicInbox::Search::TS(), $smsg->ts);
278 my @ds = gmtime($smsg->ds);
279 my $yyyymmdd = strftime('%Y%m%d', @ds);
280 add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
281 my $dt = strftime('%Y%m%d%H%M%S', @ds);
282 add_val($doc, PublicInbox::Search::DT(), $dt);
284 my $tg = $self->term_generator;
286 $tg->set_document($doc);
287 $self->index_text($subj, 1, 'S') if $subj;
288 $self->index_users($smsg);
290 msg_iter($mime, sub {
291 my ($part, $depth, @idx) = @{$_[0]};
292 my $ct = $part->content_type || 'text/plain';
293 my $fn = $part->filename;
294 if (defined $fn && $fn ne '') {
295 $self->index_text($fn, 1, 'XFN');
298 my ($s, undef) = msg_part_text($part, $ct);
299 defined $s or return;
302 my @lines = split(/\n/, $s);
303 while (defined(my $l = shift @lines)) {
305 $self->index_body(\@orig, $doc) if @orig;
308 $self->index_body(\@quot, 0) if @quot;
312 $self->index_body(\@quot, 0) if @quot;
313 $self->index_body(\@orig, $doc) if @orig;
316 foreach my $mid (@$mids) {
317 $self->index_text($mid, 1, 'XM');
319 # because too many Message-IDs are prefixed with
321 if ($mid =~ /\w{12,}/) {
322 my @long = ($mid =~ /(\w{3,}+)/g);
323 $self->index_text(join(' ', @long), 1, 'XM');
326 $smsg->{to} = $smsg->{cc} = '';
327 PublicInbox::OverIdx::parse_references($smsg, $mid0, $mids);
328 my $data = $smsg->to_doc_data($oid, $mid0);
329 $doc->set_data($data);
330 if (my $altid = $self->{-altid}) {
331 foreach my $alt (@$altid) {
332 my $pfx = $alt->{xprefix};
333 foreach my $mid (@$mids) {
334 my $id = $alt->mid2alt($mid);
335 next unless defined $id;
336 $doc->add_boolean_term($pfx . $id);
340 $doc->add_boolean_term('Q' . $_) foreach @$mids;
341 $self->{xdb}->replace_document($num, $doc);
345 # mime = Email::MIME object
346 my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
347 my $mids = mids($mime->header_obj);
348 $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility
349 unless (defined $num) { # v1
351 $num = index_mm($self, $mime);
354 if (need_xapian($self)) {
355 $self->add_xapian($mime, $num, $oid, $mids, $mid0)
357 if (my $over = $self->{over}) {
358 $over->add_overview($mime, $bytes, $num, $oid, $mid0);
363 warn "failed to index message <".join('> <',@$mids).">: $@\n";
369 # returns begin and end PostingIterator
371 my ($self, $termval) = @_;
372 my $db = $self->{xdb};
374 ($db->postlist_begin($termval), $db->postlist_end($termval));
379 my ($self, $termval, $cb) = @_;
380 my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
382 my ($head, $tail) = $self->find_doc_ids($termval);
383 return if $head == $tail;
385 for (; $head != $tail && @ids < $batch_size; $head->inc) {
386 push @ids, $head->get_docid;
392 # v1 only, where $mid is unique
394 my ($self, $mid) = @_;
395 $mid = mid_clean($mid);
397 if (my $over = $self->{over}) {
398 my $nr = eval { $over->remove_oid(undef, $mid) };
400 warn "failed to remove <$mid> from overview: $@\n";
402 warn "<$mid> missing for removal from overview\n";
405 return unless need_xapian($self);
406 my $db = $self->{xdb};
409 batch_do($self, 'Q' . $mid, sub {
411 $db->delete_document($_) for @$ids;
416 warn "failed to remove <$mid> from Xapian: $@\n";
418 warn "<$mid> missing for removal from Xapian\n";
422 # MID is a hint in V2
424 my ($self, $oid, $mid) = @_;
426 $self->{over}->remove_oid($oid, $mid) if $self->{over};
428 return unless need_xapian($self);
429 my $db = $self->{xdb};
431 # XXX careful, we cannot use batch_do here since we conditionally
432 # delete documents based on other factors, so we cannot call
433 # find_doc_ids twice.
434 my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
435 return if $head == $tail;
437 # there is only ONE element in @delete unless we
438 # have bugs in our v2writable deduplication check
440 for (; $head != $tail; $head->inc) {
441 my $docid = $head->get_docid;
442 my $doc = $db->get_document($docid);
443 my $smsg = PublicInbox::SearchMsg->wrap($mid);
444 $smsg->load_expand($doc);
445 if ($smsg->{blob} eq $oid) {
446 push(@delete, $docid);
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 sub index_git_blob_id {
466 my ($doc, $pfx, $objid) = @_;
468 my $len = length($objid);
469 for (my $len = length($objid); $len >= 7; ) {
470 $doc->add_term($pfx.$objid);
471 $objid = substr($objid, 0, --$len);
476 my ($self, $mime) = @_;
477 my $mid = eval { mid_clean(mid_mime($mime)) };
478 $self->remove_message($mid) if defined $mid;
482 my ($self, $mime) = @_;
483 my $mid = mid_clean(mid_mime($mime));
484 my $mm = $self->{mm};
487 if (defined $self->{regen_down}) {
488 $num = $mm->num_for($mid) and return $num;
490 while (($num = $self->{regen_down}--) > 0) {
491 if ($mm->mid_set($num, $mid) != 0) {
495 } elsif (defined $self->{regen_up}) {
496 $num = $mm->num_for($mid) and return $num;
498 # this is to fixup old bugs due to add-remove-add
499 while (($num = ++$self->{regen_up})) {
500 if ($mm->mid_set($num, $mid) != 0) {
506 $num = $mm->mid_insert($mid) and return $num;
508 # fallback to num_for since filters like RubyLang set the number
513 my ($self, $mime) = @_;
514 $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
518 my ($self, $mime, $bytes, $blob) = @_;
519 my $num = index_mm($self, $mime);
520 add_message($self, $mime, $bytes, $num, $blob);
524 my ($self, $mime) = @_;
525 unindex_blob($self, $mime);
526 unindex_mm($self, $mime);
530 my ($git, $blob, $sizeref) = @_;
532 my $str = $git->cat_file($blob, $sizeref);
533 # fixup bugs from import:
534 $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
535 PublicInbox::MIME->new($str);
540 # called by public-inbox-index
542 my ($self, $opts) = @_;
543 delete $self->{lock_path} if $opts->{-skip_lock};
544 $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
547 sub batch_adjust ($$$$$) {
548 my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
552 $batch_cb->($nr, $latest);
558 my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
559 my $hex = '[a-f0-9]';
560 my $h40 = $hex .'{40}';
561 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
562 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
563 my $git = $self->{git};
566 my $max = BATCH_BYTES;
572 while (defined($line = <$log>)) {
573 if ($line =~ /$addmsg/o) {
575 if (delete $D{$blob}) {
576 if (defined $self->{regen_down}) {
577 my $num = $self->{regen_down}--;
578 $self->{mm}->num_highwater($num);
582 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
583 batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
584 $add_cb->($self, $mime, $bytes, $blob);
585 } elsif ($line =~ /$delmsg/o) {
588 } elsif ($line =~ /^commit ($h40)/o) {
594 foreach my $blob (keys %D) {
595 my $mime = do_cat_mail($git, $blob, \$bytes) or next;
596 $del_cb->($self, $mime);
598 $batch_cb->($nr, $latest, $newest);
603 die "BUG: _msgmap_init is only for v1\n" if $self->{version} != 1;
604 $self->{mm} ||= eval {
605 require PublicInbox::Msgmap;
606 PublicInbox::Msgmap->new($self->{mainrepo}, 1);
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>;
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 defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
666 waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
670 sub need_update ($$$) {
671 my ($self, $cur, $new) = @_;
672 my $git = $self->{git};
673 return 1 if $cur && !is_ancestor($git, $cur, $new);
674 my $range = $cur eq '' ? $new : "$cur..$new";
675 chomp(my $n = $git->qx(qw(rev-list --count), $range));
676 ($n eq '' || $n > 0);
679 # The last git commit we indexed with Xapian or SQLite (msgmap)
680 # This needs to account for cases where Xapian or SQLite is
681 # out-of-date with respect to the other.
683 my ($self, $mm) = @_;
684 my $lm = $mm->last_commit || '';
686 if (need_xapian($self)) {
687 $lx = $self->{xdb}->get_metadata('last_commit') || '';
691 # Use last_commit from msgmap if it is older or unset
692 if (!$lm || ($lx && $lm && is_ancestor($self->{git}, $lm, $lx))) {
698 sub reindex_from ($$) {
699 my ($reindex, $last_commit) = @_;
700 return $last_commit unless $reindex;
701 ref($reindex) eq 'HASH' ? $reindex->{from} : '';
704 # indexes all unindexed messages (v1 only)
706 my ($self, $opts) = @_;
707 my $tip = $opts->{ref} || 'HEAD';
708 my ($last_commit, $lx, $xlog);
709 my $git = $self->{git};
711 my $pr = $opts->{-progress};
713 my $xdb = $self->begin_txn_lazy;
714 my $mm = _msgmap_init($self);
717 $last_commit = _last_x_commit($self, $mm);
718 $lx = reindex_from($opts->{reindex}, $last_commit);
720 $self->{over}->rollback_lazy;
721 $self->{over}->disconnect;
724 $xdb->cancel_transaction if $xdb;
725 $xdb = _xdb_release($self);
727 # ensure we leak no FDs to "git log" with Xapian <= 1.2
728 my $range = $lx eq '' ? $tip : "$lx..$tip";
729 $xlog = _git_log($self, $opts, $range);
731 $xdb = $self->begin_txn_lazy;
732 } while (_last_x_commit($self, $mm) ne $last_commit);
734 my $dbh = $mm->{dbh} if $mm;
736 my ($nr, $commit, $newest) = @_;
739 my $cur = $mm->last_commit || '';
740 if (need_update($self, $cur, $newest)) {
741 $mm->last_commit($newest);
746 if ($newest && need_xapian($self)) {
747 my $cur = $xdb->get_metadata('last_commit');
748 if (need_update($self, $cur, $newest)) {
749 $xdb->set_metadata('last_commit', $newest);
752 $self->commit_txn_lazy;
754 $xdb = _xdb_release($self);
755 # let another process do some work... <
756 $pr->("indexed $nr/$self->{ntodo}\n") if $pr && $nr;
758 $xdb = $self->begin_txn_lazy;
759 $dbh->begin_work if $dbh;
764 read_log($self, $xlog, *index_both, *unindex_both, $cb);
768 # order matters for unlocking
769 $_[0]->{xdb} = undef;
770 $_[0]->{lockfh} = undef;
773 # remote_* subs are only used by SearchIdxPart
776 if (my $w = $self->{w}) {
777 print $w "commit\n" or die "failed to write commit: $!";
779 $self->commit_txn_lazy;
785 if (my $w = delete $self->{w}) {
786 my $pid = delete $self->{pid} or die "no process to wait on\n";
787 print $w "close\n" or die "failed to write to pid:$pid: $!\n";
788 close $w or die "failed to close pipe for pid:$pid: $!\n";
789 waitpid($pid, 0) == $pid or die "remote process did not finish";
790 $? == 0 or die ref($self)." pid:$pid exited with: $?";
792 die "transaction in progress $self\n" if $self->{txn};
793 $self->_xdb_release if $self->{xdb};
798 my ($self, $oid, $mid) = @_;
799 if (my $w = $self->{w}) {
800 # triggers remove_by_oid in a shard
801 print $w "D $oid $mid\n" or die "failed to write remove $!";
803 $self->begin_txn_lazy;
804 $self->remove_by_oid($oid, $mid);
810 return if $self->{txn};
812 $self->{-inbox}->with_umask(sub {
813 my $xdb = $self->{xdb} || $self->_xdb_acquire;
814 $self->{over}->begin_lazy if $self->{over};
815 $xdb->begin_transaction if $xdb;
821 sub commit_txn_lazy {
823 delete $self->{txn} or return;
824 $self->{-inbox}->with_umask(sub {
825 if (my $xdb = $self->{xdb}) {
827 # store 'indexlevel=medium' in v2 shard=0 and
828 # v1 (only one shard)
829 # This metadata is read by Admin::detect_indexlevel:
830 if (!$self->{shard} # undef or 0, not >0
831 && $self->{indexlevel} eq 'medium') {
832 $xdb->set_metadata('indexlevel', 'medium');
835 $xdb->commit_transaction;
837 $self->{over}->commit_lazy if $self->{over};
843 if (need_xapian($self)) {
844 die "$$ $0 xdb not released\n" if $self->{xdb};
846 die "$$ $0 still in transaction\n" if $self->{txn};