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