]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2reindex.t
t/v[12]reindex.t: Test that the resulting msgmap is as expected
[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 {
37         my %config = %$ibx_config;
38         my $ibx = PublicInbox::Inbox->new(\%config);
39         my $im = PublicInbox::V2Writable->new($ibx, 1);
40         foreach my $i (1..10) {
41                 $mime->header_set('Message-Id', "<$i\@example.com>");
42                 ok($im->add($mime), "message $i added");
43                 if ($i == 4) {
44                         $im->remove($mime);
45                 }
46         }
47
48         if ('test remove later') {
49                 $mime->header_set('Message-Id', "<5\@example.com>");
50                 $im->remove($mime);
51         }
52
53         $im->done;
54         $minmax = [ $ibx->mm->minmax ];
55         ok(defined $minmax->[0] && defined $minmax->[1], 'minmax defined');
56         is_deeply($minmax, [ 1, 10 ], 'minmax as expected');
57
58         my ($min, $max) = @$minmax;
59         $msgmap = $ibx->mm->msg_range(\$min, $max);
60         is_deeply($msgmap, [
61                           [1, '1@example.com' ],
62                           [2, '2@example.com' ],
63                           [3, '3@example.com' ],
64                           [6, '6@example.com' ],
65                           [7, '7@example.com' ],
66                           [8, '8@example.com' ],
67                           [9, '9@example.com' ],
68                           [10, '10@example.com' ],
69                   ], 'msgmap as expected');
70 }
71
72 {
73         my %config = %$ibx_config;
74         my $ibx = PublicInbox::Inbox->new(\%config);
75         my $im = PublicInbox::V2Writable->new($ibx, 1);
76         eval { $im->index_sync({reindex => 1}) };
77         is($@, '', 'no error from reindexing');
78         $im->done;
79
80         delete $ibx->{mm};
81         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
82
83         my ($min, $max) = $ibx->mm->minmax;
84         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
85 }
86
87 my $xap = "$mainrepo/xap".PublicInbox::Search::SCHEMA_VERSION();
88 remove_tree($xap);
89 ok(!-d $xap, 'Xapian directories removed');
90 {
91         my %config = %$ibx_config;
92         my $ibx = PublicInbox::Inbox->new(\%config);
93         my $im = PublicInbox::V2Writable->new($ibx, 1);
94         eval { $im->index_sync({reindex => 1}) };
95         is($@, '', 'no error from reindexing');
96         $im->done;
97         ok(-d $xap, 'Xapian directories recreated');
98
99         delete $ibx->{mm};
100         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
101
102         my ($min, $max) = $ibx->mm->minmax;
103         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
104 }
105
106 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
107 remove_tree($xap);
108 ok(!-d $xap, 'Xapian directories removed again');
109 {
110         my @warn;
111         local $SIG{__WARN__} = sub { push @warn, @_ };
112         my %config = %$ibx_config;
113         my $ibx = PublicInbox::Inbox->new(\%config);
114         my $im = PublicInbox::V2Writable->new($ibx, 1);
115         eval { $im->index_sync({reindex => 1}) };
116         is($@, '', 'no error from reindexing without msgmap');
117         is(scalar(@warn), 0, 'no warnings from reindexing');
118         $im->done;
119         ok(-d $xap, 'Xapian directories recreated');
120         delete $ibx->{mm};
121         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
122
123         my ($min, $max) = $ibx->mm->minmax;
124         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
125 }
126
127 my %sizes;
128 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
129 remove_tree($xap);
130 ok(!-d $xap, 'Xapian directories removed again');
131 {
132         my @warn;
133         local $SIG{__WARN__} = sub { push @warn, @_ };
134         my %config = %$ibx_config;
135         my $ibx = PublicInbox::Inbox->new(\%config);
136         my $im = PublicInbox::V2Writable->new($ibx, 1);
137         eval { $im->index_sync({reindex => 1}) };
138         is($@, '', 'no error from reindexing without msgmap');
139         is_deeply(\@warn, [], 'no warnings');
140         $im->done;
141         ok(-d $xap, 'Xapian directories recreated');
142         delete $ibx->{mm};
143         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
144         my $mset = $ibx->search->query('"hello world"', {mset=>1});
145         isnt($mset->size, 0, "phrase search succeeds on indexlevel=full");
146         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
147
148         my ($min, $max) = $ibx->mm->minmax;
149         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
150 }
151
152 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
153 remove_tree($xap);
154 ok(!-d $xap, 'Xapian directories removed again');
155 {
156         my @warn;
157         local $SIG{__WARN__} = sub { push @warn, @_ };
158         my %config = %$ibx_config;
159         $config{indexlevel} = 'medium';
160         my $ibx = PublicInbox::Inbox->new(\%config);
161         my $im = PublicInbox::V2Writable->new($ibx);
162         eval { $im->index_sync({reindex => 1}) };
163         is($@, '', 'no error from reindexing without msgmap');
164         is_deeply(\@warn, [], 'no warnings');
165         $im->done;
166         ok(-d $xap, 'Xapian directories recreated');
167         delete $ibx->{mm};
168         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
169
170         if (0) {
171                 # not sure why, but Xapian seems to fallback to terms and
172                 # phrase searches still work
173                 delete $ibx->{search};
174                 my $mset = $ibx->search->query('"hello world"', {mset=>1});
175                 is($mset->size, 0, 'phrase search does not work on medium');
176         }
177
178         my $mset = $ibx->search->query('hello world', {mset=>1});
179         isnt($mset->size, 0, "normal search works on indexlevel=medium");
180         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
181         ok($sizes{full} > $sizes{medium}, 'medium is smaller than full');
182
183
184         my ($min, $max) = $ibx->mm->minmax;
185         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
186 }
187
188 ok(unlink "$mainrepo/msgmap.sqlite3", 'remove msgmap');
189 remove_tree($xap);
190 ok(!-d $xap, 'Xapian directories removed again');
191 {
192         my @warn;
193         local $SIG{__WARN__} = sub { push @warn, @_ };
194         my %config = %$ibx_config;
195         $config{indexlevel} = 'basic';
196         my $ibx = PublicInbox::Inbox->new(\%config);
197         my $im = PublicInbox::V2Writable->new($ibx);
198         eval { $im->index_sync({reindex => 1}) };
199         is($@, '', 'no error from reindexing without msgmap');
200         is_deeply(\@warn, [], 'no warnings');
201         $im->done;
202         ok(-d $xap, 'Xapian directories recreated');
203         delete $ibx->{mm};
204         is_deeply([ $ibx->mm->minmax ], $minmax, 'minmax unchanged');
205         my $mset = $ibx->search->query('hello', {mset=>1});
206         is($mset->size, 0, "search fails on indexlevel='basic'");
207         for (<"$xap/*/*">) { $sizes{$ibx->{indexlevel}} += -s _ if -f $_ }
208         ok($sizes{medium} > $sizes{basic}, 'basic is smaller than medium');
209
210         my ($min, $max) = $ibx->mm->minmax;
211         is_deeply($ibx->mm->msg_range(\$min, $max), $msgmap, 'msgmap unchanged');
212 }
213
214 done_testing();