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_deeply($ibx->mm->msg_range(\$min, $max),
242 [1, '1@example.com' ],
243 [2, '2@example.com' ],
244 [3, '3@example.com' ],
245 [4, '4@example.com' ],
246 ], 'msgmap as expected' );
250 local $SIG{__WARN__} = sub { push @warn, @_ };
251 my %config = %$ibx_config;
252 my $ibx = PublicInbox::Inbox->new(\%config);
253 # mark2 A delete separated from an add in the same index_sync
254 $ibx->{ref_head} = $mark2;
255 my $im = PublicInbox::V2Writable->new($ibx);
256 eval { $im->index_sync() };
257 is($@, '', 'no error from reindexing without msgmap');
258 is_deeply(\@warn, [], 'no warnings');
260 my ($min, $max) = $ibx->mm->minmax;
261 is($min, 1, 'min as expected');
262 is($max, 3, 'max as expected');
263 is_deeply($ibx->mm->msg_range(\$min, $max),
265 [1, '1@example.com' ],
266 [2, '2@example.com' ],
267 [3, '3@example.com' ],
268 ], 'msgmap as expected' );
272 local $SIG{__WARN__} = sub { push @warn, @_ };
273 my %config = %$ibx_config;
274 my $ibx = PublicInbox::Inbox->new(\%config);
275 # mark3 adds following the delete at mark2
276 $ibx->{ref_head} = $mark3;
277 my $im = PublicInbox::V2Writable->new($ibx);
278 eval { $im->index_sync() };
279 is($@, '', 'no error from reindexing without msgmap');
280 is_deeply(\@warn, [], 'no warnings');
282 my ($min, $max) = $ibx->mm->minmax;
283 is($min, 1, 'min as expected');
284 is($max, 10, 'max as expected');
285 is_deeply($ibx->mm->msg_range(\$min, $max),
287 [1, '1@example.com' ],
288 [2, '2@example.com' ],
289 [3, '3@example.com' ],
290 [5, '5@example.com' ],
291 [6, '6@example.com' ],
292 [7, '7@example.com' ],
293 [8, '8@example.com' ],
294 [9, '9@example.com' ],
295 [10, '10@example.com' ],
296 ], 'msgmap as expected' );
300 local $SIG{__WARN__} = sub { push @warn, @_ };
301 my %config = %$ibx_config;
302 my $ibx = PublicInbox::Inbox->new(\%config);
303 # mark4 A delete of an older message
304 $ibx->{ref_head} = $mark4;
305 my $im = PublicInbox::V2Writable->new($ibx);
306 eval { $im->index_sync() };
307 is($@, '', 'no error from reindexing without msgmap');
308 is_deeply(\@warn, [], 'no warnings');
310 my ($min, $max) = $ibx->mm->minmax;
311 is($min, 1, 'min as expected');
312 is($max, 10, 'max as expected');
313 is_deeply($ibx->mm->msg_range(\$min, $max),
315 [1, '1@example.com' ],
316 [2, '2@example.com' ],
317 [3, '3@example.com' ],
318 [6, '6@example.com' ],
319 [7, '7@example.com' ],
320 [8, '8@example.com' ],
321 [9, '9@example.com' ],
322 [10, '10@example.com' ],
323 ], 'msgmap as expected' );
327 # Another incremental indexing test
328 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
330 ok(!-d $xap, 'Xapian directories removed again');
333 local $SIG{__WARN__} = sub { push @warn, @_ };
334 my %config = %$ibx_config;
335 my $ibx = PublicInbox::Inbox->new(\%config);
336 # mark2 an add and it's delete in the same index_sync
337 $ibx->{ref_head} = $mark2;
338 my $im = PublicInbox::V2Writable->new($ibx);
339 eval { $im->index_sync() };
340 is($@, '', 'no error from reindexing without msgmap');
341 is_deeply(\@warn, [], 'no warnings');
343 my ($min, $max) = $ibx->mm->minmax;
344 is($min, 1, 'min as expected');
345 is($max, 3, 'max as expected');
346 is_deeply($ibx->mm->msg_range(\$min, $max),
348 [1, '1@example.com' ],
349 [2, '2@example.com' ],
350 [3, '3@example.com' ],
351 ], 'msgmap as expected' );
355 local $SIG{__WARN__} = sub { push @warn, @_ };
356 my %config = %$ibx_config;
357 my $ibx = PublicInbox::Inbox->new(\%config);
358 # mark3 adds following the delete at mark2
359 $ibx->{ref_head} = $mark3;
360 my $im = PublicInbox::V2Writable->new($ibx);
361 eval { $im->index_sync() };
362 is($@, '', 'no error from reindexing without msgmap');
363 is_deeply(\@warn, [], 'no warnings');
365 my ($min, $max) = $ibx->mm->minmax;
366 is($min, 1, 'min as expected');
367 is($max, 10, 'max as expected');
368 is_deeply($ibx->mm->msg_range(\$min, $max),
370 [1, '1@example.com' ],
371 [2, '2@example.com' ],
372 [3, '3@example.com' ],
373 [5, '5@example.com' ],
374 [6, '6@example.com' ],
375 [7, '7@example.com' ],
376 [8, '8@example.com' ],
377 [9, '9@example.com' ],
378 [10, '10@example.com' ],
379 ], 'msgmap as expected' );
383 local $SIG{__WARN__} = sub { push @warn, @_ };
384 my %config = %$ibx_config;
385 my $ibx = PublicInbox::Inbox->new(\%config);
386 # mark4 A delete of an older message
387 $ibx->{ref_head} = $mark4;
388 my $im = PublicInbox::V2Writable->new($ibx);
389 eval { $im->index_sync() };
390 is($@, '', 'no error from reindexing without msgmap');
391 is_deeply(\@warn, [], 'no warnings');
393 my ($min, $max) = $ibx->mm->minmax;
394 is($min, 1, 'min as expected');
395 is($max, 10, 'max as expected');
396 is_deeply($ibx->mm->msg_range(\$min, $max),
398 [1, '1@example.com' ],
399 [2, '2@example.com' ],
400 [3, '3@example.com' ],
401 [6, '6@example.com' ],
402 [7, '7@example.com' ],
403 [8, '8@example.com' ],
404 [9, '9@example.com' ],
405 [10, '10@example.com' ],
406 ], 'msgmap as expected' );