]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2reindex.t
ce13a07c39c08472bfb475dcdd9d801e406c088d
[public-inbox.git] / t / v2reindex.t
1 # Copyright (C) 2018 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::Temp qw/tempdir/;
9 use File::Path qw(remove_tree);
10
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for v2reindex.t" if $@;
14 }
15 use_ok 'PublicInbox::V2Writable';
16 my $mainrepo = tempdir('pi-v2reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $ibx_config = {
18         mainrepo => $mainrepo,
19         name => 'test-v2writable',
20         version => 2,
21         -primary_address => 'test@example.com',
22         indexlevel => 'full',
23 };
24 my $mime = PublicInbox::MIME->create(
25         header => [
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',
30         ],
31         body => "hello world\n",
32 );
33 local $ENV{NPROC} = 2;
34 my $minmax;
35 my $msgmap;
36 my ($mark1, $mark2, $mark3, $mark4);
37 {
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");
45                 if ($i == 4) {
46                         $mark1 = $im0->get_mark($im0->{tip});
47                         $im->remove($mime);
48                         $mark2 = $im0->get_mark($im0->{tip});
49                 }
50         }
51
52         if ('test remove later') {
53                 $mark3 = $im0->get_mark($im0->{tip});
54                 $mime->header_set('Message-Id', "<5\@example.com>");
55                 $im->remove($mime);
56                 $mark4 = $im0->get_mark($im0->{tip});
57         }
58
59         $im->done;
60         $minmax = [ $ibx->mm->minmax ];
61         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
62         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
63
64         my ($min, $max) = @$minmax;
65         $msgmap = $ibx->mm->msg_range(\$min, $max);
66         is_deeply($msgmap, [
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');
76 }
77
78 {
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');
84         $im->done;
85
86         delete $ibx->{mm};
87         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
88
89         my ($min, $max) = $ibx->mm->minmax;
90         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
91 }
92
93 my $xap = "$mainrepo/xap".PublicInbox::Search::SCHEMA_VERSION();
94 remove_tree($xap);
95 ok(!-d $xap, 'Xapian directories removed');
96 {
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');
102         $im->done;
103         ok(-d $xap, 'Xapian directories recreated');
104
105         delete $ibx->{mm};
106         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
107
108         my ($min, $max) = $ibx->mm->minmax;
109         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
110 }
111
112 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
113 remove_tree($xap);
114 ok(!-d $xap, 'Xapian directories removed again');
115 {
116         my @warn;
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');
124         $im->done;
125         ok(-d $xap, 'Xapian directories recreated');
126         delete $ibx->{mm};
127         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
128
129         my ($min, $max) = $ibx->mm->minmax;
130         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
131 }
132
133 my %sizes;
134 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
135 remove_tree($xap);
136 ok(!-d $xap, 'Xapian directories removed again');
137 {
138         my @warn;
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');
146         $im->done;
147         ok(-d $xap, 'Xapian directories recreated');
148         delete $ibx->{mm};
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 $_ }
153
154         my ($min, $max) = $ibx->mm->minmax;
155         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
156 }
157
158 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
159 remove_tree($xap);
160 ok(!-d $xap, 'Xapian directories removed again');
161 {
162         my @warn;
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');
171         $im->done;
172         ok(-d $xap, 'Xapian directories recreated');
173         delete $ibx->{mm};
174         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
175
176         if (0) {
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');
182         }
183
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');
188
189
190         my ($min, $max) = $ibx->mm->minmax;
191         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
192 }
193
194 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
195 remove_tree($xap);
196 ok(!-d $xap, 'Xapian directories removed again');
197 {
198         my @warn;
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');
207         $im->done;
208         ok(-d $xap, 'Xapian directories recreated');
209         delete $ibx->{mm};
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');
215
216         my ($min, $max) = $ibx->mm->minmax;
217         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
218 }
219
220
221 # An incremental indexing test
222 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
223 remove_tree($xap);
224 ok(!-d $xap, 'Xapian directories removed again');
225 {
226         my @warn;
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');
236         $im->done;
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),
241                   [
242                    [1, '1@example.com' ],
243                    [2, '2@example.com' ],
244                    [3, '3@example.com' ],
245                    [4, '4@example.com' ],
246                   ], 'msgmap as expected' );
247 }
248 {
249         my @warn;
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');
259         $im->done;
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),
264                   [
265                    [1, '1@example.com' ],
266                    [2, '2@example.com' ],
267                    [3, '3@example.com' ],
268                   ], 'msgmap as expected' );
269 }
270 {
271         my @warn;
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');
281         $im->done;
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),
286                   [
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' );
297 }
298 {
299         my @warn;
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');
309         $im->done;
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),
314                   [
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' );
324 }
325
326
327 # Another incremental indexing test
328 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
329 remove_tree($xap);
330 ok(!-d $xap, 'Xapian directories removed again');
331 {
332         my @warn;
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');
342         $im->done;
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),
347                   [
348                    [1, '1@example.com' ],
349                    [2, '2@example.com' ],
350                    [3, '3@example.com' ],
351                   ], 'msgmap as expected' );
352 }
353 {
354         my @warn;
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');
364         $im->done;
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),
369                   [
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' );
380 }
381 {
382         my @warn;
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');
392         $im->done;
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),
397                   [
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' );
407 }
408
409 done_testing();