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 ($mark1, $mark2, $mark3, $mark4);
38 my %config = %$ibx_config;
39 my $ibx = PublicInbox::Inbox->new(\%config);
40 my $im = PublicInbox::V2Writable->new($ibx, 1);
41 my $im0 = $im->importer();
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 = $im0->get_mark($im0->{tip});
48 $mark2 = $im0->get_mark($im0->{tip});
52 if ('test remove later') {
53 $mark3 = $im0->get_mark($im0->{tip});
54 $mime->header_set('Message-Id', "<5\@example.com>");
56 $mark4 = $im0->get_mark($im0->{tip});
60 $minmax = [ $ibx->mm->minmax ];
61 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
62 is_deeply($minmax, [ 1, 10 ], 'minmax 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::V2Writable->new($ibx, 1);
82 eval { $im->index_sync({reindex => 1}) };
83 is($@, '', 'no error from reindexing');
87 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
89 my ($min, $max) = $ibx->mm->minmax;
90 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
93 my $xap = "$mainrepo/xap".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::V2Writable->new($ibx, 1);
100 eval { $im->index_sync({reindex => 1}) };
101 is($@, '', 'no error from reindexing');
103 ok(-d $xap, 'Xapian directories recreated');
106 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
108 my ($min, $max) = $ibx->mm->minmax;
109 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
112 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
114 ok(!-d $xap, 'Xapian directories removed again');
117 local $SIG{__WARN__} = sub { push @warn, @_ };
118 my %config = %$ibx_config;
119 my $ibx = PublicInbox::Inbox->new(\%config);
120 my $im = PublicInbox::V2Writable->new($ibx, 1);
121 eval { $im->index_sync({reindex => 1}) };
122 is($@, '', 'no error from reindexing without msgmap');
123 is(scalar(@warn), 0, 'no warnings from reindexing');
125 ok(-d $xap, 'Xapian directories recreated');
127 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
129 my ($min, $max) = $ibx->mm->minmax;
130 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
134 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
136 ok(!-d $xap, 'Xapian directories removed again');
139 local $SIG{__WARN__} = sub { push @warn, @_ };
140 my %config = %$ibx_config;
141 my $ibx = PublicInbox::Inbox->new(\%config);
142 my $im = PublicInbox::V2Writable->new($ibx, 1);
143 eval { $im->index_sync({reindex => 1}) };
144 is($@, '', 'no error from reindexing without msgmap');
145 is_deeply(\@warn, [], 'no warnings');
147 ok(-d $xap, 'Xapian directories recreated');
149 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
150 my $mset = $ibx->search->query('"hello world"', {mset=>1});
151 isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
152 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
154 my ($min, $max) = $ibx->mm->minmax;
155 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
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} = 'medium';
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');
177 # not sure why, but Xapian seems to fallback to terms and
178 # phrase searches still work
179 delete $ibx->{search};
180 my $mset = $ibx->search->query('"hello world"', {mset=>1});
181 is($mset->size, 0, 'phrase search does not work on medium');
184 my $mset = $ibx->search->query('hello world', {mset=>1});
185 isnt($mset->size, 0, "normal search works on indexlevel=medium");
186 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
187 ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
190 my ($min, $max) = $ibx->mm->minmax;
191 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
194 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
196 ok(!-d $xap, 'Xapian directories removed again');
199 local $SIG{__WARN__} = sub { push @warn, @_ };
200 my %config = %$ibx_config;
201 $config{indexlevel} = 'basic';
202 my $ibx = PublicInbox::Inbox->new(\%config);
203 my $im = PublicInbox::V2Writable->new($ibx);
204 eval { $im->index_sync({reindex => 1}) };
205 is($@, '', 'no error from reindexing without msgmap');
206 is_deeply(\@warn, [], 'no warnings');
208 ok(-d $xap, 'Xapian directories recreated');
210 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
211 my $mset = $ibx->search->query('hello', {mset=>1});
212 is($mset->size, 0, "search fails on indexlevel='basic'");
213 for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
214 ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');
216 my ($min, $max) = $ibx->mm->minmax;
217 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
221 # An incremental indexing test
222 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
224 ok(!-d $xap, 'Xapian directories removed again');
227 local $SIG{__WARN__} = sub { push @warn, @_ };
228 my %config = %$ibx_config;
229 my $ibx = PublicInbox::Inbox->new(\%config);
230 # mark1 4 simple additions in the same index_sync
231 $ibx->{ref_head} = $mark1;
232 my $im = PublicInbox::V2Writable->new($ibx);
233 eval { $im->index_sync() };
234 is($@, '', 'no error from reindexing without msgmap');
235 is_deeply(\@warn, [], 'no warnings');
237 my ($min, $max) = $ibx->mm->minmax;
238 is($min, 1, 'min as expected');
239 is($max, 4, 'max as expected');
240 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
241 is_deeply($ibx->mm->msg_range(\$min, $max),
243 [1, '1@example.com' ],
244 [2, '2@example.com' ],
245 [3, '3@example.com' ],
246 [4, '4@example.com' ],
247 ], 'msgmap as expected' );
251 local $SIG{__WARN__} = sub { push @warn, @_ };
252 my %config = %$ibx_config;
253 my $ibx = PublicInbox::Inbox->new(\%config);
254 # mark2 A delete separated from an add in the same index_sync
255 $ibx->{ref_head} = $mark2;
256 my $im = PublicInbox::V2Writable->new($ibx);
257 eval { $im->index_sync() };
258 is($@, '', 'no error from reindexing without msgmap');
259 is_deeply(\@warn, [], 'no warnings');
261 my ($min, $max) = $ibx->mm->minmax;
262 is($min, 1, 'min as expected');
263 is($max, 3, 'max as expected');
264 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
265 is_deeply($ibx->mm->msg_range(\$min, $max),
267 [1, '1@example.com' ],
268 [2, '2@example.com' ],
269 [3, '3@example.com' ],
270 ], 'msgmap as expected' );
274 local $SIG{__WARN__} = sub { push @warn, @_ };
275 my %config = %$ibx_config;
276 my $ibx = PublicInbox::Inbox->new(\%config);
277 # mark3 adds following the delete at mark2
278 $ibx->{ref_head} = $mark3;
279 my $im = PublicInbox::V2Writable->new($ibx);
280 eval { $im->index_sync() };
281 is($@, '', 'no error from reindexing without msgmap');
282 is_deeply(\@warn, [], 'no warnings');
284 my ($min, $max) = $ibx->mm->minmax;
285 is($min, 1, 'min as expected');
286 is($max, 10, 'max as expected');
287 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
288 is_deeply($ibx->mm->msg_range(\$min, $max),
290 [1, '1@example.com' ],
291 [2, '2@example.com' ],
292 [3, '3@example.com' ],
293 [5, '5@example.com' ],
294 [6, '6@example.com' ],
295 [7, '7@example.com' ],
296 [8, '8@example.com' ],
297 [9, '9@example.com' ],
298 [10, '10@example.com' ],
299 ], 'msgmap as expected' );
303 local $SIG{__WARN__} = sub { push @warn, @_ };
304 my %config = %$ibx_config;
305 my $ibx = PublicInbox::Inbox->new(\%config);
306 # mark4 A delete of an older message
307 $ibx->{ref_head} = $mark4;
308 my $im = PublicInbox::V2Writable->new($ibx);
309 eval { $im->index_sync() };
310 is($@, '', 'no error from reindexing without msgmap');
311 is_deeply(\@warn, [], 'no warnings');
313 my ($min, $max) = $ibx->mm->minmax;
314 is($min, 1, 'min as expected');
315 is($max, 10, 'max as expected');
316 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
317 is_deeply($ibx->mm->msg_range(\$min, $max),
319 [1, '1@example.com' ],
320 [2, '2@example.com' ],
321 [3, '3@example.com' ],
322 [6, '6@example.com' ],
323 [7, '7@example.com' ],
324 [8, '8@example.com' ],
325 [9, '9@example.com' ],
326 [10, '10@example.com' ],
327 ], 'msgmap as expected' );
331 # Another incremental indexing test
332 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
334 ok(!-d $xap, 'Xapian directories removed again');
337 local $SIG{__WARN__} = sub { push @warn, @_ };
338 my %config = %$ibx_config;
339 my $ibx = PublicInbox::Inbox->new(\%config);
340 # mark2 an add and it's delete in the same index_sync
341 $ibx->{ref_head} = $mark2;
342 my $im = PublicInbox::V2Writable->new($ibx);
343 eval { $im->index_sync() };
344 is($@, '', 'no error from reindexing without msgmap');
345 is_deeply(\@warn, [], 'no warnings');
347 my ($min, $max) = $ibx->mm->minmax;
348 is($min, 1, 'min as expected');
349 is($max, 3, 'max as expected');
350 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
351 is_deeply($ibx->mm->msg_range(\$min, $max),
353 [1, '1@example.com' ],
354 [2, '2@example.com' ],
355 [3, '3@example.com' ],
356 ], 'msgmap as expected' );
360 local $SIG{__WARN__} = sub { push @warn, @_ };
361 my %config = %$ibx_config;
362 my $ibx = PublicInbox::Inbox->new(\%config);
363 # mark3 adds following the delete at mark2
364 $ibx->{ref_head} = $mark3;
365 my $im = PublicInbox::V2Writable->new($ibx);
366 eval { $im->index_sync() };
367 is($@, '', 'no error from reindexing without msgmap');
368 is_deeply(\@warn, [], 'no warnings');
370 my ($min, $max) = $ibx->mm->minmax;
371 is($min, 1, 'min as expected');
372 is($max, 10, 'max as expected');
373 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
374 is_deeply($ibx->mm->msg_range(\$min, $max),
376 [1, '1@example.com' ],
377 [2, '2@example.com' ],
378 [3, '3@example.com' ],
379 [5, '5@example.com' ],
380 [6, '6@example.com' ],
381 [7, '7@example.com' ],
382 [8, '8@example.com' ],
383 [9, '9@example.com' ],
384 [10, '10@example.com' ],
385 ], 'msgmap as expected' );
389 local $SIG{__WARN__} = sub { push @warn, @_ };
390 my %config = %$ibx_config;
391 my $ibx = PublicInbox::Inbox->new(\%config);
392 # mark4 A delete of an older message
393 $ibx->{ref_head} = $mark4;
394 my $im = PublicInbox::V2Writable->new($ibx);
395 eval { $im->index_sync() };
396 is($@, '', 'no error from reindexing without msgmap');
397 is_deeply(\@warn, [], 'no warnings');
399 my ($min, $max) = $ibx->mm->minmax;
400 is($min, 1, 'min as expected');
401 is($max, 10, 'max as expected');
402 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
403 is_deeply($ibx->mm->msg_range(\$min, $max),
405 [1, '1@example.com' ],
406 [2, '2@example.com' ],
407 [3, '3@example.com' ],
408 [6, '6@example.com' ],
409 [7, '7@example.com' ],
410 [8, '8@example.com' ],
411 [9, '9@example.com' ],
412 [10, '10@example.com' ],
413 ], 'msgmap as expected' );