1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
10 use PublicInbox::InboxWritable;
11 require PublicInbox::Search;
13 my $mime = PublicInbox::Eml->new(<<'EOF');
16 Subject: this is a subject
17 Date: Fri, 02 Oct 1993 00:00:00 +0000
20 my ($this) = (split('/', $0))[-1];
21 my ($tmpdir, $for_destroy) = tmpdir();
22 local $ENV{PI_CONFIG} = "$tmpdir/config";
23 my $ibx = PublicInbox::Inbox->new({
24 inboxdir => "$tmpdir/testbox",
27 -primary_address => 'test@example.com',
28 indexlevel => 'medium',
30 my @xcpdb = qw(-xcpdb -q);
33 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer;
34 for my $i (1..$ndoc) {
35 $mime->header_set('Message-ID', "<m$i\@example.com>");
36 ok($im->add($mime), "message $i added");
39 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
40 is(scalar(@shards), $nproc - 1, 'got expected shards');
41 my $orig = $ibx->over->query_xover(1, $ndoc);
42 my %nums = map {; "$_->{num}" => 1 } @$orig;
44 # ensure we can go up or down in shards, or stay the same:
45 for my $R (qw(2 4 1 3 3)) {
46 delete $ibx->{search}; # release old handles
47 my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
48 push @$cmd, '--compact' if $R == 1 && have_xapian_compact;
49 ok(run_script($cmd), "xcpdb -R$R");
50 my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
51 is(scalar(@new_shards), $R, 'resharded to two shards');
52 my $mset = $ibx->search->mset('s:this');
53 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
54 is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
55 my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
56 ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
58 delete $ibx->{search}; # release old handles
60 # ensure docids in Xapian match NNTP article numbers
63 my $XapianDatabase = do {
65 $PublicInbox::Search::X{Database};
67 foreach my $d (@new_shards) {
68 my $xdb = $XapianDatabase->new($d);
69 $tot += $xdb->get_doccount;
70 my $it = $xdb->postlist_begin('');
71 my $end = $xdb->postlist_end('');
72 for (; $it != $end; $it++) {
73 my $docid = $it->get_docid;
74 if ($xdb->get_document($docid)) {
75 ok(delete($tmp{$docid}), "saw #$docid");
79 is(scalar keys %tmp, 0, 'all docids seen');