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>
7 use PublicInbox::ContentHash qw(content_digest);
8 use File::Path qw(remove_tree);
9 use PublicInbox::TestCommon;
11 require_mods(qw(DBD::SQLite Search::Xapian));
12 use_ok 'PublicInbox::V2Writable';
13 use_ok 'PublicInbox::OverIdx';
14 my ($inboxdir, $for_destroy) = tmpdir();
16 inboxdir => $inboxdir,
17 name => 'test-v2writable',
19 -primary_address => 'test@example.com',
23 open my $fh, '<', 'COPYING' or die "can't open COPYING: $!";
27 my $phrase = q("defending all users' freedom");
28 my $mime = PublicInbox::Eml->new(<<'EOF'.$agpl);
31 Subject: this is a subject
32 Date: Fri, 02 Oct 1993 00:00:00 +0000
37 my ($mark1, $mark2, $mark3, $mark4);
39 my %config = %$ibx_config;
40 my $ibx = PublicInbox::Inbox->new(\%config);
41 my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
42 my $im0 = $im->importer(0);
43 foreach my $i (1..10) {
44 $mime->header_set('Message-Id', "<$i\@example.com>");
45 ok($im->add($mime), "message $i added");
47 $mark1 = $im0->get_mark($im0->{tip});
49 $mark2 = $im0->get_mark($im0->{tip});
53 if ('test remove later') {
54 $mark3 = $im0->get_mark($im0->{tip});
55 $mime->header_set('Message-Id', "<5\@example.com>");
57 $mark4 = $im0->get_mark($im0->{tip});
61 $minmax = [ $ibx->mm->minmax ];
62 ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
63 is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
64 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
66 my ($min, $max) = @$minmax;
67 $msgmap = $ibx->mm->msg_range(\$min, $max);
69 [1, '1@example.com' ],
70 [2, '2@example.com' ],
71 [3, '3@example.com' ],
72 [6, '6@example.com' ],
73 [7, '7@example.com' ],
74 [8, '8@example.com' ],
75 [9, '9@example.com' ],
76 [10, '10@example.com' ],
77 ], 'msgmap as expected');
81 my %config = %$ibx_config;
82 my $ibx = PublicInbox::Inbox->new(\%config);
83 my $im = PublicInbox::V2Writable->new($ibx, 1);
84 eval { $im->index_sync({reindex => 1}) };
85 is($@, '', 'no error from reindexing');
89 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
90 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
92 my ($min, $max) = $ibx->mm->minmax;
93 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
96 my $xap = "$inboxdir/xap".PublicInbox::Search::SCHEMA_VERSION();
98 ok(!-d $xap, 'Xapian directories removed');
100 my %config = %$ibx_config;
101 my $ibx = PublicInbox::Inbox->new(\%config);
102 my $im = PublicInbox::V2Writable->new($ibx, 1);
103 eval { $im->index_sync({reindex => 1}) };
104 is($@, '', 'no error from reindexing');
106 ok(-d $xap, 'Xapian directories recreated');
109 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
110 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
112 my ($min, $max) = $ibx->mm->minmax;
113 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
116 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
118 ok(!-d $xap, 'Xapian directories removed again');
121 local $SIG{__WARN__} = sub { push @warn, @_ };
122 my %config = %$ibx_config;
123 my $ibx = PublicInbox::Inbox->new(\%config);
124 my $im = PublicInbox::V2Writable->new($ibx, 1);
125 eval { $im->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');
139 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
141 ok(!-d $xap, 'Xapian directories removed again');
144 local $SIG{__WARN__} = sub { push @warn, @_ };
145 my %config = %$ibx_config;
146 my $ibx = PublicInbox::Inbox->new(\%config);
147 my $im = PublicInbox::V2Writable->new($ibx, 1);
148 eval { $im->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');
156 my $mset = $ibx->search->mset($phrase);
157 isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
158 for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
160 my ($min, $max) = $ibx->mm->minmax;
161 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
164 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
166 ok(!-d $xap, 'Xapian directories removed again');
169 local $SIG{__WARN__} = sub { push @warn, @_ };
170 my %config = %$ibx_config;
171 $config{indexlevel} = 'medium';
172 my $ibx = PublicInbox::Inbox->new(\%config);
173 my $im = PublicInbox::V2Writable->new($ibx);
174 eval { $im->index_sync({reindex => 1}) };
175 is($@, '', 'no error from reindexing without msgmap');
176 is_deeply(\@warn, [], 'no warnings');
178 ok(-d $xap, 'Xapian directories recreated');
180 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
181 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
184 # not sure why, but Xapian seems to fallback to terms and
185 # phrase searches still work
186 delete $ibx->{search};
187 my $mset = $ibx->search->mset($phrase);
188 is($mset->size, 0, 'phrase search does not work on medium');
192 my $mset = $ibx->search->mset($words);
193 isnt($mset->size, 0, "normal search works on indexlevel=medium");
194 for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
196 ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
199 my ($min, $max) = $ibx->mm->minmax;
200 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
203 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
205 ok(!-d $xap, 'Xapian directories removed again');
208 local $SIG{__WARN__} = sub { push @warn, @_ };
209 my %config = %$ibx_config;
210 $config{indexlevel} = 'basic';
211 my $ibx = PublicInbox::Inbox->new(\%config);
212 my $im = PublicInbox::V2Writable->new($ibx);
213 eval { $im->index_sync({reindex => 1}) };
214 is($@, '', 'no error from reindexing without msgmap');
215 is_deeply(\@warn, [], 'no warnings');
217 ok(-d $xap, 'Xapian directories recreated');
219 is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
220 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
222 isnt($ibx->search, 'no search for basic');
224 for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
225 ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');
227 my ($min, $max) = $ibx->mm->minmax;
228 is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
232 # An incremental indexing test
233 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
235 ok(!-d $xap, 'Xapian directories removed again');
238 local $SIG{__WARN__} = sub { push @warn, @_ };
239 my %config = %$ibx_config;
240 my $ibx = PublicInbox::Inbox->new(\%config);
241 # mark1 4 simple additions in the same index_sync
242 $ibx->{ref_head} = $mark1;
243 my $im = PublicInbox::V2Writable->new($ibx);
244 eval { $im->index_sync() };
245 is($@, '', 'no error from reindexing without msgmap');
246 is_deeply(\@warn, [], 'no warnings');
248 my ($min, $max) = $ibx->mm->minmax;
249 is($min, 1, 'min as expected');
250 is($max, 4, 'max as expected');
251 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
252 is_deeply($ibx->mm->msg_range(\$min, $max),
254 [1, '1@example.com' ],
255 [2, '2@example.com' ],
256 [3, '3@example.com' ],
257 [4, '4@example.com' ],
258 ], 'msgmap as expected' );
262 local $SIG{__WARN__} = sub { push @warn, @_ };
263 my %config = %$ibx_config;
264 my $ibx = PublicInbox::Inbox->new(\%config);
265 # mark2 A delete separated from an add in the same index_sync
266 $ibx->{ref_head} = $mark2;
267 my $im = PublicInbox::V2Writable->new($ibx);
268 eval { $im->index_sync() };
269 is($@, '', 'no error from reindexing without msgmap');
270 is_deeply(\@warn, [], 'no warnings');
272 my ($min, $max) = $ibx->mm->minmax;
273 is($min, 1, 'min as expected');
274 is($max, 3, 'max as expected');
275 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
276 is_deeply($ibx->mm->msg_range(\$min, $max),
278 [1, '1@example.com' ],
279 [2, '2@example.com' ],
280 [3, '3@example.com' ],
281 ], 'msgmap as expected' );
285 local $SIG{__WARN__} = sub { push @warn, @_ };
286 my %config = %$ibx_config;
287 my $ibx = PublicInbox::Inbox->new(\%config);
288 # mark3 adds following the delete at mark2
289 $ibx->{ref_head} = $mark3;
290 my $im = PublicInbox::V2Writable->new($ibx);
291 eval { $im->index_sync() };
292 is($@, '', 'no error from reindexing without msgmap');
293 is_deeply(\@warn, [], 'no warnings');
295 my ($min, $max) = $ibx->mm->minmax;
296 is($min, 1, 'min as expected');
297 is($max, 10, 'max as expected');
298 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
299 is_deeply($ibx->mm->msg_range(\$min, $max),
301 [1, '1@example.com' ],
302 [2, '2@example.com' ],
303 [3, '3@example.com' ],
304 [5, '5@example.com' ],
305 [6, '6@example.com' ],
306 [7, '7@example.com' ],
307 [8, '8@example.com' ],
308 [9, '9@example.com' ],
309 [10, '10@example.com' ],
310 ], 'msgmap as expected' );
314 local $SIG{__WARN__} = sub { push @warn, @_ };
315 my %config = %$ibx_config;
316 my $ibx = PublicInbox::Inbox->new(\%config);
317 # mark4 A delete of an older message
318 $ibx->{ref_head} = $mark4;
319 my $im = PublicInbox::V2Writable->new($ibx);
320 eval { $im->index_sync() };
321 is($@, '', 'no error from reindexing without msgmap');
322 is_deeply(\@warn, [], 'no warnings');
324 my ($min, $max) = $ibx->mm->minmax;
325 is($min, 1, 'min as expected');
326 is($max, 10, 'max as expected');
327 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
328 is_deeply($ibx->mm->msg_range(\$min, $max),
330 [1, '1@example.com' ],
331 [2, '2@example.com' ],
332 [3, '3@example.com' ],
333 [6, '6@example.com' ],
334 [7, '7@example.com' ],
335 [8, '8@example.com' ],
336 [9, '9@example.com' ],
337 [10, '10@example.com' ],
338 ], 'msgmap as expected' );
342 # Another incremental indexing test
343 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
345 ok(!-d $xap, 'Xapian directories removed again');
348 local $SIG{__WARN__} = sub { push @warn, @_ };
349 my %config = %$ibx_config;
350 my $ibx = PublicInbox::Inbox->new(\%config);
351 # mark2 an add and it's delete in the same index_sync
352 $ibx->{ref_head} = $mark2;
353 my $im = PublicInbox::V2Writable->new($ibx);
354 eval { $im->index_sync() };
355 is($@, '', 'no error from reindexing without msgmap');
356 is_deeply(\@warn, [], 'no warnings');
358 my ($min, $max) = $ibx->mm->minmax;
359 is($min, 1, 'min as expected');
360 is($max, 3, 'max as expected');
361 is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
362 is_deeply($ibx->mm->msg_range(\$min, $max),
364 [1, '1@example.com' ],
365 [2, '2@example.com' ],
366 [3, '3@example.com' ],
367 ], 'msgmap as expected' );
371 local $SIG{__WARN__} = sub { push @warn, @_ };
372 my %config = %$ibx_config;
373 my $ibx = PublicInbox::Inbox->new(\%config);
374 # mark3 adds following the delete at mark2
375 $ibx->{ref_head} = $mark3;
376 my $im = PublicInbox::V2Writable->new($ibx);
377 eval { $im->index_sync() };
378 is($@, '', 'no error from reindexing without msgmap');
379 is_deeply(\@warn, [], 'no warnings');
381 my ($min, $max) = $ibx->mm->minmax;
382 is($min, 1, 'min as expected');
383 is($max, 10, 'max as expected');
384 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
385 is_deeply($ibx->mm->msg_range(\$min, $max),
387 [1, '1@example.com' ],
388 [2, '2@example.com' ],
389 [3, '3@example.com' ],
390 [5, '5@example.com' ],
391 [6, '6@example.com' ],
392 [7, '7@example.com' ],
393 [8, '8@example.com' ],
394 [9, '9@example.com' ],
395 [10, '10@example.com' ],
396 ], 'msgmap as expected' );
400 local $SIG{__WARN__} = sub { push @warn, @_ };
401 my %config = %$ibx_config;
402 my $ibx = PublicInbox::Inbox->new(\%config);
403 # mark4 A delete of an older message
404 $ibx->{ref_head} = $mark4;
405 my $im = PublicInbox::V2Writable->new($ibx);
406 eval { $im->index_sync() };
407 is($@, '', 'no error from reindexing without msgmap');
408 is_deeply(\@warn, [], 'no warnings');
410 my ($min, $max) = $ibx->mm->minmax;
411 is($min, 1, 'min as expected');
412 is($max, 10, 'max as expected');
413 is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
414 is_deeply($ibx->mm->msg_range(\$min, $max),
416 [1, '1@example.com' ],
417 [2, '2@example.com' ],
418 [3, '3@example.com' ],
419 [6, '6@example.com' ],
420 [7, '7@example.com' ],
421 [8, '8@example.com' ],
422 [9, '9@example.com' ],
423 [10, '10@example.com' ],
424 ], 'msgmap as expected' );
427 my $check_rethread = sub {
430 local $SIG{__WARN__} = sub { push @warn, @_ };
431 my %config = %$ibx_config;
432 my $ibx = PublicInbox::Inbox->new(\%config);
433 my $f = $ibx->over->{dbh}->sqlite_db_filename;
434 my $over = PublicInbox::OverIdx->new($f);
435 my $dbh = $over->dbh;
436 my $non_ghost_tids = sub {
437 $dbh->selectall_arrayref(<<'');
438 SELECT tid FROM over WHERE num > 0 ORDER BY tid ASC
441 my $before = $non_ghost_tids->();
444 my $tid = PublicInbox::OverIdx::get_counter($dbh, 'thread');
445 my $nr = $dbh->do('UPDATE over SET tid = ?', undef, $tid);
446 diag "messing up all threads with tid=$tid";
448 my $v2w = PublicInbox::V2Writable->new($ibx);
450 my $pr = sub { push @pr, @_ };
451 $v2w->index_sync({reindex => 1, rethread => 1, -progress => $pr});
452 # diag "@pr"; # nobody cares
453 is_deeply(\@warn, [], 'no warnings on reindex + rethread');
455 my @n = $dbh->selectrow_array(<<EOS, undef, $tid);
456 SELECT COUNT(*) FROM over WHERE tid <= ?
458 is_deeply(\@n, [ 0 ], 'rethread dropped old threadids');
459 my $after = $non_ghost_tids->();
460 ok($after->[0]->[0] > $before->[-1]->[0],
461 'all tids greater than before');
462 is(scalar @$after, scalar @$before, 'thread count unchanged');
465 $check_rethread->('no-monster');
467 # A real example from linux-renesas-soc on lore where a 3-headed monster
468 # of a message has 3 sets of common headers. Another normal message
469 # previously existed with a single Message-ID that conflicts with one
470 # of the Message-IDs in the 3-headed monster.
473 local $SIG{__WARN__} = sub { push @warn, @_ };
474 my %config = %$ibx_config;
475 $config{indexlevel} = 'medium';
476 my $ibx = PublicInbox::Inbox->new(\%config);
477 my $im = PublicInbox::V2Writable->new($ibx);
478 my $m3 = PublicInbox::Eml->new(<<'EOF');
479 Date: Tue, 24 May 2016 14:34:22 -0700 (PDT)
480 Message-Id: <20160524.143422.552507610109476444.d@example.com>
483 Subject: Re: [PATCH v2 2/2] uno
484 From: <f@example.com>
485 In-Reply-To: <1463825855-7363-2-git-send-email-y@example.com>
486 References: <1463825855-7363-1-git-send-email-y@example.com>
487 <1463825855-7363-2-git-send-email-y@example.com>
488 Date: Wed, 25 May 2016 10:01:51 +0900
492 Subject: Re: [PATCH] dos
493 Message-ID: <20160525010150.GD7292@example.com>
494 References: <1463498133-23918-1-git-send-email-g+r@example.com>
495 In-Reply-To: <1463498133-23918-1-git-send-email-g+r@example.com>
499 Subject: [PATCH 12/13] tres
500 Date: Wed, 01 Jun 2016 01:32:35 +0300
501 Message-ID: <1923946.Jvi0TDUXFC@wasted.example.com>
502 In-Reply-To: <13205049.n7pM8utpHF@wasted.example.com>
503 References: <13205049.n7pM8utpHF@wasted.example.com>
505 Somehow we got a message with 3 sets of headers into one
506 message, could've been something broken on the archiver side.
509 my $m1 = PublicInbox::Eml->new(<<'EOF');
512 Subject: [PATCH 12/13]
513 Date: Wed, 01 Jun 2016 01:32:35 +0300
514 Message-ID: <1923946.Jvi0TDUXFC@wasted.example.com>
515 In-Reply-To: <13205049.n7pM8utpHF@wasted.example.com>
516 References: <13205049.n7pM8utpHF@wasted.example.com>
518 This is probably one of the original messages
525 eval { $im->index_sync() };
526 is($@, '', 'no error from initial indexing');
527 is_deeply(\@warn, [], 'no warnings from initial index');
528 eval { $im->index_sync({reindex=>1}) };
529 is($@, '', 'no error from reindexing after reused Message-ID (x3)');
530 is_deeply(\@warn, [], 'no warnings on reindex');
533 for my $s (qw(uno dos tres)) {
534 my $mset = $ibx->search->mset("s:$s");
535 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
536 is(scalar(@$msgs), 1, "only one result for `$s'");
537 $uniq{$msgs->[0]->{num}}++;
539 is_deeply([values %uniq], [3], 'search on different subjects');
542 # XXX: not deterministic when dealing with ambiguous messages, oh well
543 $check_rethread->('3-headed-monster once');
544 $check_rethread->('3-headed-monster twice');