]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-compact
016873d31e23fd93afc90e929bb6378f42543392
[public-inbox.git] / script / public-inbox-compact
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use warnings;
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;
10 use Cwd 'abs_path';
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;
17 my $ibx;
18 $config->each_inbox(sub {
19         $ibx = $_[0] if abs_path($_[0]->{mainrepo}) eq $dir
20 });
21 unless ($ibx) {
22         warn "W: $dir not configured in ".
23                 PublicInbox::Config::default_file() . "\n";
24         $ibx = {
25                 mainrepo => $dir,
26                 name => 'ignored',
27                 address => [ 'old@example.com' ],
28         };
29         $ibx = PublicInbox::Inbox->new($ibx);
30 }
31 my $v = ($ibx->{version} || 1);
32 if ($v == 2) {
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";
38         $v2w->lock_acquire;
39         my $new = tempdir(CLEANUP => 1, DIR => $ibx->{mainrepo});
40         my @parts;
41         my $skel;
42         while (defined(my $dn = readdir($dh))) {
43                 if ($dn =~ /\A\d+\z/) {
44                         push @parts, "$xroot/$dn";
45                 } elsif ($dn eq 'skel') {
46                         $skel = "$xroot/$dn";
47                 } elsif ($dn eq '.' || $dn eq '..') {
48                 } else {
49                         warn "W: skipping unknown Xapian DB: $xroot/$dn\n";
50                 }
51         }
52         close $dh;
53         my %pids;
54         if (@parts) {
55                 my $pid = spawn([ qw(xapian-compact), @parts, "$new/0" ]);
56                 defined $pid or die "compact failed: $?\n";
57                 $pids{$pid} = 'xapian-compact (parts)';
58         } else {
59                 warn "No parts found in $xroot\n";
60         }
61         if (defined $skel) {
62                 my $pid = spawn([ qw(xapian-compact), $skel, "$new/skel" ]);
63                 defined $pid or die "compact failed: $?\n";
64                 $pids{$pid} = 'xapian-compact (skel)';
65         } else {
66                 warn "$xroot/skel missing\n";
67         }
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 $?;
73         }
74         rename($xroot, "$new/old") or die "rename $xroot => $new/old: $!\n";
75         rename($new, $xroot) or die "rename $new => $xroot: $!\n";
76         $v2w->lock_release;
77         remove_tree("$xroot/old") or die "failed to remove $xroot/old: $!\n";
78 } elsif ($v == 1) {
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);
86         $im->lock_acquire;
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";
90         $im->lock_release;
91         remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
92 } else {
93         die "Unsupported inbox version: $v\n";
94 }