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