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 v1reindex.t" if $@;
15 use_ok 'PublicInbox::SearchIdx';
16 use_ok 'PublicInbox::Import';
17 my $mainrepo = tempdir('pi-v1reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 is(system(qw(git init -q --bare), $mainrepo), 0);
20 mainrepo => $mainrepo,
21 name => 'test-v1reindex',
22 -primary_address => 'test@example.com',
25 my $mime = PublicInbox::MIME->create(
27 From => 'a@example.com',
28 To => 'test@example.com',
29 Subject => 'this is a subject',
30 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
32 body => "hello world\n",
37 my %config = %$ibx_config;
38 my $ibx = PublicInbox::Inbox->new(\%config);
39 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
40 foreach my $i (1..10) {
41 $mime->header_set('Message-Id', "<$i\@example.com>");
42 ok($im->add($mime), "message $i added");
48 if ('test remove later') {
49 $mime->header_set('Message-Id', "<5\@example.com>");
54 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
55 eval { $rw->index_sync() };
56 is($@, '', 'no error from indexing');
58 $minmax = [ $ibx->mm->minmax ];
59 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
60 is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
62 my ($min, $max) = @$minmax;
63 $msgmap = $ibx->mm->msg_range(\$min, $max);
65 [1, '1@example.com' ],
66 [2, '2@example.com' ],
67 [3, '3@example.com' ],
68 [6, '6@example.com' ],
69 [7, '7@example.com' ],
70 [8, '8@example.com' ],
71 [9, '9@example.com' ],
72 [10, '10@example.com' ],
73 ], 'msgmap as expected');
77 my %config = %$ibx_config;
78 my $ibx = PublicInbox::Inbox->new(\%config);
79 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
80 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
81 eval { $rw->index_sync({reindex => 1}) };
82 is($@, '', 'no error from reindexing');
85 my ($min, $max) = $ibx->mm->minmax;
86 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
89 my $xap = "$mainrepo/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
91 ok(!-d $xap, 'Xapian directories removed');
93 my %config = %$ibx_config;
94 my $ibx = PublicInbox::Inbox->new(\%config);
95 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
96 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
98 eval { $rw->index_sync({reindex => 1}) };
99 is($@, '', 'no error from reindexing');
101 ok(-d $xap, 'Xapian directories recreated');
104 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
106 my ($min, $max) = $ibx->mm->minmax;
107 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
110 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
112 ok(!-d $xap, 'Xapian directories removed again');
115 local $SIG{__WARN__} = sub { push @warn, @_ };
116 my %config = %$ibx_config;
117 my $ibx = PublicInbox::Inbox->new(\%config);
118 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
119 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
120 eval { $rw->index_sync({reindex => 1}) };
121 is($@, '', 'no error from reindexing without msgmap');
122 is(scalar(@warn), 0, 'no warnings from reindexing');
124 ok(-d $xap, 'Xapian directories recreated');
126 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
128 my ($min, $max) = $ibx->mm->minmax;
129 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
132 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
134 ok(!-d $xap, 'Xapian directories removed again');
137 local $SIG{__WARN__} = sub { push @warn, @_ };
138 my %config = %$ibx_config;
139 my $ibx = PublicInbox::Inbox->new(\%config);
140 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
141 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
142 eval { $rw->index_sync({reindex => 1}) };
143 is($@, '', 'no error from reindexing without msgmap');
144 is_deeply(\@warn, [], 'no warnings');
146 ok(-d $xap, 'Xapian directories recreated');
148 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
150 my ($min, $max) = @$minmax;
151 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
154 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
156 ok(!-d $xap, 'Xapian directories removed again');
159 local $SIG{__WARN__} = sub { push @warn, @_ };
160 my %config = %$ibx_config;
161 $config{indexlevel} = 'medium';
162 my $ibx = PublicInbox::Inbox->new(\%config);
163 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
164 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
165 eval { $rw->index_sync({reindex => 1}) };
166 is($@, '', 'no error from reindexing without msgmap');
167 is_deeply(\@warn, [], 'no warnings');
169 ok(-d $xap, 'Xapian directories recreated');
171 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
172 my $mset = $ibx->search->query('hello world', {mset=>1});
173 isnt($mset->size, 0, 'got Xapian search results');
175 my ($min, $max) = $ibx->mm->minmax;
176 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
179 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
181 ok(!-d $xap, 'Xapian directories removed again');
184 local $SIG{__WARN__} = sub { push @warn, @_ };
185 my %config = %$ibx_config;
186 $config{indexlevel} = 'basic';
187 my $ibx = PublicInbox::Inbox->new(\%config);
188 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
189 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
190 eval { $rw->index_sync({reindex => 1}) };
191 is($@, '', 'no error from reindexing without msgmap');
192 is_deeply(\@warn, [], 'no warnings');
194 ok(-d $xap, 'Xapian directories recreated');
196 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
197 my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
198 is($mset->size, 0, "no Xapian search results");
200 my ($min, $max) = $ibx->mm->minmax;
201 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
204 # upgrade existing basic to medium
205 # note: changing indexlevels is not yet supported in v2,
206 # and may not be without more effort
210 local $SIG{__WARN__} = sub { push @warn, @_ };
211 my %config = %$ibx_config;
212 $config{indexleve} = 'medium';
213 my $ibx = PublicInbox::Inbox->new(\%config);
214 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
215 eval { $rw->index_sync };
216 is($@, '', 'no error from indexing');
217 is_deeply(\@warn, [], 'no warnings');
218 my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
219 isnt($mset->size, 0, 'search OK after basic -> medium');
221 my ($min, $max) = $ibx->mm->minmax;
222 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');