]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
extindex: support --rethread and content bifurcation
[public-inbox.git] / t / extsearch.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use PublicInbox::Search;
9 use PublicInbox::InboxWritable;
10 use Fcntl qw(:seek);
11 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
12 require_git(2.6);
13 require_mods(qw(DBD::SQLite Search::Xapian));
14 use_ok 'PublicInbox::ExtSearch';
15 use_ok 'PublicInbox::ExtSearchIdx';
16 use_ok 'PublicInbox::OverIdx';
17 my $sock = tcp_server();
18 my $host_port = $sock->sockhost . ':' . $sock->sockport;
19 my ($home, $for_destroy) = tmpdir();
20 local $ENV{HOME} = $home;
21 mkdir "$home/.public-inbox" or BAIL_OUT $!;
22 my $cfg_path = "$home/.public-inbox/config";
23 open my $fh, '>', $cfg_path or BAIL_OUT $!;
24 print $fh <<EOF or BAIL_OUT $!;
25 [publicinboxMda]
26         spamcheck = none
27 EOF
28 close $fh or BAIL_OUT $!;
29 my $v2addr = 'v2test@example.com';
30 my $v1addr = 'v1test@example.com';
31 ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
32         "$home/v2test", 'http://example.com/v2test', $v2addr ]), 'v2test init');
33 my $env = { ORIGINAL_RECIPIENT => $v2addr };
34 my $eml = eml_load('t/utf8.eml');
35
36 $eml->header_set('List-Id', '<v2.example.com>');
37 open($fh, '+>', undef) or BAIL_OUT $!;
38 $fh->autoflush(1);
39 print $fh $eml->as_string or BAIL_OUT $!;
40 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
41
42 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
43
44 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
45         'http://example.com/v1test', $v1addr ]), 'v1test init');
46
47 $eml->header_set('List-Id', '<v1.example.com>');
48 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
49 truncate($fh, 0) or BAIL_OUT $!;
50 print $fh $eml->as_string or BAIL_OUT $!;
51 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
52
53 $env = { ORIGINAL_RECIPIENT => $v1addr };
54 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
55
56 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
57
58 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
59 {
60         my $es = PublicInbox::ExtSearch->new("$home/extindex");
61         ok($es->has_threadid, '->has_threadid');
62 }
63
64 { # TODO: -extindex should write this to config
65         open $fh, '>>', $cfg_path or BAIL_OUT $!;
66         print $fh <<EOF or BAIL_OUT $!;
67 ; for ->ALL
68 [extindex "all"]
69         topdir = $home/extindex
70 EOF
71         close $fh or BAIL_OUT $!;
72
73         my $pi_cfg = PublicInbox::Config->new;
74         $pi_cfg->fill_all;
75         ok($pi_cfg->ALL, '->ALL');
76         my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
77         my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
78         is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
79                 '->nntp_xref_for');
80 }
81
82 SKIP: {
83         require_mods(qw(Net::NNTP), 1);
84         my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
85         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
86         my $td = start_script($cmd, undef, { 3 => $sock });
87         my $n = Net::NNTP->new($host_port);
88         my @xp = $n->xpath('<testmessage@example.com>');
89         is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
90         $n->group('v1.example');
91         my $res = $n->head(1);
92         @$res = grep(/^Xref: /, @$res);
93         like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
94 }
95
96 my $es = PublicInbox::ExtSearch->new("$home/extindex");
97 {
98         my $smsg = $es->over->get_art(1);
99         ok($smsg, 'got first article');
100         is($es->over->get_art(2), undef, 'only one added');
101         my $xref3 = $es->over->get_xref3(1);
102         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
103         like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
104         is(scalar(@$xref3), 2, 'only to entries');
105 }
106
107 if ('inbox edited') {
108         my ($in, $out, $err);
109         $in = $out = $err = '';
110         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
111         my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
112         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
113         ok(run_script($cmd, $env, $opt), '-edit');
114         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
115                 'extindex again');
116         like($err, qr/discontiguous range/, 'warned about discontiguous range');
117         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
118         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
119         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
120         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
121         my $eml2 = $es->smsg_eml($msg2);
122         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
123         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
124         my $eml1 = $es->smsg_eml($msg1);
125         like($eml1->body, qr/test message/, 'original body in #1');
126         my $x1 = $es->over->get_xref3(1);
127         my $x2 = $es->over->get_xref3(2);
128         is(scalar(@$x1), 1, 'original only has one xref3');
129         is(scalar(@$x2), 1, 'new message has one xref3');
130         isnt($x1->[0], $x2->[0], 'xref3 differs');
131
132         my $mset = $es->mset('b:"BEST MSG"');
133         is($mset->size, 1, 'new message found');
134         $mset = $es->mset('b:"test message"');
135         is($mset->size, 1, 'old message found');
136         delete @$es{qw(git over xdb)}; # fork preparation
137
138         my $pi_cfg = PublicInbox::Config->new;
139         $pi_cfg->fill_all;
140         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
141                 '2 results in ->ALL');
142         my $res = {};
143         my $nr = 0;
144         $pi_cfg->each_inbox(sub {
145                 $nr++;
146                 my ($ibx) = @_;
147                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
148                 my $mset = $ibx->isrch->mset('s:Testing');
149                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
150         });
151         is($nr, 2, 'two inboxes');
152         my $exp = {};
153         for my $v (qw(v1 v2)) {
154                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
155                 my $smsg = $ibx->over->get_art(1);
156                 $smsg->psgi_cull;
157                 $exp->{"$v.example"} = [ $smsg ];
158         }
159         is_deeply($res, $exp, 'isearch limited results');
160         $pi_cfg = $res = $exp = undef;
161
162         open my $rmfh, '+>', undef or BAIL_OUT $!;
163         $rmfh->autoflush(1);
164         print $rmfh $eml2->as_string or BAIL_OUT $!;
165         seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
166         $opt->{0} = $rmfh;
167         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
168
169         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
170                 'extindex after rm');
171         is($es->over->get_art(2), undef, 'doc #2 gone');
172         $mset = $es->mset('b:"BEST MSG"');
173         is($mset->size, 0, 'new message gone');
174 }
175
176 my $misc = $es->misc;
177 my @it = $misc->mset('')->items;
178 is(scalar(@it), 2, 'two inboxes');
179 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
180 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
181
182 my $cfg = PublicInbox::Config->new;
183 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
184 my $f = "$home/extindex/ei$schema_version/over.sqlite3";
185 my $oidx = PublicInbox::OverIdx->new($f);
186 if ('inject w/o indexing') {
187         use PublicInbox::Import;
188         my $v1ibx = $cfg->lookup_name('v1test');
189         my $last_v1_commit = $v1ibx->mm->last_commit;
190         my $v2ibx = $cfg->lookup_name('v2test');
191         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
192         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
193         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
194         is($last_v2_commit, $cmt, 'v2 index up-to-date');
195
196         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
197         $v2im->{lock_path} = undef;
198         $v2im->{path_type} = 'v2';
199         $v2im->add(eml_load('t/mda-mime.eml'));
200         $v2im->done;
201         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
202         isnt($tip, $cmt, '0.git v2 updated');
203
204         # inject a message w/o updating index
205         rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
206                 BAIL_OUT $!;
207         open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
208         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
209                 BAIL_OUT '-mda';
210         rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
211                 BAIL_OUT $!;
212
213         my ($in, $out, $err);
214         $in = $out = $err = '';
215         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
216         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
217                 undef, undef), 'extindex noop');
218         $es->{xdb}->reopen;
219         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
220         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
221         $mset = $es->mset('mid:multipart-html-sucks@11');
222         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
223         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
224
225         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
226         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
227                 $last_v2_commit, '-index v2 worked');
228         ok(run_script([qw(-extindex --all), "$home/extindex"]),
229                 'extindex updates');
230
231         $es->{xdb}->reopen;
232         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
233         is($mset->size, 1, 'got v1 message');
234         $mset = $es->mset('mid:multipart-html-sucks@11');
235         is($mset->size, 1, 'got v2 message');
236 }
237
238 if ('reindex catches missed messages') {
239         my $v2ibx = $cfg->lookup_name('v2test');
240         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
241         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
242         my $eml = eml_load('t/data/0001.patch');
243         $im->add($eml);
244         $im->done;
245         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
246         isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
247         $oidx->dbh;
248         my $uv = $v2ibx->uidvalidity;
249         my $lc_key = "lc-v2:v2.example//$uv;0";
250         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
251                 'update lc-v2 meta, old is as expected');
252         my $max = $oidx->max;
253         $oidx->dbh_close;
254         ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
255                 '-extindex noop');
256         is($oidx->max, $max, '->max unchanged');
257         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
258         $oidx->dbh_close;
259         my $opt = { 2 => \(my $err = '') };
260         ok(run_script([qw(-extindex --reindex), "$home/extindex",
261                         $v2ibx->{inboxdir}], undef, $opt),
262                         '--reindex for unseen');
263         is($oidx->max, $max + 1, '->max bumped');
264         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 stays unchanged');
265         my @err = split(/^/, $err);
266         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
267         like($err[0], qr/I: reindex_unseen/, 'got reindex_unseen message');
268         my $new = $oidx->get_art($max + 1);
269         is($new->{subject}, $eml->header('Subject'), 'new message added');
270
271         $es->{xdb}->reopen;
272         my $mset = $es->mset("mid:$new->{mid}");
273         is($mset->size, 1, 'previously unseen, now indexed in Xapian');
274
275         ok($im->remove($eml), 'remove new message from v2 inbox');
276         $im->done;
277         my $cmt_c = $v2ibx->mm->last_commit_xap($schema_version, 0);
278         is($oidx->eidx_meta($lc_key, $cmt_c), $cmt_b,
279                 'bump lc-v2 meta again to skip v2 remove');
280         $err = '';
281         $oidx->dbh_close;
282         ok(run_script([qw(-extindex --reindex), "$home/extindex",
283                         $v2ibx->{inboxdir}], undef, $opt),
284                         '--reindex for stale');
285         @err = split(/^/, $err);
286         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
287         like($err[0], qr/\(#$new->{num}\): stale/, 'got stale message warning');
288         is($oidx->get_art($new->{num}), undef,
289                 'stale message gone from over');
290         is_deeply($oidx->get_xref3($new->{num}), [],
291                 'stale message has no xref3');
292         $es->{xdb}->reopen;
293         $mset = $es->mset("mid:$new->{mid}");
294         is($mset->size, 0, 'stale mid gone Xapian');
295 }
296
297 if ('reindex catches content bifurcation') {
298         use PublicInbox::MID qw(mids);
299         my $v2ibx = $cfg->lookup_name('v2test');
300         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
301         my $eml = eml_load('t/data/message_embed.eml');
302         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
303         $im->add($eml);
304         $im->done;
305         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
306         my $uv = $v2ibx->uidvalidity;
307         my $lc_key = "lc-v2:v2.example//$uv;0";
308         $oidx->dbh;
309         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
310                 'update lc-v2 meta, old is as expected');
311         my $mid = mids($eml)->[0];
312         my $smsg = $v2ibx->over->next_by_mid($mid, \(my $id), \(my $prev));
313         my $oldmax = $oidx->max;
314         my $x3_orig = $oidx->get_xref3(3);
315         is(scalar(@$x3_orig), 1, '#3 has one xref');
316         $oidx->add_xref3(3, $smsg->{num}, $smsg->{blob}, 'v2.example');
317         my $x3 = $oidx->get_xref3(3);
318         is(scalar(@$x3), 2, 'injected xref3');
319         $oidx->commit_lazy;
320         my $opt = { 2 => \(my $err = '') };
321         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
322                 'extindex --all is noop');
323         is($err, '', 'no warnings in index');
324         $oidx->dbh;
325         is($oidx->max, $oldmax, 'oidx->max unchanged');
326         $oidx->dbh_close;
327         ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
328                 undef, $opt), 'extindex --reindex');
329         $oidx->dbh;
330         ok($oidx->max > $oldmax, 'oidx->max bumped');
331         like($err, qr/split into 2 due to deduplication change/,
332                 'bifurcation noted');
333         my $added = $oidx->get_art($oidx->max);
334         is($added->{blob}, $smsg->{blob}, 'new blob indexed');
335         is_deeply(["v2.example:$smsg->{num}:$smsg->{blob}"],
336                 $oidx->get_xref3($added->{num}),
337                 'xref3 corrected for bifurcated message');
338         is_deeply($oidx->get_xref3(3), $x3_orig, 'xref3 restored for #3');
339 }
340
341 if ('--reindex --rethread') {
342         my $before = $oidx->dbh->selectrow_array(<<'');
343 SELECT MAX(tid) FROM over WHERE num > 0
344
345         my $opt = {};
346         ok(run_script([qw(-extindex --reindex --rethread --all),
347                         "$home/extindex"], undef, $opt),
348                         '--rethread');
349         my $after = $oidx->dbh->selectrow_array(<<'');
350 SELECT MIN(tid) FROM over WHERE num > 0
351
352         # actual rethread logic is identical to v1/v2 and tested elsewhere
353         ok($after > $before, '--rethread updates MIN(tid)');
354 }
355
356 if ('remove v1test and test gc') {
357         xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
358                 { GIT_CONFIG => $cfg_path });
359         my $opt = { 2 => \(my $err = '') };
360         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
361                 'extindex --gc');
362         like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
363         is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
364                 'no non-informational messages');
365         $misc->{xdb}->reopen;
366         @it = $misc->mset('')->items;
367         is(scalar(@it), 1, 'only one inbox left');
368 }
369
370 done_testing;