1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 use File::Path qw(remove_tree);
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13 plan skip_all => "$mod missing for v2reindex.t" if $@;
15 use_ok 'PublicInbox::V2Writable';
16 my $mainrepo = tempdir('pi-v2reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 mainrepo => $mainrepo,
19 name => 'test-v2writable',
21 -primary_address => 'test@example.com',
24 my $mime = PublicInbox::MIME->create(
26 From => 'a@example.com',
27 To => 'test@example.com',
28 Subject => 'this is a subject',
29 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
31 body => "hello world\n",
33 local $ENV{NPROC} = 2;
36 my %config = %$ibx_config;
37 my $ibx = PublicInbox::Inbox->new(\%config);
38 my $im = PublicInbox::V2Writable->new($ibx, 1);
39 foreach my $i (1..10) {
40 $mime->header_set('Message-Id', "<$i\@example.com>");
41 ok($im->add($mime), "message $i added");
47 if ('test remove later') {
48 $mime->header_set('Message-Id', "<5\@example.com>");
53 $minmax = [ $ibx->mm->minmax ];
54 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
55 is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
59 my %config = %$ibx_config;
60 my $ibx = PublicInbox::Inbox->new(\%config);
61 my $im = PublicInbox::V2Writable->new($ibx, 1);
62 eval { $im->index_sync({reindex => 1}) };
63 is($@, '', 'no error from reindexing');
67 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
70 my $xap = "$mainrepo/xap".PublicInbox::Search::SCHEMA_VERSION();
72 ok(!-d $xap, 'Xapian directories removed');
74 my %config = %$ibx_config;
75 my $ibx = PublicInbox::Inbox->new(\%config);
76 my $im = PublicInbox::V2Writable->new($ibx, 1);
77 eval { $im->index_sync({reindex => 1}) };
78 is($@, '', 'no error from reindexing');
80 ok(-d $xap, 'Xapian directories recreated');
83 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
86 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
88 ok(!-d $xap, 'Xapian directories removed again');
91 local $SIG{__WARN__} = sub { push @warn, @_ };
92 my %config = %$ibx_config;
93 my $ibx = PublicInbox::Inbox->new(\%config);
94 my $im = PublicInbox::V2Writable->new($ibx, 1);
95 eval { $im->index_sync({reindex => 1}) };
96 is($@, '', 'no error from reindexing without msgmap');
97 is(scalar(@warn), 0, 'no warnings from reindexing');
99 ok(-d $xap, 'Xapian directories recreated');
101 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
105 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
107 ok(!-d $xap, 'Xapian directories removed again');
110 local $SIG{__WARN__} = sub { push @warn, @_ };
111 my %config = %$ibx_config;
112 my $ibx = PublicInbox::Inbox->new(\%config);
113 my $im = PublicInbox::V2Writable->new($ibx, 1);
114 eval { $im->index_sync({reindex => 1}) };
115 is($@, '', 'no error from reindexing without msgmap');
116 is_deeply(\@warn, [], 'no warnings');
118 ok(-d $xap, 'Xapian directories recreated');
120 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
121 my $mset = $ibx->search->query('"hello world"', {mset=>1});
122 isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
123 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
126 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
128 ok(!-d $xap, 'Xapian directories removed again');
131 local $SIG{__WARN__} = sub { push @warn, @_ };
132 my %config = %$ibx_config;
133 $config{indexlevel} = 'medium';
134 my $ibx = PublicInbox::Inbox->new(\%config);
135 my $im = PublicInbox::V2Writable->new($ibx);
136 eval { $im->index_sync({reindex => 1}) };
137 is($@, '', 'no error from reindexing without msgmap');
138 is_deeply(\@warn, [], 'no warnings');
140 ok(-d $xap, 'Xapian directories recreated');
142 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
145 # not sure why, but Xapian seems to fallback to terms and
146 # phrase searches still work
147 delete $ibx->{search};
148 my $mset = $ibx->search->query('"hello world"', {mset=>1});
149 is($mset->size, 0, 'phrase search does not work on medium');
152 my $mset = $ibx->search->query('hello world', {mset=>1});
153 isnt($mset->size, 0, "normal search works on indexlevel=medium");
154 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
155 ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
158 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
160 ok(!-d $xap, 'Xapian directories removed again');
163 local $SIG{__WARN__} = sub { push @warn, @_ };
164 my %config = %$ibx_config;
165 $config{indexlevel} = 'basic';
166 my $ibx = PublicInbox::Inbox->new(\%config);
167 my $im = PublicInbox::V2Writable->new($ibx);
168 eval { $im->index_sync({reindex => 1}) };
169 is($@, '', 'no error from reindexing without msgmap');
170 is_deeply(\@warn, [], 'no warnings');
172 ok(-d $xap, 'Xapian directories recreated');
174 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
175 my $mset = $ibx->search->query('hello', {mset=>1});
176 is($mset->size, 0, "search fails on indexlevel='basic'");
177 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
178 ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');