2 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 use PublicInbox::V2Writable;
8 use PublicInbox::Search;
9 use PublicInbox::Config;
11 use File::Temp qw(tempdir);
12 use File::Path qw(remove_tree);
13 use PublicInbox::Spawn qw(spawn);
14 my $usage = "Usage: public-inbox-compact REPO_DIR\n";
15 my $dir = shift or die $usage;
16 my $config = PublicInbox::Config->new;
18 $config->each_inbox(sub {
19 $ibx = $_[0] if abs_path($_[0]->{mainrepo}) eq $dir
22 warn "W: $dir not configured in ".
23 PublicInbox::Config::default_file() . "\n";
27 address => [ 'old@example.com' ],
29 $ibx = PublicInbox::Inbox->new($ibx);
31 my $v = ($ibx->{version} || 1);
33 require PublicInbox::V2Writable;
34 my $v2w = PublicInbox::V2Writable->new($ibx);
35 my $xap_v = 'xap'.PublicInbox::Search::SCHEMA_VERSION;
36 my $xroot = "$ibx->{mainrepo}/$xap_v";
37 opendir my $dh, $xroot or die "Failed to opendir $xroot: $!\n";
39 my $new = tempdir(CLEANUP => 1, DIR => $ibx->{mainrepo});
42 while (defined(my $dn = readdir($dh))) {
43 if ($dn =~ /\A\d+\z/) {
44 push @parts, "$xroot/$dn";
45 } elsif ($dn eq 'skel') {
47 } elsif ($dn eq '.' || $dn eq '..') {
49 warn "W: skipping unknown Xapian DB: $xroot/$dn\n";
55 my $pid = spawn([ qw(xapian-compact), @parts, "$new/0" ]);
56 defined $pid or die "compact failed: $?\n";
57 $pids{$pid} = 'xapian-compact (parts)';
59 warn "No parts found in $xroot\n";
62 my $pid = spawn([ qw(xapian-compact), $skel, "$new/skel" ]);
63 defined $pid or die "compact failed: $?\n";
64 $pids{$pid} = 'xapian-compact (skel)';
66 warn "$xroot/skel missing\n";
68 die "No xapian-compact processes running\n" unless scalar keys %pids;
69 while (scalar keys %pids) {
70 my $pid = waitpid(-1, 0);
71 my $desc = delete $pids{$pid};
72 die "$desc failed: $?\n" if $?;
74 rename($xroot, "$new/old") or die "rename $xroot => $new/old: $!\n";
75 rename($new, $xroot) or die "rename $new => $xroot: $!\n";
77 remove_tree("$xroot/old") or die "failed to remove $xroot/old: $!\n";
79 require PublicInbox::Import;
80 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
81 my $xap_v = 'xapian'.PublicInbox::Search::SCHEMA_VERSION;
82 my $v1_root = "$ibx->{mainrepo}/public-inbox";
83 my $old = "$v1_root/$xap_v";
84 -d $old or die "$old does not exist\n";
85 my $new = tempdir(CLEANUP => 1, DIR => $v1_root);
87 PublicInbox::Import::run_die([ qw(xapian-compact), $old, $new ]);
88 rename($old, "$new/old") or die "rename $old => $new: $!\n";
89 rename($new, $old) or die "rename $new => $old: $!\n";
91 remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
93 die "Unsupported inbox version: $v\n";