2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
6 use PublicInbox::Config;
7 use PublicInbox::InboxWritable;
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;
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"]),
50 my $es = PublicInbox::ExtSearch->new("$home/extindex");
51 ok($es->has_threadid, '->has_threadid');
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');
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;
102 topdir = $home/extindex
104 my $pi_cfg = PublicInbox::Config->new;
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 },
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),
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;
173 is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
174 '2 results in ->ALL');
177 $pi_cfg->each_inbox(sub {
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);
184 is($nr, 2, 'two inboxes');
186 for my $v (qw(v1 v2)) {
187 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
188 my $smsg = $ibx->over->get_art(1);
190 $exp->{"$v.example"} = [ $smsg ];
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'));
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
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');
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"]),
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');
273 my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
274 isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
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;
282 ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
284 is($oidx->max, $max, '->max unchanged');
285 is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
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');
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');
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');
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');
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);
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";
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');
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');
364 is($oidx->max, $oldmax, 'oidx->max unchanged');
366 ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
367 undef, $opt), 'extindex --reindex') or diag explain($opt);
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
385 ok(run_script([qw(-extindex --reindex --rethread --all),
386 "$home/extindex"], undef, $opt),
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),
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),
426 $max = 0 if $max < 0;
427 my @dirs = glob("$d/ei*/?");
428 like($dirs[-1], qr!/ei[0-9]+/$max\z!, '-j works');
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*/?");
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");
451 delete @$es{qw(xdb qp)};
452 is($es->mset('z:0..')->size, $nresult0, 'new shards, same results');
455 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
458 ok(run_script([qw(-xcpdb -R2), $d]), 'xcpdb -R2');
459 @dirs = glob("$d/ei*/?");
461 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] kept");
464 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
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 ])
485 $env = { ORIGINAL_RECIPIENT => $tmp_addr };
486 my $mid = 'tmpmsg@example.com';
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');
499 my $es = PublicInbox::ExtSearch->new("$home/extindex");
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..');
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');
519 is($es->over->next_by_mid($mid, \$id, \$prv), undef,
520 'tmpmsg gone from over');
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');
548 ok(!$_->get_metadata('indexlevel'), 'no indexlevel in >0 shard')
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');
560 ok(!$_->get_metadata('indexlevel'), 'no indexlevel in >0 shard')
562 my $mpi = "$d/ALL.git/objects/pack/multi-pack-index";
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';
574 my $d = "$home/extindex";
575 lei_ok('convert', '-o', "$home/md1", $d);
576 lei_ok('convert', '-o', "$home/md2", "extindex:$d");
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');
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*";
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
607 ok run_script([qw(-extindex --all -vvv), $eidxdir]),
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
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';
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 {
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;
641 Message-ID: <a@example.com>
642 X-Archives-Hash: dadfad
643 Organization: felonious feline family
645 hello world gmane:666
648 PublicInbox::IO::write_file '>>', $cfg_path, <<EOF;
649 [publicinbox "altid-test"]
650 inboxdir = $aibx->{inboxdir}
651 address = b\@example.com
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
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';
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';
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';
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';
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';
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';