+ die "No Xapian shards found in $old\n" unless @old_shards;
+
+ my ($src, $max_shard);
+ if (!defined($reshard) || $reshard == scalar(@old_shards)) {
+ # 1:1 copy
+ $max_shard = scalar(@old_shards) - 1;
+ } else {
+ # M:N copy
+ $max_shard = $reshard - 1;
+ $src = [ map { "$old/$_" } @old_shards ];
+ }
+ foreach my $dn (0..$max_shard) {
+ my $tmpl = "$dn-XXXXXXXX";
+ my $wip = File::Temp->newdir($tmpl, DIR => $old);
+ same_fs_or_die($old, $wip->dirname);
+ my $cur = "$old/$dn";
+ push @queue, [ $src // $cur , $wip ];
+ $tmp->{$cur} = $wip;
+ }
+ # mark old shards to be unlinked
+ if ($src) {
+ $tmp->{$_} ||= undef for @$src;
+ }
+ }
+ ($tmp, \@queue);
+}
+
+sub run {
+ my ($ibx, $task, $opt) = @_; # task = 'cpdb' or 'compact'
+ my $cb = \&${\"PublicInbox::Xapcmd::$task"};
+ PublicInbox::Admin::progress_prepare($opt ||= {});
+ defined(my $dir = $ibx->{inboxdir}) or die "no inboxdir defined\n";
+ -d $dir or die "inboxdir=$dir does not exist\n";
+ runnable_or_die($XAPIAN_COMPACT) if $opt->{compact};
+ my $reindex; # v1:{ from => $x40 }, v2:{ from => [ $x40, $x40, .. ] } }
+
+ if (!$opt->{-coarse_lock}) {
+ $reindex = $opt->{reindex} = {};
+ $reindex->{from} = []; # per-epoch ranges
+ require PublicInbox::SearchIdx;
+ PublicInbox::SearchIdx::load_xapian_writable();