]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2reindex.t
t/v[12]reindex.t Verify num_highwater
[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($ibx->mm->num_highwater, 4, 'num_highwater as expected');
241         is_deeply($ibx->mm->msg_range(\$min, $max),
242                   [
243                    [1, '1@example.com' ],
244                    [2, '2@example.com' ],
245                    [3, '3@example.com' ],
246                    [4, '4@example.com' ],
247                   ], 'msgmap as expected' );
248 }
249 {
250         my @warn;
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');
260         $im->done;
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),
266                   [
267                    [1, '1@example.com' ],
268                    [2, '2@example.com' ],
269                    [3, '3@example.com' ],
270                   ], 'msgmap as expected' );
271 }
272 {
273         my @warn;
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');
283         $im->done;
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),
289                   [
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' );
300 }
301 {
302         my @warn;
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');
312         $im->done;
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),
318                   [
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' );
328 }
329
330
331 # Another incremental indexing test
332 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
333 remove_tree($xap);
334 ok(!-d $xap, 'Xapian directories removed again');
335 {
336         my @warn;
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');
346         $im->done;
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),
352                   [
353                    [1, '1@example.com' ],
354                    [2, '2@example.com' ],
355                    [3, '3@example.com' ],
356                   ], 'msgmap as expected' );
357 }
358 {
359         my @warn;
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');
369         $im->done;
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),
375                   [
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' );
386 }
387 {
388         my @warn;
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');
398         $im->done;
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),
404                   [
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' );
414 }
415
416 done_testing();