]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
extindex: delete stale messages from over.sqlite3
[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 Fcntl qw(:seek);
10 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
11 require_git(2.6);
12 require_mods(qw(DBD::SQLite Search::Xapian));
13 use_ok 'PublicInbox::ExtSearch';
14 use_ok 'PublicInbox::ExtSearchIdx';
15 my $sock = tcp_server();
16 my $host_port = $sock->sockhost . ':' . $sock->sockport;
17 my ($home, $for_destroy) = tmpdir();
18 local $ENV{HOME} = $home;
19 mkdir "$home/.public-inbox" or BAIL_OUT $!;
20 my $cfg_path = "$home/.public-inbox/config";
21 open my $fh, '>', $cfg_path or BAIL_OUT $!;
22 print $fh <<EOF or BAIL_OUT $!;
23 [publicinboxMda]
24         spamcheck = none
25 EOF
26 close $fh or BAIL_OUT $!;
27 my $v2addr = 'v2test@example.com';
28 my $v1addr = 'v1test@example.com';
29 ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
30         "$home/v2test", 'http://example.com/v2test', $v2addr ]), 'v2test init');
31 my $env = { ORIGINAL_RECIPIENT => $v2addr };
32 my $eml = eml_load('t/utf8.eml');
33
34 $eml->header_set('List-Id', '<v2.example.com>');
35 open($fh, '+>', undef) or BAIL_OUT $!;
36 $fh->autoflush(1);
37 print $fh $eml->as_string or BAIL_OUT $!;
38 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
39
40 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
41
42 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
43         'http://example.com/v1test', $v1addr ]), 'v1test init');
44
45 $eml->header_set('List-Id', '<v1.example.com>');
46 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
47 truncate($fh, 0) or BAIL_OUT $!;
48 print $fh $eml->as_string or BAIL_OUT $!;
49 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
50
51 $env = { ORIGINAL_RECIPIENT => $v1addr };
52 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
53
54 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
55
56 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
57 {
58         my $es = PublicInbox::ExtSearch->new("$home/extindex");
59         ok($es->has_threadid, '->has_threadid');
60 }
61
62 { # TODO: -extindex should write this to config
63         open $fh, '>>', $cfg_path or BAIL_OUT $!;
64         print $fh <<EOF or BAIL_OUT $!;
65 ; for ->ALL
66 [extindex "all"]
67         topdir = $home/extindex
68 EOF
69         close $fh or BAIL_OUT $!;
70
71         my $pi_cfg = PublicInbox::Config->new;
72         $pi_cfg->fill_all;
73         ok($pi_cfg->ALL, '->ALL');
74         my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
75         my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
76         is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
77                 '->nntp_xref_for');
78 }
79
80 SKIP: {
81         require_mods(qw(Net::NNTP), 1);
82         my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
83         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
84         my $td = start_script($cmd, undef, { 3 => $sock });
85         my $n = Net::NNTP->new($host_port);
86         my @xp = $n->xpath('<testmessage@example.com>');
87         is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
88         $n->group('v1.example');
89         my $res = $n->head(1);
90         @$res = grep(/^Xref: /, @$res);
91         like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
92 }
93
94 my $es = PublicInbox::ExtSearch->new("$home/extindex");
95 {
96         my $smsg = $es->over->get_art(1);
97         ok($smsg, 'got first article');
98         is($es->over->get_art(2), undef, 'only one added');
99         my $xref3 = $es->over->get_xref3(1);
100         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
101         like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
102         is(scalar(@$xref3), 2, 'only to entries');
103 }
104
105 if ('inbox edited') {
106         my ($in, $out, $err);
107         $in = $out = $err = '';
108         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
109         my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
110         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
111         ok(run_script($cmd, $env, $opt), '-edit');
112         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
113                 'extindex again');
114         like($err, qr/discontiguous range/, 'warned about discontiguous range');
115         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
116         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
117         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
118         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
119         my $eml2 = $es->smsg_eml($msg2);
120         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
121         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
122         my $eml1 = $es->smsg_eml($msg1);
123         like($eml1->body, qr/test message/, 'original body in #1');
124         my $x1 = $es->over->get_xref3(1);
125         my $x2 = $es->over->get_xref3(2);
126         is(scalar(@$x1), 1, 'original only has one xref3');
127         is(scalar(@$x2), 1, 'new message has one xref3');
128         isnt($x1->[0], $x2->[0], 'xref3 differs');
129
130         my $mset = $es->mset('b:"BEST MSG"');
131         is($mset->size, 1, 'new message found');
132         $mset = $es->mset('b:"test message"');
133         is($mset->size, 1, 'old message found');
134         delete @$es{qw(git over xdb)}; # fork preparation
135
136         my $pi_cfg = PublicInbox::Config->new;
137         $pi_cfg->fill_all;
138         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
139                 '2 results in ->ALL');
140         my $res = {};
141         my $nr = 0;
142         $pi_cfg->each_inbox(sub {
143                 $nr++;
144                 my ($ibx) = @_;
145                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
146                 my $mset = $ibx->isrch->mset('s:Testing');
147                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
148         });
149         is($nr, 2, 'two inboxes');
150         my $exp = {};
151         for my $v (qw(v1 v2)) {
152                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
153                 my $smsg = $ibx->over->get_art(1);
154                 $smsg->psgi_cull;
155                 $exp->{"$v.example"} = [ $smsg ];
156         }
157         is_deeply($res, $exp, 'isearch limited results');
158         $pi_cfg = $res = $exp = undef;
159
160         open my $rmfh, '+>', undef or BAIL_OUT $!;
161         $rmfh->autoflush(1);
162         print $rmfh $eml2->as_string or BAIL_OUT $!;
163         seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
164         $opt->{0} = $rmfh;
165         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
166
167         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
168                 'extindex after rm');
169         is($es->over->get_art(2), undef, 'doc #2 gone');
170         $mset = $es->mset('b:"BEST MSG"');
171         is($mset->size, 0, 'new message gone');
172 }
173
174 my $misc = $es->misc;
175 my @it = $misc->mset('')->items;
176 is(scalar(@it), 2, 'two inboxes');
177 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
178 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
179
180 my $cfg = PublicInbox::Config->new;
181 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
182 if ('inject w/o indexing') {
183         use PublicInbox::Import;
184         my $v1ibx = $cfg->lookup_name('v1test');
185         my $last_v1_commit = $v1ibx->mm->last_commit;
186         my $v2ibx = $cfg->lookup_name('v2test');
187         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
188         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
189         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
190         is($last_v2_commit, $cmt, 'v2 index up-to-date');
191
192         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
193         $v2im->{lock_path} = undef;
194         $v2im->{path_type} = 'v2';
195         $v2im->add(eml_load('t/mda-mime.eml'));
196         $v2im->done;
197         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
198         isnt($tip, $cmt, '0.git v2 updated');
199
200         # inject a message w/o updating index
201         rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
202                 BAIL_OUT $!;
203         open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
204         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
205                 BAIL_OUT '-mda';
206         rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
207                 BAIL_OUT $!;
208
209         my ($in, $out, $err);
210         $in = $out = $err = '';
211         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
212         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
213                 undef, undef), 'extindex noop');
214         $es->{xdb}->reopen;
215         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
216         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
217         $mset = $es->mset('mid:multipart-html-sucks@11');
218         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
219         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
220
221         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
222         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
223                 $last_v2_commit, '-index v2 worked');
224         ok(run_script([qw(-extindex --all), "$home/extindex"]),
225                 'extindex updates');
226
227         $es->{xdb}->reopen;
228         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
229         is($mset->size, 1, 'got v1 message');
230         $mset = $es->mset('mid:multipart-html-sucks@11');
231         is($mset->size, 1, 'got v2 message');
232 }
233
234 if ('reindex catches missed messages') {
235         use PublicInbox::InboxWritable;
236         use PublicInbox::OverIdx;
237         my $v2ibx = $cfg->lookup_name('v2test');
238         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
239         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
240         my $eml = eml_load('t/data/0001.patch');
241         $im->add($eml);
242         $im->done;
243         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
244         isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
245         my $f = "$home/extindex/ei$schema_version/over.sqlite3";
246         my $oidx = PublicInbox::OverIdx->new($f);
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');
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');
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 ('remove v1test and test gc') {
298         xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
299                 { GIT_CONFIG => $cfg_path });
300         my $opt = { 2 => \(my $err = '') };
301         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
302                 'extindex --gc');
303         like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
304         is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
305                 'no non-informational messages');
306         $misc->{xdb}->reopen;
307         @it = $misc->mset('')->items;
308         is(scalar(@it), 1, 'only one inbox left');
309 }
310
311 done_testing;