]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2reindex.t
t/*.t: use Email::MIME->create over PublicInbox::MIME->create
[public-inbox.git] / t / v2reindex.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Path qw(remove_tree);
9 use PublicInbox::TestCommon;
10 require_git(2.6);
11 require_mods(qw(DBD::SQLite Search::Xapian Email::MIME));
12 use_ok 'PublicInbox::V2Writable';
13 my ($inboxdir, $for_destroy) = tmpdir();
14 my $ibx_config = {
15         inboxdir => $inboxdir,
16         name => 'test-v2writable',
17         version => 2,
18         -primary_address => 'test@example.com',
19         indexlevel => 'full',
20 };
21 my $agpl = do {
22         open my $fh, '<', 'COPYING' or die "can't open COPYING: $!";
23         local $/;
24         <$fh>;
25 };
26 my $phrase = q("defending all users' freedom");
27 my $mime = Email::MIME->create(
28         header => [
29                 From => 'a@example.com',
30                 To => 'test@example.com',
31                 Subject => 'this is a subject',
32                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
33         ],
34         body => $agpl,
35 );
36 my $minmax;
37 my $msgmap;
38 my ($mark1, $mark2, $mark3, $mark4);
39 {
40         my %config = %$ibx_config;
41         my $ibx = PublicInbox::Inbox->new(\%config);
42         my $im = PublicInbox::V2Writable->new($ibx, {nproc => 1});
43         my $im0 = $im->importer();
44         foreach my $i (1..10) {
45                 $mime->header_set('Message-Id', "<$i\@example.com>");
46                 ok($im->add($mime), "message $i added");
47                 if ($i == 4) {
48                         $mark1 = $im0->get_mark($im0->{tip});
49                         $im->remove($mime);
50                         $mark2 = $im0->get_mark($im0->{tip});
51                 }
52         }
53
54         if ('test remove later') {
55                 $mark3 = $im0->get_mark($im0->{tip});
56                 $mime->header_set('Message-Id', "<5\@example.com>");
57                 $im->remove($mime);
58                 $mark4 = $im0->get_mark($im0->{tip});
59         }
60
61         $im->done;
62         $minmax = [ $ibx->mm->minmax ];
63         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
64         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
65         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
66
67         my ($min, $max) = @$minmax;
68         $msgmap = $ibx->mm->msg_range(\$min, $max);
69         is_deeply($msgmap, [
70                           [1, '1@example.com' ],
71                           [2, '2@example.com' ],
72                           [3, '3@example.com' ],
73                           [6, '6@example.com' ],
74                           [7, '7@example.com' ],
75                           [8, '8@example.com' ],
76                           [9, '9@example.com' ],
77                           [10, '10@example.com' ],
78                   ], 'msgmap as expected');
79 }
80
81 {
82         my %config = %$ibx_config;
83         my $ibx = PublicInbox::Inbox->new(\%config);
84         my $im = PublicInbox::V2Writable->new($ibx, 1);
85         eval { $im->index_sync({reindex => 1}) };
86         is($@, '', 'no error from reindexing');
87         $im->done;
88
89         delete $ibx->{mm};
90         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
91         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
92
93         my ($min, $max) = $ibx->mm->minmax;
94         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
95 }
96
97 my $xap = "$inboxdir/xap".PublicInbox::Search::SCHEMA_VERSION();
98 remove_tree($xap);
99 ok(!-d $xap, 'Xapian directories removed');
100 {
101         my %config = %$ibx_config;
102         my $ibx = PublicInbox::Inbox->new(\%config);
103         my $im = PublicInbox::V2Writable->new($ibx, 1);
104         eval { $im->index_sync({reindex => 1}) };
105         is($@, '', 'no error from reindexing');
106         $im->done;
107         ok(-d $xap, 'Xapian directories recreated');
108
109         delete $ibx->{mm};
110         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
111         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
112
113         my ($min, $max) = $ibx->mm->minmax;
114         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
115 }
116
117 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
118 remove_tree($xap);
119 ok(!-d $xap, 'Xapian directories removed again');
120 {
121         my @warn;
122         local $SIG{__WARN__} = sub { push @warn, @_ };
123         my %config = %$ibx_config;
124         my $ibx = PublicInbox::Inbox->new(\%config);
125         my $im = PublicInbox::V2Writable->new($ibx, 1);
126         eval { $im->index_sync({reindex => 1}) };
127         is($@, '', 'no error from reindexing without msgmap');
128         is(scalar(@warn), 0, 'no warnings from reindexing');
129         $im->done;
130         ok(-d $xap, 'Xapian directories recreated');
131         delete $ibx->{mm};
132         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
133         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
134
135         my ($min, $max) = $ibx->mm->minmax;
136         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
137 }
138
139 my %sizes;
140 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
141 remove_tree($xap);
142 ok(!-d $xap, 'Xapian directories removed again');
143 {
144         my @warn;
145         local $SIG{__WARN__} = sub { push @warn, @_ };
146         my %config = %$ibx_config;
147         my $ibx = PublicInbox::Inbox->new(\%config);
148         my $im = PublicInbox::V2Writable->new($ibx, 1);
149         eval { $im->index_sync({reindex => 1}) };
150         is($@, '', 'no error from reindexing without msgmap');
151         is_deeply(\@warn, [], 'no warnings');
152         $im->done;
153         ok(-d $xap, 'Xapian directories recreated');
154         delete $ibx->{mm};
155         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
156         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
157         my $mset = $ibx->search->query($phrase, {mset=>1});
158         isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
159         for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
160
161         my ($min, $max) = $ibx->mm->minmax;
162         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
163 }
164
165 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
166 remove_tree($xap);
167 ok(!-d $xap, 'Xapian directories removed again');
168 {
169         my @warn;
170         local $SIG{__WARN__} = sub { push @warn, @_ };
171         my %config = %$ibx_config;
172         $config{indexlevel} = 'medium';
173         my $ibx = PublicInbox::Inbox->new(\%config);
174         my $im = PublicInbox::V2Writable->new($ibx);
175         eval { $im->index_sync({reindex => 1}) };
176         is($@, '', 'no error from reindexing without msgmap');
177         is_deeply(\@warn, [], 'no warnings');
178         $im->done;
179         ok(-d $xap, 'Xapian directories recreated');
180         delete $ibx->{mm};
181         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
182         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
183
184         if (0) {
185                 # not sure why, but Xapian seems to fallback to terms and
186                 # phrase searches still work
187                 delete $ibx->{search};
188                 my $mset = $ibx->search->query($phrase, {mset=>1});
189                 is($mset->size, 0, 'phrase search does not work on medium');
190         }
191         my $words = $phrase;
192         $words =~ tr/"'//d;
193         my $mset = $ibx->search->query($words, {mset=>1});
194         isnt($mset->size, 0, "normal search works on indexlevel=medium");
195         for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
196
197         ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
198
199
200         my ($min, $max) = $ibx->mm->minmax;
201         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
202 }
203
204 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
205 remove_tree($xap);
206 ok(!-d $xap, 'Xapian directories removed again');
207 {
208         my @warn;
209         local $SIG{__WARN__} = sub { push @warn, @_ };
210         my %config = %$ibx_config;
211         $config{indexlevel} = 'basic';
212         my $ibx = PublicInbox::Inbox->new(\%config);
213         my $im = PublicInbox::V2Writable->new($ibx);
214         eval { $im->index_sync({reindex => 1}) };
215         is($@, '', 'no error from reindexing without msgmap');
216         is_deeply(\@warn, [], 'no warnings');
217         $im->done;
218         ok(-d $xap, 'Xapian directories recreated');
219         delete $ibx->{mm};
220         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
221         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
222
223         isnt($ibx->search, 'no search for basic');
224
225         for (glob("$xap/*/*")) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
226         ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');
227
228         my ($min, $max) = $ibx->mm->minmax;
229         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
230 }
231
232
233 # An incremental indexing test
234 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
235 remove_tree($xap);
236 ok(!-d $xap, 'Xapian directories removed again');
237 {
238         my @warn;
239         local $SIG{__WARN__} = sub { push @warn, @_ };
240         my %config = %$ibx_config;
241         my $ibx = PublicInbox::Inbox->new(\%config);
242         # mark1 4 simple additions in the same index_sync
243         $ibx->{ref_head} = $mark1;
244         my $im = PublicInbox::V2Writable->new($ibx);
245         eval { $im->index_sync() };
246         is($@, '', 'no error from reindexing without msgmap');
247         is_deeply(\@warn, [], 'no warnings');
248         $im->done;
249         my ($min, $max) = $ibx->mm->minmax;
250         is($min, 1, 'min as expected');
251         is($max, 4, 'max as expected');
252         is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
253         is_deeply($ibx->mm->msg_range(\$min, $max),
254                   [
255                    [1, '1@example.com' ],
256                    [2, '2@example.com' ],
257                    [3, '3@example.com' ],
258                    [4, '4@example.com' ],
259                   ], 'msgmap as expected' );
260 }
261 {
262         my @warn;
263         local $SIG{__WARN__} = sub { push @warn, @_ };
264         my %config = %$ibx_config;
265         my $ibx = PublicInbox::Inbox->new(\%config);
266         # mark2 A delete separated from an add in the same index_sync
267         $ibx->{ref_head} = $mark2;
268         my $im = PublicInbox::V2Writable->new($ibx);
269         eval { $im->index_sync() };
270         is($@, '', 'no error from reindexing without msgmap');
271         is_deeply(\@warn, [], 'no warnings');
272         $im->done;
273         my ($min, $max) = $ibx->mm->minmax;
274         is($min, 1, 'min as expected');
275         is($max, 3, 'max as expected');
276         is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
277         is_deeply($ibx->mm->msg_range(\$min, $max),
278                   [
279                    [1, '1@example.com' ],
280                    [2, '2@example.com' ],
281                    [3, '3@example.com' ],
282                   ], 'msgmap as expected' );
283 }
284 {
285         my @warn;
286         local $SIG{__WARN__} = sub { push @warn, @_ };
287         my %config = %$ibx_config;
288         my $ibx = PublicInbox::Inbox->new(\%config);
289         # mark3 adds following the delete at mark2
290         $ibx->{ref_head} = $mark3;
291         my $im = PublicInbox::V2Writable->new($ibx);
292         eval { $im->index_sync() };
293         is($@, '', 'no error from reindexing without msgmap');
294         is_deeply(\@warn, [], 'no warnings');
295         $im->done;
296         my ($min, $max) = $ibx->mm->minmax;
297         is($min, 1, 'min as expected');
298         is($max, 10, 'max as expected');
299         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
300         is_deeply($ibx->mm->msg_range(\$min, $max),
301                   [
302                    [1, '1@example.com' ],
303                    [2, '2@example.com' ],
304                    [3, '3@example.com' ],
305                    [5, '5@example.com' ],
306                    [6, '6@example.com' ],
307                    [7, '7@example.com' ],
308                    [8, '8@example.com' ],
309                    [9, '9@example.com' ],
310                    [10, '10@example.com' ],
311                   ], 'msgmap as expected' );
312 }
313 {
314         my @warn;
315         local $SIG{__WARN__} = sub { push @warn, @_ };
316         my %config = %$ibx_config;
317         my $ibx = PublicInbox::Inbox->new(\%config);
318         # mark4 A delete of an older message
319         $ibx->{ref_head} = $mark4;
320         my $im = PublicInbox::V2Writable->new($ibx);
321         eval { $im->index_sync() };
322         is($@, '', 'no error from reindexing without msgmap');
323         is_deeply(\@warn, [], 'no warnings');
324         $im->done;
325         my ($min, $max) = $ibx->mm->minmax;
326         is($min, 1, 'min as expected');
327         is($max, 10, 'max as expected');
328         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
329         is_deeply($ibx->mm->msg_range(\$min, $max),
330                   [
331                    [1, '1@example.com' ],
332                    [2, '2@example.com' ],
333                    [3, '3@example.com' ],
334                    [6, '6@example.com' ],
335                    [7, '7@example.com' ],
336                    [8, '8@example.com' ],
337                    [9, '9@example.com' ],
338                    [10, '10@example.com' ],
339                   ], 'msgmap as expected' );
340 }
341
342
343 # Another incremental indexing test
344 ok(unlink "$inboxdir/msgmap.sqlite3", 'remove msgmap');
345 remove_tree($xap);
346 ok(!-d $xap, 'Xapian directories removed again');
347 {
348         my @warn;
349         local $SIG{__WARN__} = sub { push @warn, @_ };
350         my %config = %$ibx_config;
351         my $ibx = PublicInbox::Inbox->new(\%config);
352         # mark2 an add and it's delete in the same index_sync
353         $ibx->{ref_head} = $mark2;
354         my $im = PublicInbox::V2Writable->new($ibx);
355         eval { $im->index_sync() };
356         is($@, '', 'no error from reindexing without msgmap');
357         is_deeply(\@warn, [], 'no warnings');
358         $im->done;
359         my ($min, $max) = $ibx->mm->minmax;
360         is($min, 1, 'min as expected');
361         is($max, 3, 'max as expected');
362         is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
363         is_deeply($ibx->mm->msg_range(\$min, $max),
364                   [
365                    [1, '1@example.com' ],
366                    [2, '2@example.com' ],
367                    [3, '3@example.com' ],
368                   ], 'msgmap as expected' );
369 }
370 {
371         my @warn;
372         local $SIG{__WARN__} = sub { push @warn, @_ };
373         my %config = %$ibx_config;
374         my $ibx = PublicInbox::Inbox->new(\%config);
375         # mark3 adds following the delete at mark2
376         $ibx->{ref_head} = $mark3;
377         my $im = PublicInbox::V2Writable->new($ibx);
378         eval { $im->index_sync() };
379         is($@, '', 'no error from reindexing without msgmap');
380         is_deeply(\@warn, [], 'no warnings');
381         $im->done;
382         my ($min, $max) = $ibx->mm->minmax;
383         is($min, 1, 'min as expected');
384         is($max, 10, 'max as expected');
385         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
386         is_deeply($ibx->mm->msg_range(\$min, $max),
387                   [
388                    [1, '1@example.com' ],
389                    [2, '2@example.com' ],
390                    [3, '3@example.com' ],
391                    [5, '5@example.com' ],
392                    [6, '6@example.com' ],
393                    [7, '7@example.com' ],
394                    [8, '8@example.com' ],
395                    [9, '9@example.com' ],
396                    [10, '10@example.com' ],
397                   ], 'msgmap as expected' );
398 }
399 {
400         my @warn;
401         local $SIG{__WARN__} = sub { push @warn, @_ };
402         my %config = %$ibx_config;
403         my $ibx = PublicInbox::Inbox->new(\%config);
404         # mark4 A delete of an older message
405         $ibx->{ref_head} = $mark4;
406         my $im = PublicInbox::V2Writable->new($ibx);
407         eval { $im->index_sync() };
408         is($@, '', 'no error from reindexing without msgmap');
409         is_deeply(\@warn, [], 'no warnings');
410         $im->done;
411         my ($min, $max) = $ibx->mm->minmax;
412         is($min, 1, 'min as expected');
413         is($max, 10, 'max as expected');
414         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
415         is_deeply($ibx->mm->msg_range(\$min, $max),
416                   [
417                    [1, '1@example.com' ],
418                    [2, '2@example.com' ],
419                    [3, '3@example.com' ],
420                    [6, '6@example.com' ],
421                    [7, '7@example.com' ],
422                    [8, '8@example.com' ],
423                    [9, '9@example.com' ],
424                    [10, '10@example.com' ],
425                   ], 'msgmap as expected' );
426 }
427
428 # A real example from linux-renesas-soc on lore where a 3-headed monster
429 # of a message has 3 sets of common headers.  Another normal message
430 # previously existed with a single Message-ID that conflicts with one
431 # of the Message-IDs in the 3-headed monster.
432 {
433         my @warn;
434         local $SIG{__WARN__} = sub { push @warn, @_ };
435         my %config = %$ibx_config;
436         $config{indexlevel} = 'medium';
437         my $ibx = PublicInbox::Inbox->new(\%config);
438         my $im = PublicInbox::V2Writable->new($ibx);
439         my $m3 = PublicInbox::MIME->new(<<'EOF');
440 Date: Tue, 24 May 2016 14:34:22 -0700 (PDT)
441 Message-Id: <20160524.143422.552507610109476444.d@example.com>
442 To: t@example.com
443 Cc: c@example.com
444 Subject: Re: [PATCH v2 2/2] uno
445 From: <f@example.com>
446 In-Reply-To: <1463825855-7363-2-git-send-email-y@example.com>
447 References: <1463825855-7363-1-git-send-email-y@example.com>
448         <1463825855-7363-2-git-send-email-y@example.com>
449 Date: Wed, 25 May 2016 10:01:51 +0900
450 From: h@example.com
451 To: g@example.com
452 Cc: m@example.com
453 Subject: Re: [PATCH] dos
454 Message-ID: <20160525010150.GD7292@example.com>
455 References: <1463498133-23918-1-git-send-email-g+r@example.com>
456 In-Reply-To: <1463498133-23918-1-git-send-email-g+r@example.com>
457 From: s@example.com
458 To: h@example.com
459 Cc: m@example.com
460 Subject: [PATCH 12/13] tres
461 Date: Wed, 01 Jun 2016 01:32:35 +0300
462 Message-ID: <1923946.Jvi0TDUXFC@wasted.example.com>
463 In-Reply-To: <13205049.n7pM8utpHF@wasted.example.com>
464 References: <13205049.n7pM8utpHF@wasted.example.com>
465
466 Somehow we got a message with 3 sets of headers into one
467 message, could've been something broken on the archiver side.
468 EOF
469
470         my $m1 = PublicInbox::MIME->new(<<'EOF');
471 From: a@example.com
472 To: t@example.com
473 Subject: [PATCH 12/13]
474 Date: Wed, 01 Jun 2016 01:32:35 +0300
475 Message-ID: <1923946.Jvi0TDUXFC@wasted.example.com>
476 In-Reply-To: <13205049.n7pM8utpHF@wasted.example.com>
477 References: <13205049.n7pM8utpHF@wasted.example.com>
478
479 This is probably one of the original messages
480
481 EOF
482         $im->add($m1);
483         $im->add($m3);
484         $im->done;
485         remove_tree($xap);
486         eval { $im->index_sync() };
487         is($@, '', 'no error from initial indexing');
488         is_deeply(\@warn, [], 'no warnings from initial index');
489         eval { $im->index_sync({reindex=>1}) };
490         is($@, '', 'no error from reindexing after reused Message-ID (x3)');
491         is_deeply(\@warn, [], 'no warnings on reindex');
492
493         my %uniq;
494         for my $s (qw(uno dos tres)) {
495                 my $msgs = $ibx->search->query("s:$s");
496                 is(scalar(@$msgs), 1, "only one result for `$s'");
497                 $uniq{$msgs->[0]->{num}}++;
498         }
499         is_deeply([values %uniq], [3], 'search on different subjects');
500 }
501
502 done_testing();