1 # Copyright (C) 2018-2019 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::Path qw(remove_tree);
9 use PublicInbox::TestCommon;
12 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
14 plan skip_all => "$mod missing for v1reindex.t" if $@;
16 use_ok 'PublicInbox::SearchIdx';
17 use_ok 'PublicInbox::Import';
18 my ($inboxdir, $for_destroy) = tmpdir();
19 is(system(qw(git init -q --bare), $inboxdir), 0);
21 inboxdir => $inboxdir,
22 name => 'test-v1reindex',
23 -primary_address => 'test@example.com',
26 my $mime = PublicInbox::MIME->create(
28 From => 'a@example.com',
29 To => 'test@example.com',
30 Subject => 'this is a subject',
31 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
33 body => "hello world\n",
37 my ($mark1, $mark2, $mark3, $mark4);
39 my %config = %$ibx_config;
40 my $ibx = PublicInbox::Inbox->new(\%config);
41 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
42 foreach my $i (1..10) {
43 $mime->header_set('Message-Id', "<$i\@example.com>");
44 ok($im->add($mime), "message $i added");
46 $mark1 = $im->get_mark($im->{tip});
48 $mark2 = $im->get_mark($im->{tip});
52 if ('test remove later') {
53 $mark3 = $im->get_mark($im->{tip});
54 $mime->header_set('Message-Id', "<5\@example.com>");
56 $mark4 = $im->get_mark($im->{tip});
60 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
61 eval { $rw->index_sync() };
62 is($@, '', 'no error from indexing');
64 $minmax = [ $ibx->mm->minmax ];
65 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
66 is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
67 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
69 my ($min, $max) = @$minmax;
70 $msgmap = $ibx->mm->msg_range(\$min, $max);
72 [1, '1@example.com' ],
73 [2, '2@example.com' ],
74 [3, '3@example.com' ],
75 [6, '6@example.com' ],
76 [7, '7@example.com' ],
77 [8, '8@example.com' ],
78 [9, '9@example.com' ],
79 [10, '10@example.com' ],
80 ], 'msgmap as expected');
84 my %config = %$ibx_config;
85 my $ibx = PublicInbox::Inbox->new(\%config);
86 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
87 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
88 eval { $rw->index_sync({reindex => 1}) };
89 is($@, '', 'no error from reindexing');
92 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
94 my ($min, $max) = $ibx->mm->minmax;
95 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
98 my $xap = "$inboxdir/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
100 ok(!-d $xap, 'Xapian directories removed');
102 my %config = %$ibx_config;
103 my $ibx = PublicInbox::Inbox->new(\%config);
104 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
105 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
107 eval { $rw->index_sync({reindex => 1}) };
108 is($@, '', 'no error from reindexing');
110 ok(-d $xap, 'Xapian directories recreated');
113 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
114 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
116 my ($min, $max) = $ibx->mm->minmax;
117 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
120 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
122 ok(!-d $xap, 'Xapian directories removed again');
125 local $SIG{__WARN__} = sub { push @warn, @_ };
126 my %config = %$ibx_config;
127 my $ibx = PublicInbox::Inbox->new(\%config);
128 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
129 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
130 eval { $rw->index_sync({reindex => 1}) };
131 is($@, '', 'no error from reindexing without msgmap');
132 is(scalar(@warn), 0, 'no warnings from reindexing');
134 ok(-d $xap, 'Xapian directories recreated');
136 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
137 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
139 my ($min, $max) = $ibx->mm->minmax;
140 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
143 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
145 ok(!-d $xap, 'Xapian directories removed again');
148 local $SIG{__WARN__} = sub { push @warn, @_ };
149 my %config = %$ibx_config;
150 my $ibx = PublicInbox::Inbox->new(\%config);
151 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
152 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
153 eval { $rw->index_sync({reindex => 1}) };
154 is($@, '', 'no error from reindexing without msgmap');
155 is_deeply(\@warn, [], 'no warnings');
157 ok(-d $xap, 'Xapian directories recreated');
159 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
160 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
162 my ($min, $max) = @$minmax;
163 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
166 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
168 ok(!-d $xap, 'Xapian directories removed again');
171 local $SIG{__WARN__} = sub { push @warn, @_ };
172 my %config = %$ibx_config;
173 $config{indexlevel} = 'medium';
174 my $ibx = PublicInbox::Inbox->new(\%config);
175 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
176 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
177 eval { $rw->index_sync({reindex => 1}) };
178 is($@, '', 'no error from reindexing without msgmap');
179 is_deeply(\@warn, [], 'no warnings');
181 ok(-d $xap, 'Xapian directories recreated');
183 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
184 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
185 my $mset = $ibx->search->query('hello world', {mset=>1});
186 isnt($mset->size, 0, 'got Xapian search results');
188 my ($min, $max) = $ibx->mm->minmax;
189 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
192 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
194 ok(!-d $xap, 'Xapian directories removed again');
197 local $SIG{__WARN__} = sub { push @warn, @_ };
198 my %config = %$ibx_config;
199 $config{indexlevel} = 'basic';
200 my $ibx = PublicInbox::Inbox->new(\%config);
201 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
202 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
203 eval { $rw->index_sync({reindex => 1}) };
204 is($@, '', 'no error from reindexing without msgmap');
205 is_deeply(\@warn, [], 'no warnings');
207 ok(-d $xap, 'Xapian directories recreated');
209 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
210 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
211 isnt($ibx->search, 'no search for basic');
213 my ($min, $max) = $ibx->mm->minmax;
214 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
217 # upgrade existing basic to medium
218 # note: changing indexlevels is not yet supported in v2,
219 # and may not be without more effort
223 local $SIG{__WARN__} = sub { push @warn, @_ };
224 my %config = %$ibx_config;
225 $config{indexlevel} = 'medium';
226 my $ibx = PublicInbox::Inbox->new(\%config);
227 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
228 eval { $rw->index_sync };
229 is($@, '', 'no error from indexing');
230 is_deeply(\@warn, [], 'no warnings');
231 my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
232 isnt($mset->size, 0, 'search OK after basic -> medium');
234 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
236 my ($min, $max) = $ibx->mm->minmax;
237 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
240 # An incremental indexing test
241 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
243 ok(!-d $xap, 'Xapian directories removed again');
246 local $SIG{__WARN__} = sub { push @warn, @_ };
247 my %config = %$ibx_config;
248 my $ibx = PublicInbox::Inbox->new(\%config);
249 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
250 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
251 # mark1 4 simple additions in the same index_sync
252 eval { $rw->index_sync({ref => $mark1}) };
253 is($@, '', 'no error from reindexing without msgmap');
254 is_deeply(\@warn, [], 'no warnings');
256 my ($min, $max) = $ibx->mm->minmax;
257 is($min, 1, 'min as expected');
258 is($max, 4, 'max as expected');
259 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
260 is_deeply($ibx->mm->msg_range(\$min, $max),
262 [1, '1@example.com' ],
263 [2, '2@example.com' ],
264 [3, '3@example.com' ],
265 [4, '4@example.com' ],
266 ], 'msgmap as expected' );
270 local $SIG{__WARN__} = sub { push @warn, @_ };
271 my %config = %$ibx_config;
272 my $ibx = PublicInbox::Inbox->new(\%config);
273 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
274 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
275 # mark2 A delete separated form and add in the same index_sync
276 eval { $rw->index_sync({ref => $mark2}) };
277 is($@, '', 'no error from reindexing without msgmap');
278 is_deeply(\@warn, [], 'no warnings');
280 my ($min, $max) = $ibx->mm->minmax;
281 is($min, 1, 'min as expected');
282 is($max, 3, 'max as expected');
283 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
284 is_deeply($ibx->mm->msg_range(\$min, $max),
286 [1, '1@example.com' ],
287 [2, '2@example.com' ],
288 [3, '3@example.com' ],
289 ], 'msgmap as expected' );
293 local $SIG{__WARN__} = sub { push @warn, @_ };
294 my %config = %$ibx_config;
295 my $ibx = PublicInbox::Inbox->new(\%config);
296 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
297 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
298 # mark3 adds following the delete at mark2
299 eval { $rw->index_sync({ref => $mark3}) };
300 is($@, '', 'no error from reindexing without msgmap');
301 is_deeply(\@warn, [], 'no warnings');
303 my ($min, $max) = $ibx->mm->minmax;
304 is($min, 1, 'min as expected');
305 is($max, 10, 'max as expected');
306 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
307 is_deeply($ibx->mm->msg_range(\$min, $max),
309 [1, '1@example.com' ],
310 [2, '2@example.com' ],
311 [3, '3@example.com' ],
312 [5, '5@example.com' ],
313 [6, '6@example.com' ],
314 [7, '7@example.com' ],
315 [8, '8@example.com' ],
316 [9, '9@example.com' ],
317 [10, '10@example.com' ],
318 ], 'msgmap as expected' );
322 local $SIG{__WARN__} = sub { push @warn, @_ };
323 my %config = %$ibx_config;
324 my $ibx = PublicInbox::Inbox->new(\%config);
325 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
326 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
327 # mark4 A delete of an older message
328 eval { $rw->index_sync({ref => $mark4}) };
329 is($@, '', 'no error from reindexing without msgmap');
330 is_deeply(\@warn, [], 'no warnings');
332 my ($min, $max) = $ibx->mm->minmax;
333 is($min, 1, 'min as expected');
334 is($max, 10, 'max as expected');
335 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
336 is_deeply($ibx->mm->msg_range(\$min, $max),
338 [1, '1@example.com' ],
339 [2, '2@example.com' ],
340 [3, '3@example.com' ],
341 [6, '6@example.com' ],
342 [7, '7@example.com' ],
343 [8, '8@example.com' ],
344 [9, '9@example.com' ],
345 [10, '10@example.com' ],
346 ], 'msgmap as expected' );
350 # Another incremental indexing test
351 ok(unlink "$inboxdir/public-inbox/msgmap.sqlite3", 'remove msgmap');
353 ok(!-d $xap, 'Xapian directories removed again');
356 local $SIG{__WARN__} = sub { push @warn, @_ };
357 my %config = %$ibx_config;
358 my $ibx = PublicInbox::Inbox->new(\%config);
359 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
360 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
361 # mark2 an add and it's delete in the same index_sync
362 eval { $rw->index_sync({ref => $mark2}) };
363 is($@, '', 'no error from reindexing without msgmap');
364 is_deeply(\@warn, [], 'no warnings');
366 my ($min, $max) = $ibx->mm->minmax;
367 is($min, 1, 'min as expected');
368 is($max, 3, 'max as expected');
369 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
370 is_deeply($ibx->mm->msg_range(\$min, $max),
372 [1, '1@example.com' ],
373 [2, '2@example.com' ],
374 [3, '3@example.com' ],
375 ], 'msgmap as expected' );
379 local $SIG{__WARN__} = sub { push @warn, @_ };
380 my %config = %$ibx_config;
381 my $ibx = PublicInbox::Inbox->new(\%config);
382 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
383 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
384 # mark3 adds following the delete at mark2
385 eval { $rw->index_sync({ref => $mark3}) };
386 is($@, '', 'no error from reindexing without msgmap');
387 is_deeply(\@warn, [], 'no warnings');
389 my ($min, $max) = $ibx->mm->minmax;
390 is($min, 1, 'min as expected');
391 is($max, 10, 'max as expected');
392 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
393 is_deeply($ibx->mm->msg_range(\$min, $max),
395 [1, '1@example.com' ],
396 [2, '2@example.com' ],
397 [3, '3@example.com' ],
398 [5, '5@example.com' ],
399 [6, '6@example.com' ],
400 [7, '7@example.com' ],
401 [8, '8@example.com' ],
402 [9, '9@example.com' ],
403 [10, '10@example.com' ],
404 ], 'msgmap as expected' );
408 local $SIG{__WARN__} = sub { push @warn, @_ };
409 my %config = %$ibx_config;
410 my $ibx = PublicInbox::Inbox->new(\%config);
411 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
412 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
413 # mark4 A delete of an older message
414 eval { $rw->index_sync({ref => $mark4}) };
415 is($@, '', 'no error from reindexing without msgmap');
416 is_deeply(\@warn, [], 'no warnings');
418 my ($min, $max) = $ibx->mm->minmax;
419 is($min, 1, 'min as expected');
420 is($max, 10, 'max as expected');
421 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
422 is_deeply($ibx->mm->msg_range(\$min, $max),
424 [1, '1@example.com' ],
425 [2, '2@example.com' ],
426 [3, '3@example.com' ],
427 [6, '6@example.com' ],
428 [7, '7@example.com' ],
429 [8, '8@example.com' ],
430 [9, '9@example.com' ],
431 [10, '10@example.com' ],
432 ], 'msgmap as expected' );