1 # Copyright (C) 2019 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;
12 my $mime = PublicInbox::MIME->create(
14 From => 'a@example.com',
15 To => 'test@example.com',
16 Subject => 'this is a subject',
17 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
22 my ($this) = (split('/', $0))[-1];
23 my ($tmpdir, $for_destroy) = tmpdir();
24 local $ENV{PI_CONFIG} = "$tmpdir/config";
25 my $ibx = PublicInbox::Inbox->new({
26 inboxdir => "$tmpdir/testbox",
29 -primary_address => 'test@example.com',
30 indexlevel => 'medium',
32 my @xcpdb = qw(-xcpdb -q);
35 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer(1);
36 for my $i (1..$ndoc) {
37 $mime->header_set('Message-ID', "<m$i\@example.com>");
38 ok($im->add($mime), "message $i added");
41 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
42 is(scalar(@shards), $nproc, 'got expected shards');
43 my $orig = $ibx->over->query_xover(1, $ndoc);
44 my %nums = map {; "$_->{num}" => 1 } @$orig;
46 # ensure we can go up or down in shards, or stay the same:
47 for my $R (qw(2 4 1 3 3)) {
48 delete $ibx->{search}; # release old handles
49 my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
50 push @$cmd, '--compact' if $R == 1;
51 ok(run_script($cmd), "xcpdb -R$R");
52 my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
53 is(scalar(@new_shards), $R, 'resharded to two shards');
54 my $msgs = $ibx->search->query('s:this');
55 is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
56 my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
57 ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
59 delete $ibx->{search}; # release old handles
61 # ensure docids in Xapian match NNTP article numbers
64 foreach my $d (@new_shards) {
65 my $xdb = Search::Xapian::Database->new($d);
66 $tot += $xdb->get_doccount;
67 my $it = $xdb->postlist_begin('');
68 my $end = $xdb->postlist_end('');
69 for (; $it != $end; $it++) {
70 my $docid = $it->get_docid;
71 if ($xdb->get_document($docid)) {
72 ok(delete($tmp{$docid}), "saw #$docid");
76 is(scalar keys %tmp, 0, 'all docids seen');