]> Sergey Matveev's repositories - public-inbox.git/blob - t/v1reindex.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / v1reindex.t
1 # Copyright (C) 2018-2019 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 require './t/common.perl';
11 require_git(2.6);
12
13 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for v1reindex.t" if $@;
16 }
17 use_ok 'PublicInbox::SearchIdx';
18 use_ok 'PublicInbox::Import';
19 my $mainrepo = tempdir('pi-v1reindex-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 is(system(qw(git init -q --bare), $mainrepo), 0);
21 my $ibx_config = {
22         mainrepo => $mainrepo,
23         name => 'test-v1reindex',
24         -primary_address => 'test@example.com',
25         indexlevel => 'full',
26 };
27 my $mime = PublicInbox::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 => "hello world\n",
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::Import->new($ibx->git, undef, undef, $ibx);
43         foreach my $i (1..10) {
44                 $mime->header_set('Message-Id', "<$i\@example.com>");
45                 ok($im->add($mime), "message $i added");
46                 if ($i == 4) {
47                         $mark1 = $im->get_mark($im->{tip});
48                         $im->remove($mime);
49                         $mark2 = $im->get_mark($im->{tip});
50                 }
51         }
52
53         if ('test remove later') {
54                 $mark3 = $im->get_mark($im->{tip});
55                 $mime->header_set('Message-Id', "<5\@example.com>");
56                 $im->remove($mime);
57                 $mark4 = $im->get_mark($im->{tip});
58         }
59
60         $im->done;
61         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
62         eval { $rw->index_sync() };
63         is($@, '', 'no error from indexing');
64
65         $minmax = [ $ibx->mm->minmax ];
66         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
67         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
68         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
69
70         my ($min, $max) = @$minmax;
71         $msgmap = $ibx->mm->msg_range(\$min, $max);
72         is_deeply($msgmap, [
73                           [1, '1@example.com' ],
74                           [2, '2@example.com' ],
75                           [3, '3@example.com' ],
76                           [6, '6@example.com' ],
77                           [7, '7@example.com' ],
78                           [8, '8@example.com' ],
79                           [9, '9@example.com' ],
80                           [10, '10@example.com' ],
81                   ], 'msgmap as expected');
82 }
83
84 {
85         my %config = %$ibx_config;
86         my $ibx = PublicInbox::Inbox->new(\%config);
87         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
88         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
89         eval { $rw->index_sync({reindex => 1}) };
90         is($@, '', 'no error from reindexing');
91         $im->done;
92
93         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
94
95         my ($min, $max) = $ibx->mm->minmax;
96         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
97 }
98
99 my $xap = "$mainrepo/public-inbox/xapian".PublicInbox::Search::SCHEMA_VERSION();
100 remove_tree($xap);
101 ok(!-d $xap, 'Xapian directories removed');
102 {
103         my %config = %$ibx_config;
104         my $ibx = PublicInbox::Inbox->new(\%config);
105         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
106         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
107
108         eval { $rw->index_sync({reindex => 1}) };
109         is($@, '', 'no error from reindexing');
110         $im->done;
111         ok(-d $xap, 'Xapian directories recreated');
112
113         delete $ibx->{mm};
114         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
115         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
116
117         my ($min, $max) = $ibx->mm->minmax;
118         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
119 }
120
121 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
122 remove_tree($xap);
123 ok(!-d $xap, 'Xapian directories removed again');
124 {
125         my @warn;
126         local $SIG{__WARN__} = sub { push @warn, @_ };
127         my %config = %$ibx_config;
128         my $ibx = PublicInbox::Inbox->new(\%config);
129         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
130         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
131         eval { $rw->index_sync({reindex => 1}) };
132         is($@, '', 'no error from reindexing without msgmap');
133         is(scalar(@warn), 0, 'no warnings from reindexing');
134         $im->done;
135         ok(-d $xap, 'Xapian directories recreated');
136         delete $ibx->{mm};
137         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
138         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
139
140         my ($min, $max) = $ibx->mm->minmax;
141         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
142 }
143
144 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
145 remove_tree($xap);
146 ok(!-d $xap, 'Xapian directories removed again');
147 {
148         my @warn;
149         local $SIG{__WARN__} = sub { push @warn, @_ };
150         my %config = %$ibx_config;
151         my $ibx = PublicInbox::Inbox->new(\%config);
152         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
153         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
154         eval { $rw->index_sync({reindex => 1}) };
155         is($@, '', 'no error from reindexing without msgmap');
156         is_deeply(\@warn, [], 'no warnings');
157         $im->done;
158         ok(-d $xap, 'Xapian directories recreated');
159         delete $ibx->{mm};
160         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
161         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
162
163         my ($min, $max) = @$minmax;
164         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
165 }
166
167 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
168 remove_tree($xap);
169 ok(!-d $xap, 'Xapian directories removed again');
170 {
171         my @warn;
172         local $SIG{__WARN__} = sub { push @warn, @_ };
173         my %config = %$ibx_config;
174         $config{indexlevel} = 'medium';
175         my $ibx = PublicInbox::Inbox->new(\%config);
176         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
177         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
178         eval { $rw->index_sync({reindex => 1}) };
179         is($@, '', 'no error from reindexing without msgmap');
180         is_deeply(\@warn, [], 'no warnings');
181         $im->done;
182         ok(-d $xap, 'Xapian directories recreated');
183         delete $ibx->{mm};
184         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
185         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
186         my $mset = $ibx->search->query('hello world', {mset=>1});
187         isnt($mset->size, 0, 'got Xapian search results');
188
189         my ($min, $max) = $ibx->mm->minmax;
190         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
191 }
192
193 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
194 remove_tree($xap);
195 ok(!-d $xap, 'Xapian directories removed again');
196 {
197         my @warn;
198         local $SIG{__WARN__} = sub { push @warn, @_ };
199         my %config = %$ibx_config;
200         $config{indexlevel} = 'basic';
201         my $ibx = PublicInbox::Inbox->new(\%config);
202         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
203         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
204         eval { $rw->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         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
212         isnt($ibx->search, 'no search for basic');
213
214         my ($min, $max) = $ibx->mm->minmax;
215         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
216 }
217
218 # upgrade existing basic to medium
219 # note: changing indexlevels is not yet supported in v2,
220 # and may not be without more effort
221 # no removals
222 {
223         my @warn;
224         local $SIG{__WARN__} = sub { push @warn, @_ };
225         my %config = %$ibx_config;
226         $config{indexlevel} = 'medium';
227         my $ibx = PublicInbox::Inbox->new(\%config);
228         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
229         eval { $rw->index_sync };
230         is($@, '', 'no error from indexing');
231         is_deeply(\@warn, [], 'no warnings');
232         my $mset = $ibx->search->reopen->query('hello world', {mset=>1});
233         isnt($mset->size, 0, 'search OK after basic -> medium');
234
235         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
236
237         my ($min, $max) = $ibx->mm->minmax;
238         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
239 }
240
241 # An incremental indexing test
242 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
243 remove_tree($xap);
244 ok(!-d $xap, 'Xapian directories removed again');
245 {
246         my @warn;
247         local $SIG{__WARN__} = sub { push @warn, @_ };
248         my %config = %$ibx_config;
249         my $ibx = PublicInbox::Inbox->new(\%config);
250         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
251         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
252         # mark1 4 simple additions in the same index_sync
253         eval { $rw->index_sync({ref => $mark1}) };
254         is($@, '', 'no error from reindexing without msgmap');
255         is_deeply(\@warn, [], 'no warnings');
256         $im->done;
257         my ($min, $max) = $ibx->mm->minmax;
258         is($min, 1, 'min as expected');
259         is($max, 4, 'max as expected');
260         is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
261         is_deeply($ibx->mm->msg_range(\$min, $max),
262                   [
263                    [1, '1@example.com' ],
264                    [2, '2@example.com' ],
265                    [3, '3@example.com' ],
266                    [4, '4@example.com' ],
267                   ], 'msgmap as expected' );
268 }
269 {
270         my @warn;
271         local $SIG{__WARN__} = sub { push @warn, @_ };
272         my %config = %$ibx_config;
273         my $ibx = PublicInbox::Inbox->new(\%config);
274         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
275         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
276         # mark2 A delete separated form and add in the same index_sync
277         eval { $rw->index_sync({ref => $mark2}) };
278         is($@, '', 'no error from reindexing without msgmap');
279         is_deeply(\@warn, [], 'no warnings');
280         $im->done;
281         my ($min, $max) = $ibx->mm->minmax;
282         is($min, 1, 'min as expected');
283         is($max, 3, 'max as expected');
284         is($ibx->mm->num_highwater, 4, 'num_highwater 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                   ], 'msgmap as expected' );
291 }
292 {
293         my @warn;
294         local $SIG{__WARN__} = sub { push @warn, @_ };
295         my %config = %$ibx_config;
296         my $ibx = PublicInbox::Inbox->new(\%config);
297         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
298         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
299         # mark3 adds following the delete at mark2
300         eval { $rw->index_sync({ref => $mark3}) };
301         is($@, '', 'no error from reindexing without msgmap');
302         is_deeply(\@warn, [], 'no warnings');
303         $im->done;
304         my ($min, $max) = $ibx->mm->minmax;
305         is($min, 1, 'min as expected');
306         is($max, 10, 'max as expected');
307         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
308         is_deeply($ibx->mm->msg_range(\$min, $max),
309                   [
310                    [1, '1@example.com' ],
311                    [2, '2@example.com' ],
312                    [3, '3@example.com' ],
313                    [5, '5@example.com' ],
314                    [6, '6@example.com' ],
315                    [7, '7@example.com' ],
316                    [8, '8@example.com' ],
317                    [9, '9@example.com' ],
318                    [10, '10@example.com' ],
319                   ], 'msgmap as expected' );
320 }
321 {
322         my @warn;
323         local $SIG{__WARN__} = sub { push @warn, @_ };
324         my %config = %$ibx_config;
325         my $ibx = PublicInbox::Inbox->new(\%config);
326         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
327         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
328         # mark4 A delete of an older message
329         eval { $rw->index_sync({ref => $mark4}) };
330         is($@, '', 'no error from reindexing without msgmap');
331         is_deeply(\@warn, [], 'no warnings');
332         $im->done;
333         my ($min, $max) = $ibx->mm->minmax;
334         is($min, 1, 'min as expected');
335         is($max, 10, 'max as expected');
336         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
337         is_deeply($ibx->mm->msg_range(\$min, $max),
338                   [
339                    [1, '1@example.com' ],
340                    [2, '2@example.com' ],
341                    [3, '3@example.com' ],
342                    [6, '6@example.com' ],
343                    [7, '7@example.com' ],
344                    [8, '8@example.com' ],
345                    [9, '9@example.com' ],
346                    [10, '10@example.com' ],
347                   ], 'msgmap as expected' );
348 }
349
350
351 # Another incremental indexing test
352 ok(unlink "$mainrepo/public-inbox/msgmap.sqlite3", 'remove msgmap');
353 remove_tree($xap);
354 ok(!-d $xap, 'Xapian directories removed again');
355 {
356         my @warn;
357         local $SIG{__WARN__} = sub { push @warn, @_ };
358         my %config = %$ibx_config;
359         my $ibx = PublicInbox::Inbox->new(\%config);
360         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
361         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
362         # mark2 an add and it's delete in the same index_sync
363         eval { $rw->index_sync({ref => $mark2}) };
364         is($@, '', 'no error from reindexing without msgmap');
365         is_deeply(\@warn, [], 'no warnings');
366         $im->done;
367         my ($min, $max) = $ibx->mm->minmax;
368         is($min, 1, 'min as expected');
369         is($max, 3, 'max as expected');
370         is($ibx->mm->num_highwater, 4, 'num_highwater as expected');
371         is_deeply($ibx->mm->msg_range(\$min, $max),
372                   [
373                    [1, '1@example.com' ],
374                    [2, '2@example.com' ],
375                    [3, '3@example.com' ],
376                   ], 'msgmap as expected' );
377 }
378 {
379         my @warn;
380         local $SIG{__WARN__} = sub { push @warn, @_ };
381         my %config = %$ibx_config;
382         my $ibx = PublicInbox::Inbox->new(\%config);
383         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
384         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
385         # mark3 adds following the delete at mark2
386         eval { $rw->index_sync({ref => $mark3}) };
387         is($@, '', 'no error from reindexing without msgmap');
388         is_deeply(\@warn, [], 'no warnings');
389         $im->done;
390         my ($min, $max) = $ibx->mm->minmax;
391         is($min, 1, 'min as expected');
392         is($max, 10, 'max as expected');
393         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
394         is_deeply($ibx->mm->msg_range(\$min, $max),
395                   [
396                    [1, '1@example.com' ],
397                    [2, '2@example.com' ],
398                    [3, '3@example.com' ],
399                    [5, '5@example.com' ],
400                    [6, '6@example.com' ],
401                    [7, '7@example.com' ],
402                    [8, '8@example.com' ],
403                    [9, '9@example.com' ],
404                    [10, '10@example.com' ],
405                   ], 'msgmap as expected' );
406 }
407 {
408         my @warn;
409         local $SIG{__WARN__} = sub { push @warn, @_ };
410         my %config = %$ibx_config;
411         my $ibx = PublicInbox::Inbox->new(\%config);
412         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
413         my $rw = PublicInbox::SearchIdx->new($ibx, 1);
414         # mark4 A delete of an older message
415         eval { $rw->index_sync({ref => $mark4}) };
416         is($@, '', 'no error from reindexing without msgmap');
417         is_deeply(\@warn, [], 'no warnings');
418         $im->done;
419         my ($min, $max) = $ibx->mm->minmax;
420         is($min, 1, 'min as expected');
421         is($max, 10, 'max as expected');
422         is($ibx->mm->num_highwater, 10, 'num_highwater as expected');
423         is_deeply($ibx->mm->msg_range(\$min, $max),
424                   [
425                    [1, '1@example.com' ],
426                    [2, '2@example.com' ],
427                    [3, '3@example.com' ],
428                    [6, '6@example.com' ],
429                    [7, '7@example.com' ],
430                    [8, '8@example.com' ],
431                    [9, '9@example.com' ],
432                    [10, '10@example.com' ],
433                   ], 'msgmap as expected' );
434 }
435
436
437 done_testing();