Revert "daemon: check connections WIP"
[public-inbox.git] / t / extsearch.t
blob6bcfdd841988e8e7f617541c8b915d3a9e2f8b24
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use PublicInbox::Config;
7 use PublicInbox::InboxWritable;
8 require_git(2.6);
9 require_mods(qw(json DBD::SQLite Xapian));
10 use autodie qw(chmod open rename truncate unlink);
11 use PublicInbox::Search;
12 use_ok 'PublicInbox::ExtSearch';
13 use_ok 'PublicInbox::ExtSearchIdx';
14 use_ok 'PublicInbox::OverIdx';
15 my ($home, $for_destroy) = tmpdir();
16 local $ENV{HOME} = $home;
17 mkdir "$home/.public-inbox" or BAIL_OUT $!;
18 my $cfg_path = "$home/.public-inbox/config";
19 PublicInbox::IO::write_file '>', $cfg_path, <<EOF;
20 [publicinboxMda]
21         spamcheck = none
22 EOF
23 my $v2addr = 'v2test@example.com';
24 my $v1addr = 'v1test@example.com';
25 ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
26         "$home/v2test", 'http://example.com/v2test', $v2addr ]), 'v2test init');
27 my $env = { ORIGINAL_RECIPIENT => $v2addr };
28 my $eml = eml_load('t/utf8.eml');
29 my $eidxdir = "$home/extindex";
31 $eml->header_set('List-Id', '<v2.example.com>');
33 my $in = \($eml->as_string);
34 run_script(['-mda', '--no-precheck'], $env, { 0 => $in }) or BAIL_OUT '-mda';
36 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
37         'http://example.com/v1test', $v1addr ]), 'v1test init');
39 $eml->header_set('List-Id', '<v1.example.com>');
40 $in = \$eml->as_string;
42 $env = { ORIGINAL_RECIPIENT => $v1addr };
43 run_script(['-mda', '--no-precheck'], $env, { 0 => $in }) or BAIL_OUT '-mda';
45 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
47 ok(run_script([qw(-extindex --dangerous --all), "$home/extindex"]),
48         'extindex init');
50         my $es = PublicInbox::ExtSearch->new("$home/extindex");
51         ok($es->has_threadid, '->has_threadid');
54 if ('with boost') {
55         xsys_e([qw(git config publicinbox.v1test.boost), 10],
56                 { GIT_CONFIG => $cfg_path });
57         ok(run_script([qw(-extindex --all), "$home/extindex-b"]),
58                 'extindex init with boost');
59         my $es = PublicInbox::ExtSearch->new("$home/extindex-b");
60         my $smsg = $es->over->get_art(1);
61         ok($smsg, 'got first article');
62         my $xref3 = $es->over->get_xref3($smsg->{num});
63         my @v1 = grep(/\Av1/, @$xref3);
64         my @v2 = grep(/\Av2/, @$xref3);
65         like($v1[0], qr/\Av1\.example.*?\b\Q$smsg->{blob}\E\b/,
66                 'smsg->{blob} respected boost');
67         is(scalar(@$xref3), 2, 'only to entries');
68         undef $es;
70         xsys_e([qw(git config publicinbox.v2test.boost), 20],
71                 { GIT_CONFIG => $cfg_path });
72         ok(run_script([qw(-extindex --all --reindex), "$home/extindex-b"]),
73                 'extindex --reindex with altered boost');
75         $es = PublicInbox::ExtSearch->new("$home/extindex-b");
76         $smsg = $es->over->get_art(1);
77         like($v2[0], qr/\Av2\.example.*?\b\Q$smsg->{blob}\E\b/,
78                         'smsg->{blob} respects boost after reindex');
80         # high boost added later
81         my $b2 = "$home/extindex-bb";
82         ok(run_script([qw(-extindex), $b2, "$home/v1test"]),
83                 'extindex with low boost inbox only');
84         ok(run_script([qw(-extindex), $b2, "$home/v2test"]),
85                 'extindex with high boost inbox only');
86         $es = PublicInbox::ExtSearch->new($b2);
87         $smsg = $es->over->get_art(1);
88         $xref3 = $es->over->get_xref3($smsg->{num});
89         like($v2[0], qr/\Av2\.example.*?\b\Q$smsg->{blob}\E\b/,
90                 'smsg->{blob} respected boost across 2 index runs');
92         xsys_e([qw(git config --unset publicinbox.v1test.boost)],
93                 { GIT_CONFIG => $cfg_path });
94         xsys_e([qw(git config --unset publicinbox.v2test.boost)],
95                 { GIT_CONFIG => $cfg_path });
98 { # TODO: -extindex should write this to config
99         PublicInbox::IO::write_file '>>', $cfg_path, <<EOF;
100 ; for ->ALL
101 [extindex "all"]
102         topdir = $home/extindex
104         my $pi_cfg = PublicInbox::Config->new;
105         $pi_cfg->fill_all;
106         ok($pi_cfg->ALL, '->ALL');
107         my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
108         my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
109         is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
110                 '->nntp_xref_for');
113 SKIP: {
114         require_mods(qw(Net::NNTP), 1);
115         my $sock = tcp_server();
116         my $host_port = tcp_host_port($sock);
117         my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
118         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
119         my $td = start_script($cmd, undef, { 3 => $sock });
120         my $n = Net::NNTP->new($host_port);
121         my @xp = $n->xpath('<testmessage@example.com>');
122         is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
123         $n->group('v1.example');
124         my $res = $n->head(1);
125         @$res = grep(/^Xref: /, @$res);
126         like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
129 my $es = PublicInbox::ExtSearch->new("$home/extindex");
131         my $smsg = $es->over->get_art(1);
132         ok($smsg, 'got first article');
133         is($es->over->get_art(2), undef, 'only one added');
134         my $xref3 = $es->over->get_xref3(1);
135         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
136         like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
137         is(scalar(@$xref3), 2, 'only to entries');
140 if ('inbox edited') {
141         my ($in, $out, $err);
142         $in = $out = $err = '';
143         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
144         my $env = { MAIL_EDITOR => "$^X -w -i -p -e 's/test message/BEST MSG/'" };
145         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
146         ok(run_script($cmd, $env, $opt), '-edit');
147         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
148                 'extindex again');
149         like($err, qr/discontiguous range/, 'warned about discontiguous range');
150         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
151         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
152         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
153         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
154         my $eml2 = $es->smsg_eml($msg2);
155         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
156         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
157         my $eml1 = $es->smsg_eml($msg1);
158         like($eml1->body, qr/test message/, 'original body in #1');
159         my $x1 = $es->over->get_xref3(1);
160         my $x2 = $es->over->get_xref3(2);
161         is(scalar(@$x1), 1, 'original only has one xref3');
162         is(scalar(@$x2), 1, 'new message has one xref3');
163         isnt($x1->[0], $x2->[0], 'xref3 differs');
165         my $mset = $es->mset('b:"BEST MSG"');
166         is($mset->size, 1, 'new message found');
167         $mset = $es->mset('b:"test message"');
168         is($mset->size, 1, 'old message found');
169         delete @$es{qw(git over xdb qp)}; # fork preparation
171         my $pi_cfg = PublicInbox::Config->new;
172         $pi_cfg->fill_all;
173         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
174                 '2 results in ->ALL');
175         my $res = {};
176         my $nr = 0;
177         $pi_cfg->each_inbox(sub {
178                 $nr++;
179                 my ($ibx) = @_;
180                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
181                 my $mset = $ibx->isrch->mset('s:Testing');
182                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
183         });
184         is($nr, 2, 'two inboxes');
185         my $exp = {};
186         for my $v (qw(v1 v2)) {
187                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
188                 my $smsg = $ibx->over->get_art(1);
189                 $smsg->psgi_cull;
190                 $exp->{"$v.example"} = [ $smsg ];
191         }
192         is_deeply($res, $exp, 'isearch limited results');
193         $pi_cfg = $res = $exp = undef;
195         $opt->{0} = \($eml2->as_string);
196         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
198         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
199                 'extindex after rm');
200         is($es->over->get_art(2), undef, 'doc #2 gone');
201         $mset = $es->mset('b:"BEST MSG"');
202         is($mset->size, 0, 'new message gone');
205 my $misc = $es->misc;
206 my @it = $misc->mset('')->items;
207 is(scalar(@it), 2, 'two inboxes');
208 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
209 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
211 my $cfg = PublicInbox::Config->new;
212 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
213 my $f = "$home/extindex/ei$schema_version/over.sqlite3";
214 my $oidx = PublicInbox::OverIdx->new($f);
215 if ('inject w/o indexing') {
216         use PublicInbox::Import;
217         my $v1ibx = $cfg->lookup_name('v1test');
218         my $last_v1_commit = $v1ibx->mm->last_commit;
219         my $v2ibx = $cfg->lookup_name('v2test');
220         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
221         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
222         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
223         is($last_v2_commit, $cmt, 'v2 index up-to-date');
225         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
226         $v2im->{lock_path} = undef;
227         $v2im->{path_type} = 'v2';
228         $v2im->add(eml_load('t/mda-mime.eml'));
229         $v2im->done;
230         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
231         isnt($tip, $cmt, '0.git v2 updated');
233         # inject a message w/o updating index
234         rename("$home/v1test/public-inbox", "$home/v1test/skip-index");
235         open(my $eh, '<', 't/iso-2202-jp.eml');
236         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
237                 BAIL_OUT '-mda';
238         rename("$home/v1test/skip-index", "$home/v1test/public-inbox");
240         my ($in, $out, $err);
241         $in = $out = $err = '';
242         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
243         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
244                 undef, undef), 'extindex noop');
245         $es->{xdb}->reopen;
246         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
247         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
248         $mset = $es->mset('mid:multipart-html-sucks@11');
249         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
250         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
252         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
253         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
254                 $last_v2_commit, '-index v2 worked');
255         ok(run_script([qw(-extindex --all), "$home/extindex"]),
256                 'extindex updates');
258         $es->{xdb}->reopen;
259         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
260         is($mset->size, 1, 'got v1 message');
261         $mset = $es->mset('mid:multipart-html-sucks@11');
262         is($mset->size, 1, 'got v2 message');
265 if ('reindex catches missed messages') {
266         my $v2ibx = $cfg->lookup_name('v2test');
267         $v2ibx->{-no_fsync} = 1;
268         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
269         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
270         my $eml = eml_load('t/data/0001.patch');
271         $im->add($eml);
272         $im->done;
273         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
274         isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
275         $oidx->dbh;
276         my $uv = $v2ibx->uidvalidity;
277         my $lc_key = "lc-v2:v2.example//$uv;0";
278         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
279                 'update lc-v2 meta, old is as expected');
280         my $max = $oidx->max;
281         $oidx->dbh_close;
282         ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
283                 '-extindex noop');
284         is($oidx->max, $max, '->max unchanged');
285         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
286         $oidx->dbh_close;
287         my $opt = { 2 => \(my $err = '') };
288         ok(run_script([qw(-extindex --reindex), "$home/extindex",
289                         $v2ibx->{inboxdir}], undef, $opt),
290                         '--reindex for unseen');
291         is($oidx->max, $max + 1, '->max bumped');
292         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 stays unchanged');
293         my @err = split(/^/, $err);
294         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
295         like $err[0], qr/# .*? reindex_unseen/, 'got reindex_unseen message';
296         my $new = $oidx->get_art($max + 1);
297         is($new->{subject}, $eml->header('Subject'), 'new message added');
299         $es->{xdb}->reopen;
300         # git patch-id --stable <t/data/0001.patch | awk '{print $1}'
301         my $patchid = '91ee6b761fc7f47cad9f2b09b10489f313eb5b71';
302         my $mset = $es->search->mset("patchid:$patchid");
303         is($mset->size, 1, 'patchid search works');
305         $mset = $es->mset("mid:$new->{mid}");
306         is($mset->size, 1, 'previously unseen, now indexed in Xapian');
308         ok($im->remove($eml), 'remove new message from v2 inbox');
309         $im->done;
310         my $cmt_c = $v2ibx->mm->last_commit_xap($schema_version, 0);
311         is($oidx->eidx_meta($lc_key, $cmt_c), $cmt_b,
312                 'bump lc-v2 meta again to skip v2 remove');
313         $err = '';
314         $oidx->dbh_close;
315         ok(run_script([qw(-extindex --reindex), "$home/extindex",
316                         $v2ibx->{inboxdir}], undef, $opt),
317                         '--reindex for stale');
318         @err = split(/^/, $err);
319         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
320         like($err[0], qr/\(#$new->{num}\): stale/, 'got stale message warning');
321         is($oidx->get_art($new->{num}), undef,
322                 'stale message gone from over');
323         is_deeply($oidx->get_xref3($new->{num}), [],
324                 'stale message has no xref3');
325         $es->{xdb}->reopen;
326         $mset = $es->mset("mid:$new->{mid}");
327         is($mset->size, 0, 'stale mid gone Xapian');
329         ok(run_script([qw(-extindex --reindex --all --fast), "$home/extindex"],
330                         undef, $opt), '--reindex w/ --fast');
331         ok(!run_script([qw(-extindex --all --fast), "$home/extindex"],
332                         undef, $opt), '--fast alone makes no sense');
335 if ('reindex catches content bifurcation') {
336         use PublicInbox::MID qw(mids);
337         my $v2ibx = $cfg->lookup_name('v2test');
338         $v2ibx->{-no_fsync} = 1;
339         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
340         my $eml = eml_load('t/data/message_embed.eml');
341         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
342         $im->add($eml);
343         $im->done;
344         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
345         my $uv = $v2ibx->uidvalidity;
346         my $lc_key = "lc-v2:v2.example//$uv;0";
347         $oidx->dbh;
348         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
349                 'update lc-v2 meta, old is as expected');
350         my $mid = mids($eml)->[0];
351         my $smsg = $v2ibx->over->next_by_mid($mid, \(my $id), \(my $prev));
352         my $oldmax = $oidx->max;
353         my $x3_orig = $oidx->get_xref3(3);
354         is(scalar(@$x3_orig), 1, '#3 has one xref');
355         $oidx->add_xref3(3, $smsg->{num}, $smsg->{blob}, 'v2.example');
356         my $x3 = $oidx->get_xref3(3);
357         is(scalar(@$x3), 2, 'injected xref3');
358         $oidx->commit_lazy;
359         my $opt = { 2 => \(my $err = '') };
360         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
361                 'extindex --all is noop');
362         is($err, '', 'no warnings in index');
363         $oidx->dbh;
364         is($oidx->max, $oldmax, 'oidx->max unchanged');
365         $oidx->dbh_close;
366         ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
367                 undef, $opt), 'extindex --reindex') or diag explain($opt);
368         $oidx->dbh;
369         ok($oidx->max > $oldmax, 'oidx->max bumped');
370         like($err, qr/split into 2 due to deduplication change/,
371                 'bifurcation noted');
372         my $added = $oidx->get_art($oidx->max);
373         is($added->{blob}, $smsg->{blob}, 'new blob indexed');
374         is_deeply(["v2.example:$smsg->{num}:$smsg->{blob}"],
375                 $oidx->get_xref3($added->{num}),
376                 'xref3 corrected for bifurcated message');
377         is_deeply($oidx->get_xref3(3), $x3_orig, 'xref3 restored for #3');
380 if ('--reindex --rethread') {
381         my $before = $oidx->dbh->selectrow_array(<<'');
382 SELECT MAX(tid) FROM over WHERE num > 0
384         my $opt = {};
385         ok(run_script([qw(-extindex --reindex --rethread --all),
386                         "$home/extindex"], undef, $opt),
387                         '--rethread');
388         my $after = $oidx->dbh->selectrow_array(<<'');
389 SELECT MIN(tid) FROM over WHERE num > 0
391         # actual rethread logic is identical to v1/v2 and tested elsewhere
392         ok($after > $before, '--rethread updates MIN(tid)');
395 if ('remove v1test and test gc') {
396         xsys_e([qw(git config --unset publicinbox.v1test.inboxdir)],
397                 { GIT_CONFIG => $cfg_path });
398         my $opt = { 2 => \(my $err = '') };
399         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
400                 'extindex --gc');
401         like($err, qr/^# remove #1 v1\.example /ms, 'removed v1 message');
402         is(scalar(grep(!/^#/, split(/^/m, $err))), 0,
403                 'no non-informational messages');
404         $misc->{xdb}->reopen;
405         @it = $misc->mset('')->items;
406         is(scalar(@it), 1, 'only one inbox left');
409 if ('dedupe + dry-run') {
410         my @cmd = ('-extindex', "$home/extindex");
411         my $opt = { 2 => \(my $err = '') };
412         ok(run_script([@cmd, '--dedupe'], undef, $opt), '--dedupe');
413         ok(run_script([@cmd, qw(--dedupe --dry-run)], undef, $opt),
414                 '--dry-run --dedupe');
415         is $err, '', 'no errors';
416         ok(!run_script([@cmd, qw(--dry-run)], undef, $opt),
417                 '--dry-run alone fails');
420 for my $j (1, 3, 6) {
421         my $o = { 2 => \(my $err = '') };
422         my $d = "$home/extindex-j$j";
423         ok(run_script(['-extindex', "-j$j", '--all', $d], undef, $o),
424                 "init with -j$j");
425         my $max = $j - 2;
426         $max = 0 if $max < 0;
427         my @dirs = glob("$d/ei*/?");
428         like($dirs[-1], qr!/ei[0-9]+/$max\z!, '-j works');
431 SKIP: {
432         my $d = "$home/extindex-j1";
433         my $es = PublicInbox::ExtSearch->new($d);
434         ok(my $nresult0 = $es->mset('z:0..')->size, 'got results');
435         ok(ref($es->{xdb}), '{xdb} created');
436         my $nshards1 = $es->{nshard};
437         is($nshards1, 1, 'correct shard count');
439         my @ei_dir = glob("$d/ei*/");
440         chmod 0755, $ei_dir[0];
441         my $mode = sprintf('%04o', 07777 & (stat($ei_dir[0]))[2]);
442         is($mode, '0755', 'mode set on ei*/ dir');
443         my $o = { 2 => \(my $err = '') };
444         ok(run_script([qw(-xcpdb -R4), $d]), 'xcpdb R4');
445         my @dirs = glob("$d/ei*/?");
446         for my $i (0..3) {
447                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] created");
448                 my $m = sprintf('%04o', 07777 & (stat($dirs[$i]))[2]);
449                 is($m, $mode, "shard [$i] mode");
450         }
451         delete @$es{qw(xdb qp)};
452         is($es->mset('z:0..')->size, $nresult0, 'new shards, same results');
454         for my $i (4..5) {
455                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
456         }
458         ok(run_script([qw(-xcpdb -R2), $d]), 'xcpdb -R2');
459         @dirs = glob("$d/ei*/?");
460         for my $i (0..1) {
461                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] kept");
462         }
463         for my $i (2..3) {
464                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
465         }
466         have_xapian_compact 1;
467         ok(run_script([qw(-compact), $d], undef, $o), 'compact');
468         # n.b. stderr contains xapian-compact output
470         my @d2 = glob("$d/ei*/?");
471         is_deeply(\@d2, \@dirs, 'dirs consistent after compact');
472         ok(run_script([qw(-extindex --dedupe --all), $d]),
473                 '--dedupe works after compact');
474         ok(run_script([qw(-extindex --gc), $d], undef, $o),
475                 '--gc works after compact');
478 { # ensure --gc removes non-xposted messages
479         my $old_size = -s $cfg_path // xbail "stat $cfg_path $!";
480         my $tmp_addr = 'v2tmp@example.com';
481         run_script([qw(-init v2tmp --indexlevel basic
482                 --newsgroup v2tmp.example),
483                 "$home/v2tmp", 'http://example.com/v2tmp', $tmp_addr ])
484                 or xbail '-init';
485         $env = { ORIGINAL_RECIPIENT => $tmp_addr };
486         my $mid = 'tmpmsg@example.com';
487         my $in = \<<EOM;
488 From: b\@z
489 To: b\@r
490 Message-Id: <$mid>
491 Subject: tmpmsg
492 Date: Tue, 19 Jan 2038 03:14:07 +0000
495         run_script([qw(-mda --no-precheck)], $env, {0 => $in}) or xbail '-mda';
496         ok(run_script([qw(-extindex --all), "$home/extindex"]), 'update');
497         my $nr;
498         {
499                 my $es = PublicInbox::ExtSearch->new("$home/extindex");
500                 my ($id, $prv);
501                 my $smsg = $es->over->next_by_mid($mid, \$id, \$prv);
502                 ok($smsg, 'tmpmsg indexed');
503                 my $mset = $es->search->mset("mid:$mid");
504                 is($mset->size, 1, 'new message found');
505                 $mset = $es->search->mset('z:0..');
506                 $nr = $mset->size;
507         }
508         truncate($cfg_path, $old_size);
509         my $rdr = { 2 => \(my $err) };
510         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $rdr),
511                 'gc to get rid of removed inbox');
512         is_deeply([ grep(!/^(?:I:|#)/, split(/^/m, $err)) ], [],
513                 'no non-informational errors in stderr');
515         my $es = PublicInbox::ExtSearch->new("$home/extindex");
516         my $mset = $es->search->mset("mid:$mid");
517         is($mset->size, 0, 'tmpmsg gone from search');
518         my ($id, $prv);
519         is($es->over->next_by_mid($mid, \$id, \$prv), undef,
520                 'tmpmsg gone from over');
521         $id = $prv = undef;
522         is($es->over->next_by_mid('testmessage@example.com', \$id, \$prv),
523                 undef, 'remaining message not indavderover');
524         $mset = $es->search->mset('z:0..');
525         is($mset->size, $nr - 1, 'existing messages not clobbered from search');
526         my $o = $es->over->{dbh}->selectall_arrayref(<<EOM);
527 SELECT num FROM over ORDER BY num
529         is(scalar(@$o), $mset->size, 'over row count matches Xapian');
530         my $x = $es->over->{dbh}->selectall_arrayref(<<EOM);
531 SELECT DISTINCT(docid) FROM xref3 ORDER BY docid
533         is_deeply($x, $o, 'xref3 and over docids match');
537         my $d = "$home/eidx-med";
538         ok(run_script([qw(-extindex --dangerous --all -L medium -j3), $d]),
539                 'extindex medium init');
540         my $es = PublicInbox::ExtSearch->new($d);
541         is($es->xdb->get_metadata('indexlevel'), 'medium',
542                 'es indexlevel before');
543         my @xdb = $es->xdb_shards_flat;
544         is($xdb[0]->get_metadata('indexlevel'), 'medium',
545                 '0 indexlevel before');
546         shift @xdb;
547         for (@xdb) {
548                 ok(!$_->get_metadata('indexlevel'), 'no indexlevel in >0 shard')
549         }
550         is($es->xdb->get_metadata('indexlevel'), 'medium', 'indexlevel before');
551         ok(run_script([qw(-xcpdb -R5), $d]), 'xcpdb R5');
552         $es = PublicInbox::ExtSearch->new($d);
553         is($es->xdb->get_metadata('indexlevel'), 'medium',
554                 '0 indexlevel after');
555         @xdb = $es->xdb_shards_flat;
556         is(scalar(@xdb), 5, 'got 5 shards');
557         is($xdb[0]->get_metadata('indexlevel'), 'medium', '0 indexlevel after');
558         shift @xdb;
559         for (@xdb) {
560                 ok(!$_->get_metadata('indexlevel'), 'no indexlevel in >0 shard')
561         }
562         my $mpi = "$d/ALL.git/objects/pack/multi-pack-index";
563         SKIP: {
564                 skip 'git too old for for multi-pack-index', 2 if !-f $mpi;
565                 unlink glob("$d/ALL.git/objects/pack/*");
566                 ok run_script([qw(-extindex --all -L medium -j3
567                                 --no-multi-pack-index), $d]),
568                                 'test --no-multi-pack-index';
569                 ok !-f $mpi, '--no-multi-pack-index respected';
570         }
573 test_lei(sub {
574         my $d = "$home/extindex";
575         lei_ok('convert', '-o', "$home/md1", $d);
576         lei_ok('convert', '-o', "$home/md2", "extindex:$d");
577         my $dst = [];
578         my $cb = sub { push @$dst, $_[2]->as_string };
579         require PublicInbox::MdirReader;
580         PublicInbox::MdirReader->new->maildir_each_eml("$home/md1", $cb);
581         my @md1 = sort { $a cmp $b } @$dst;
582         ok(scalar(@md1), 'dumped messages to md1');
583         $dst = [];
584         PublicInbox::MdirReader->new->maildir_each_eml("$home/md2", $cb);
585         @$dst = sort { $a cmp $b } @$dst;
586         is_deeply($dst, \@md1,
587                 "convert from extindex w/ or w/o `extindex' prefix");
589         my @o = glob "$home/extindex/ei*/over.sqlite*";
590         unlink(@o);
591         ok(!lei('convert', '-o', "$home/fail", "extindex:$d"));
592         like($lei_err, qr/unindexed .*?not supported/,
593                 'noted unindexed extindex is unsupported');
596 require PublicInbox::XhcMset;
597 if ('indexheader support') {
598         xsys_e [qw(git config extindex.all.indexheader
599                 boolean_term:xarchiveshash:X-Archives-Hash)],
600                 { GIT_CONFIG => $cfg_path };
601         my $eml = eml_load('t/plack-qp.eml');
602         $eml->header_set('X-Archives-Hash', 'deadbeefcafe');
603         $in = \($eml->as_string);
604         $env->{ORIGINAL_RECIPIENT} = $v2addr;
605         run_script([qw(-mda --no-precheck)], $env, { 0 => $in }) or
606                 xbail '-mda';
607         ok run_script([qw(-extindex --all -vvv), $eidxdir]),
608                 'extindex update';
609         $es = PublicInbox::Config->new($cfg_path)->ALL;
610         my $mset = $es->mset('xarchiveshash:deadbeefcafe');
611         is $mset->size, 1, 'extindex.*.indexheader works';
612         require PublicInbox::XapClient;
613         local $PublicInbox::Search::XHC =
614                         PublicInbox::XapClient::start_helper('-j0') or
615                         xbail "no XHC: $@";
616         my @args;
617         $es->async_mset('xarchiveshash:deadbeefcafe', {} , sub { @args = @_ });
618         is scalar(@args), 2, 'no extra args on xarchiveshash hit';
619         is $args[0]->size, 1, 'async mset xarchiveshash hit works';
620         ok !$args[1], 'no error on xarchiveshash hit';
621         @args = ();
622         $es->async_mset('xarchiveshash:cafebeefdead', {} , sub { @args = @_ });
623         is scalar(@args), 2, 'no extra args on xarchiveshash miss';
624         is $args[0]->size, 0, 'async mset xarchivehash miss works';
625         ok !$args[1], 'no error on xarchiveshash miss';
628 if ('per-inbox altid w/ extindex') {
629         my $another = 'another-nntp.sqlite3';
630         my $altid = [ "serial:gmane:file=$another" ];
631         my $aibx = create_inbox 'v2', version => 2, indexlevel => 'basic',
632                                 altid => $altid, sub {
633                 my ($im, $ibx) = @_;
634                 my $mm = PublicInbox::Msgmap->new_file(
635                                         "$ibx->{inboxdir}/$another", 2);
636                 $mm->mid_set(1234, 'a@example.com') == 1 or xbail 'mid_set';
637                 $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
638 From: a@example.com
639 To: b@example.com
640 Subject: boo!
641 Message-ID: <a@example.com>
642 X-Archives-Hash: dadfad
643 Organization: felonious feline family
645 hello world gmane:666
647         };
648         PublicInbox::IO::write_file '>>', $cfg_path, <<EOF;
649 [publicinbox "altid-test"]
650         inboxdir = $aibx->{inboxdir}
651         address = b\@example.com
652         altid = $altid->[0]
653         indexheader = phrase:organization:Organization
655         ok run_script([qw(-extindex --all -vvv), $eidxdir]),
656                 'extindex update w/ altid';
657         local $PublicInbox::Search::XHC =
658                         PublicInbox::XapClient::start_helper('-j0') or
659                         xbail "no XHC: $@";
660         my @args;
661         my $pi_cfg = PublicInbox::Config->new($cfg_path);
662         my $ibx = $pi_cfg->lookup('b@example.com');
663         my $mset = $ibx->isrch->mset('gmane:1234');
665         is $mset->size, 1, 'isrch->mset altid hit';
666         $ibx->isrch->async_mset('gmane:1234', {} , sub { @args = @_ });
667         is scalar(@args), 2, 'no extra args on altid hit';
668         is $args[0]->size, 1, 'isrch->async_mset altid hit';
670         $mset = $ibx->isrch->mset('organization:felonious');
671         is $mset->size, 1, 'isrch->mset indexheader hit';
672         @args = ();
673         $ibx->isrch->async_mset('organization:felonious', {} , sub { @args = @_ });
674         is scalar(@args), 2, 'no extra args on indexheader hit';
675         is $args[0]->size, 1, 'isrch->async_mset indexheader hit';
677         $mset = $ibx->isrch->mset('organization:world');
678         is $mset->size, 0, 'isrch->mset indexheader miss';
679         @args = ();
680         $ibx->isrch->async_mset('organization:world', {} , sub { @args = @_ });
681         is scalar(@args), 2, 'no extra args on indexheader miss';
682         is $args[0]->size, 0, 'isrch->async_mset indexheader miss';
684         $mset = $ibx->isrch->mset('xarchiveshash:deadbeefcafe');
685         is $mset->size, 0, 'isrch->mset does not cross inbox on indexheader';
686         $mset = $ibx->isrch->mset('xarchiveshash:dadfad');
687         is $mset->size, 1, 'isrch->mset hits global indexheader';
689         $es = $pi_cfg->ALL;
690         $mset = $es->mset('xarchiveshash:dadfad');
691         is $mset->size, 1, 'esrch->mset global indexheader hit';
692         $mset = $es->mset('gmane:1234');
693         is $mset->size, 1, '->mset altid hit works globally';
695         $mset = $es->mset('gmane:666');
696         is $mset->size, 0, 'global ->mset hits';
697         $mset = $ibx->isrch->mset('gmane:666');
698         is $mset->size, 0, 'isrch->mset altid miss works';
700         @args = ();
701         $ibx->isrch->async_mset('gmane:666', {} , sub { @args = @_ });
702         is scalar(@args), 2, 'no extra args on altid miss';
703         is $args[0]->size, 0, 'isrch->async_mset altid miss works';
706 if ('max-size') {
707         my $dir = "$home/extindex-max";
708         my $rdr = { 2 => \(my $err) };
709         ok run_script([qw(-extindex --max-size=500 --all -vvv), $dir],
710                         undef, $rdr), 'extindex with max-size';
711         my $es = PublicInbox::ExtSearch->new($dir);
712         my $mset = $es->mset('z:500..');
713         is $mset->size, 0, 'no hits w/ max-size=500';
714         like $err, qr/ skipping [a-f0-9]{40,} .*? > 500\b/,
715                 'noted skipping messages in stderr';
718 done_testing;