1 # Copyright (C) 2017-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use bytes (); # only for bytes::length
7 use File::Temp qw/tempdir/;
8 use PublicInbox::MID qw(mids);
10 my @mods = qw(DBI DBD::SQLite Search::Xapian);
11 foreach my $mod (@mods) {
13 plan skip_all => "missing $mod for $0" if $@;
15 require PublicInbox::SearchIdx;
16 require PublicInbox::Inbox;
17 my $tmpdir = tempdir('pi-search-thr-index.XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $git_dir = "$tmpdir/a.git";
20 is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
21 my $ibx = PublicInbox::Inbox->new({inboxdir => $git_dir});
22 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
23 ok($rw, "search indexer created");
41 # nb. using internal API, fragile!
42 my $xdb = $rw->begin_txn_lazy;
45 foreach (reverse split(/\n\n/, $data)) {
47 my $mime = Email::MIME->new(\$_);
48 $mime->header_set('From' => 'bw@g');
49 $mime->header_set('To' => 'git@vger.kernel.org');
50 my $bytes = bytes::length($mime->as_string);
51 my $mid = mids($mime->header_obj)->[0];
52 my $doc_id = $rw->add_message($mime, $bytes, ++$num, 'ignored', $mid);
54 ok($doc_id, 'message added: '. $mid);
59 my $dbh = $rw->{over}->connect;
60 foreach my $mid (@mids) {
61 my $msgs = $rw->{over}->get_thread($mid);
62 is(3, scalar(@$msgs), "got all messages from $mid");
63 foreach my $m (@$msgs) {
64 my $tid = $dbh->selectrow_array(<<'', undef, $m->{num});
65 SELECT tid FROM over WHERE num = ? LIMIT 1
71 is(scalar keys %tids, 1, 'all messages have the same tid');
75 $xdb = $rw->begin_txn_lazy;
77 my $mime = Email::MIME->new(<<'');
81 To: git@vger.kernel.org
83 my $dbh = $rw->{over}->connect;
85 my $reidx = $rw->{over}->next_by_mid('1-bw@g', \$id, \$prev);
87 my $num = $reidx->{num};
88 my $tid0 = $dbh->selectrow_array(<<'', undef, $num);
89 SELECT tid FROM over WHERE num = ? LIMIT 1
91 my $bytes = bytes::length($mime->as_string);
92 my $mid = mids($mime->header_obj)->[0];
93 my $doc_id = $rw->add_message($mime, $bytes, $num, 'ignored', $mid);
94 ok($doc_id, 'message reindexed'. $mid);
95 is($doc_id, $num, "article number unchanged: $num");
97 my $tid1 = $dbh->selectrow_array(<<'', undef, $num);
98 SELECT tid FROM over WHERE num = ? LIMIT 1
100 is($tid1, $tid0, 'tid unchanged on reindex');
103 $rw->commit_txn_lazy;