1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::ContentHash qw(content_digest);
7 use File::Path qw(remove_tree);
8 use PublicInbox::TestCommon;
11 require_mods(qw(DBD::SQLite Search::Xapian));
12 use_ok 'PublicInbox::SearchIdx';
13 use_ok 'PublicInbox::Import';
14 my ($inboxdir, $for_destroy) = tmpdir();
16 inboxdir => $inboxdir,
17 name => 'test-v1reindex',
18 -primary_address => 'test@example.com',
21 my $mime = PublicInbox::Eml->new(<<'EOF');
24 Subject: this is a subject
25 Date: Fri, 02 Oct 1993 00:00:00 +0000
31 my ($mark1, $mark2, $mark3, $mark4);
33 my %config = %$ibx_config;
34 my $ibx = PublicInbox::Inbox->new(\%config);
35 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
37 foreach my $i (1..10) {
38 $mime->header_set('Message-Id', "<$i\@example.com>");
39 ok($im->add($mime), "message $i added");
41 $mark1 = $im->get_mark($im->{tip});
43 $mark2 = $im->get_mark($im->{tip});
47 if ('test remove later') {
48 $mark3 = $im->get_mark($im->{tip});
49 $mime->header_set('Message-Id', "<5\@example.com>");
51 $mark4 = $im->get_mark($im->{tip});
55 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
56 eval { $rw->index_sync() };
57 is($@, '', 'no error from indexing');
59 $minmax = [ $ibx->mm->minmax ];
60 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
61 is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
62 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
64 my ($min, $max) = @$minmax;
65 $msgmap = $ibx->mm->msg_range(\$min, $max);
67 [1, '1@example.com' ],
68 [2, '2@example.com' ],
69 [3, '3@example.com' ],
70 [6, '6@example.com' ],
71 [7, '7@example.com' ],
72 [8, '8@example.com' ],
73 [9, '9@example.com' ],
74 [10, '10@example.com' ],
75 ], 'msgmap as expected');
79 my %config = %$ibx_config;
80 my $ibx = PublicInbox::Inbox->new(\%config);
81 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
82 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
83 eval { $rw->index_sync({reindex => 1}) };
84 is($@, '', 'no error from reindexing');
87 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
89 my ($min, $max) = $ibx->mm->minmax;
90 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
93 my $xap = "$inboxdir/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
95 ok(!-d $xap, 'Xapian directories removed');
97 my %config = %$ibx_config;
98 my $ibx = PublicInbox::Inbox->new(\%config);
99 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
100 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
102 eval { $rw->index_sync({reindex => 1}) };
103 is($@, '', 'no error from reindexing');
105 ok(-d $xap, 'Xapian directories recreated');
108 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
109 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
111 my ($min, $max) = $ibx->mm->minmax;
112 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
115 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
117 ok(!-d $xap, 'Xapian directories removed again');
120 local $SIG{__WARN__} = sub { push @warn, @_ };
121 my %config = %$ibx_config;
122 my $ibx = PublicInbox::Inbox->new(\%config);
123 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
124 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
125 eval { $rw->index_sync({reindex => 1}) };
126 is($@, '', 'no error from reindexing without msgmap');
127 is(scalar(@warn), 0, 'no warnings from reindexing');
129 ok(-d $xap, 'Xapian directories recreated');
131 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
132 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
134 my ($min, $max) = $ibx->mm->minmax;
135 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
138 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
140 ok(!-d $xap, 'Xapian directories removed again');
143 local $SIG{__WARN__} = sub { push @warn, @_ };
144 my %config = %$ibx_config;
145 my $ibx = PublicInbox::Inbox->new(\%config);
146 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
147 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
148 eval { $rw->index_sync({reindex => 1}) };
149 is($@, '', 'no error from reindexing without msgmap');
150 is_deeply(\@warn, [], 'no warnings');
152 ok(-d $xap, 'Xapian directories recreated');
154 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
155 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
157 my ($min, $max) = @$minmax;
158 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
161 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
163 ok(!-d $xap, 'Xapian directories removed again');
166 local $SIG{__WARN__} = sub { push @warn, @_ };
167 my %config = %$ibx_config;
168 $config{indexlevel} = 'medium';
169 my $ibx = PublicInbox::Inbox->new(\%config);
170 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
171 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
172 eval { $rw->index_sync({reindex => 1}) };
173 is($@, '', 'no error from reindexing without msgmap');
174 is_deeply(\@warn, [], 'no warnings');
176 ok(-d $xap, 'Xapian directories recreated');
178 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
179 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
180 my $mset = $ibx->search->query('hello world', {mset=>1});
181 isnt($mset->size, 0, 'got Xapian search results');
183 my ($min, $max) = $ibx->mm->minmax;
184 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
187 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
189 ok(!-d $xap, 'Xapian directories removed again');
192 local $SIG{__WARN__} = sub { push @warn, @_ };
193 my %config = %$ibx_config;
194 $config{indexlevel} = 'basic';
195 my $ibx = PublicInbox::Inbox->new(\%config);
196 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
197 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
198 eval { $rw->index_sync({reindex => 1}) };
199 is($@, '', 'no error from reindexing without msgmap');
200 is_deeply(\@warn, [], 'no warnings');
202 ok(-d $xap, 'Xapian directories recreated');
204 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
205 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
206 isnt($ibx->search, 'no search for basic');
208 my ($min, $max) = $ibx->mm->minmax;
209 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
212 # upgrade existing basic to medium
213 # note: changing indexlevels is not yet supported in v2,
214 # and may not be without more effort
218 local $SIG{__WARN__} = sub { push @warn, @_ };
219 my %config = %$ibx_config;
220 $config{indexlevel} = 'medium';
221 my $ibx = PublicInbox::Inbox->new(\%config);
222 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
223 eval { $rw->index_sync };
224 is($@, '', 'no error from indexing');
225 is_deeply(\@warn, [], 'no warnings');
226 my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
227 isnt($mset->size, 0, 'search OK after basic -> medium');
229 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
231 my ($min, $max) = $ibx->mm->minmax;
232 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
235 # An incremental indexing test
236 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
238 ok(!-d $xap, 'Xapian directories removed again');
241 local $SIG{__WARN__} = sub { push @warn, @_ };
242 my %config = %$ibx_config;
243 my $ibx = PublicInbox::Inbox->new(\%config);
244 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
245 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
246 # mark1 4 simple additions in the same index_sync
247 eval { $rw->index_sync({ref => $mark1}) };
248 is($@, '', 'no error from reindexing without msgmap');
249 is_deeply(\@warn, [], 'no warnings');
251 my ($min, $max) = $ibx->mm->minmax;
252 is($min, 1, 'min as expected');
253 is($max, 4, 'max as expected');
254 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
255 is_deeply($ibx->mm->msg_range(\$min, $max),
257 [1, '1@example.com' ],
258 [2, '2@example.com' ],
259 [3, '3@example.com' ],
260 [4, '4@example.com' ],
261 ], 'msgmap as expected' );
265 local $SIG{__WARN__} = sub { push @warn, @_ };
266 my %config = %$ibx_config;
267 my $ibx = PublicInbox::Inbox->new(\%config);
268 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
269 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
270 # mark2 A delete separated form and add in the same index_sync
271 eval { $rw->index_sync({ref => $mark2}) };
272 is($@, '', 'no error from reindexing without msgmap');
273 is_deeply(\@warn, [], 'no warnings');
275 my ($min, $max) = $ibx->mm->minmax;
276 is($min, 1, 'min as expected');
277 is($max, 3, 'max as expected');
278 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
279 is_deeply($ibx->mm->msg_range(\$min, $max),
281 [1, '1@example.com' ],
282 [2, '2@example.com' ],
283 [3, '3@example.com' ],
284 ], 'msgmap as expected' );
288 local $SIG{__WARN__} = sub { push @warn, @_ };
289 my %config = %$ibx_config;
290 my $ibx = PublicInbox::Inbox->new(\%config);
291 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
292 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
293 # mark3 adds following the delete at mark2
294 eval { $rw->index_sync({ref => $mark3}) };
295 is($@, '', 'no error from reindexing without msgmap');
296 is_deeply(\@warn, [], 'no warnings');
298 my ($min, $max) = $ibx->mm->minmax;
299 is($min, 1, 'min as expected');
300 is($max, 10, 'max as expected');
301 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
302 is_deeply($ibx->mm->msg_range(\$min, $max),
304 [1, '1@example.com' ],
305 [2, '2@example.com' ],
306 [3, '3@example.com' ],
307 [5, '5@example.com' ],
308 [6, '6@example.com' ],
309 [7, '7@example.com' ],
310 [8, '8@example.com' ],
311 [9, '9@example.com' ],
312 [10, '10@example.com' ],
313 ], 'msgmap as expected' );
317 local $SIG{__WARN__} = sub { push @warn, @_ };
318 my %config = %$ibx_config;
319 my $ibx = PublicInbox::Inbox->new(\%config);
320 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
321 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
322 # mark4 A delete of an older message
323 eval { $rw->index_sync({ref => $mark4}) };
324 is($@, '', 'no error from reindexing without msgmap');
325 is_deeply(\@warn, [], 'no warnings');
327 my ($min, $max) = $ibx->mm->minmax;
328 is($min, 1, 'min as expected');
329 is($max, 10, 'max as expected');
330 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
331 is_deeply($ibx->mm->msg_range(\$min, $max),
333 [1, '1@example.com' ],
334 [2, '2@example.com' ],
335 [3, '3@example.com' ],
336 [6, '6@example.com' ],
337 [7, '7@example.com' ],
338 [8, '8@example.com' ],
339 [9, '9@example.com' ],
340 [10, '10@example.com' ],
341 ], 'msgmap as expected' );
345 # Another incremental indexing test
346 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
348 ok(!-d $xap, 'Xapian directories removed again');
351 local $SIG{__WARN__} = sub { push @warn, @_ };
352 my %config = %$ibx_config;
353 my $ibx = PublicInbox::Inbox->new(\%config);
354 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
355 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
356 # mark2 an add and it's delete in the same index_sync
357 eval { $rw->index_sync({ref => $mark2}) };
358 is($@, '', 'no error from reindexing without msgmap');
359 is_deeply(\@warn, [], 'no warnings');
361 my ($min, $max) = $ibx->mm->minmax;
362 is($min, 1, 'min as expected');
363 is($max, 3, 'max as expected');
364 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
365 is_deeply($ibx->mm->msg_range(\$min, $max),
367 [1, '1@example.com' ],
368 [2, '2@example.com' ],
369 [3, '3@example.com' ],
370 ], 'msgmap as expected' );
374 local $SIG{__WARN__} = sub { push @warn, @_ };
375 my %config = %$ibx_config;
376 my $ibx = PublicInbox::Inbox->new(\%config);
377 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
378 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
379 # mark3 adds following the delete at mark2
380 eval { $rw->index_sync({ref => $mark3}) };
381 is($@, '', 'no error from reindexing without msgmap');
382 is_deeply(\@warn, [], 'no warnings');
384 my ($min, $max) = $ibx->mm->minmax;
385 is($min, 1, 'min as expected');
386 is($max, 10, 'max as expected');
387 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
388 is_deeply($ibx->mm->msg_range(\$min, $max),
390 [1, '1@example.com' ],
391 [2, '2@example.com' ],
392 [3, '3@example.com' ],
393 [5, '5@example.com' ],
394 [6, '6@example.com' ],
395 [7, '7@example.com' ],
396 [8, '8@example.com' ],
397 [9, '9@example.com' ],
398 [10, '10@example.com' ],
399 ], 'msgmap as expected' );
403 local $SIG{__WARN__} = sub { push @warn, @_ };
404 my %config = %$ibx_config;
405 my $ibx = PublicInbox::Inbox->new(\%config);
406 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
407 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
408 # mark4 A delete of an older message
409 eval { $rw->index_sync({ref => $mark4}) };
410 is($@, '', 'no error from reindexing without msgmap');
411 is_deeply(\@warn, [], 'no warnings');
413 my ($min, $max) = $ibx->mm->minmax;
414 is($min, 1, 'min as expected');
415 is($max, 10, 'max as expected');
416 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
417 is_deeply($ibx->mm->msg_range(\$min, $max),
419 [1, '1@example.com' ],
420 [2, '2@example.com' ],
421 [3, '3@example.com' ],
422 [6, '6@example.com' ],
423 [7, '7@example.com' ],
424 [8, '8@example.com' ],
425 [9, '9@example.com' ],
426 [10, '10@example.com' ],
427 ], 'msgmap as expected' );