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 my @mods = qw(DBI DBD::SQLite Search::Xapian);
7 foreach my $mod (@mods) {
9 plan skip_all => "missing $mod for $0" if $@;
11 require './t/common.perl';
13 use PublicInbox::MIME;
14 use PublicInbox::InboxWritable;
16 my $mime = PublicInbox::MIME->create(
18 From => 'a@example.com',
19 To => 'test@example.com',
20 Subject => 'this is a subject',
21 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
26 my ($this) = (split('/', $0))[-1];
27 my ($tmpdir, $for_destroy) = tmpdir();
28 my $ibx = PublicInbox::Inbox->new({
29 inboxdir => "$tmpdir/testbox",
32 -primary_address => 'test@example.com',
33 indexlevel => 'medium',
35 my @xcpdb = qw(-xcpdb -q);
38 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer(1);
39 for my $i (1..$ndoc) {
40 $mime->header_set('Message-ID', "<m$i\@example.com>");
41 ok($im->add($mime), "message $i added");
44 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
45 is(scalar(@shards), $nproc, 'got expected shards');
46 my $orig = $ibx->over->query_xover(1, $ndoc);
47 my %nums = map {; "$_->{num}" => 1 } @$orig;
49 # ensure we can go up or down in shards, or stay the same:
50 for my $R (qw(2 4 1 3 3)) {
51 delete $ibx->{search}; # release old handles
52 my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
53 push @$cmd, '--compact' if $R == 1;
54 ok(run_script($cmd), "xcpdb -R$R");
55 my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
56 is(scalar(@new_shards), $R, 'resharded to two shards');
57 my $msgs = $ibx->search->query('s:this');
58 is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
59 my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
60 ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
62 delete $ibx->{search}; # release old handles
64 # ensure docids in Xapian match NNTP article numbers
67 foreach my $d (@new_shards) {
68 my $xdb = Search::Xapian::Database->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');