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