]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-compact
compact: better handling of over.sqlite3* files
[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 my $usage = "Usage: public-inbox-compact REPO_DIR\n";
14 my $dir = shift or die $usage;
15 my $config = PublicInbox::Config->new;
16 my $ibx;
17 $dir = abs_path($dir);
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 $ibx = PublicInbox::InboxWritable->new($ibx);
33 $ibx->umask_prepare;
34
35 sub commit_changes ($$$) {
36         my ($im, $old, $new) = @_;
37         my @st = stat($old) or die "failed to stat($old): $!\n";
38
39         for my $suf (qw(.pipe.lock -journal)) {
40                 my $orig = "$old/over.sqlite3$suf";
41                 link($orig, "$new/over.sqlite3$suf") and next;
42                 next if $!{ENOENT};
43                 die "failed to link $orig => $new/over.sqlite3$suf: $!\n";
44         }
45         link("$old/over.sqlite3", "$new/over.sqlite3") or die
46                 "failed to link {$old => $new}/over.sqlite3: $!\n";
47
48         rename($old, "$new/old") or die "rename $old => $new/old: $!\n";
49         chmod($st[2] & 07777, $new) or die "chmod $old: $!\n";
50         rename($new, $old) or die "rename $new => $old: $!\n";
51         $im->lock_release;
52         remove_tree("$old/old") or die "failed to remove $old/old: $!\n";
53 }
54
55 if ($v == 2) {
56         require PublicInbox::V2Writable;
57         my $v2w = PublicInbox::V2Writable->new($ibx);
58         my $xap_v = 'xap'.PublicInbox::Search::SCHEMA_VERSION;
59         my $old = "$dir/$xap_v";
60         opendir my $dh, $old or die "Failed to opendir $old: $!\n";
61         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $dir);
62         $ibx->with_umask(sub {
63                 $v2w->lock_acquire;
64                 my @parts;
65                 while (defined(my $dn = readdir($dh))) {
66                         if ($dn =~ /\A\d+\z/) {
67                                 push @parts, "$old/$dn";
68                         } elsif ($dn eq '.' || $dn eq '..') {
69                         } elsif ($dn =~ /\Aover\.sqlite3/) {
70                         } else {
71                                 warn "W: skipping unknown Xapian DB: $old/$dn\n"
72                         }
73                 }
74                 close $dh;
75                 die "No Xapian parts found in $old\n" unless @parts;
76                 my $cmd = ['xapian-compact', @parts, "$new/0" ];
77                 PublicInbox::Import::run_die($cmd);
78                 commit_changes($v2w, $old, $new);
79         });
80 } elsif ($v == 1) {
81         require PublicInbox::Import;
82         my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
83         my $xap_v = 'xapian'.PublicInbox::Search::SCHEMA_VERSION;
84         my $v1_root = "$dir/public-inbox";
85         my $old = "$v1_root/$xap_v";
86         -d $old or die "$old does not exist\n";
87         my $new = tempdir('compact-XXXXXXXX', CLEANUP => 1, DIR => $v1_root);
88         $ibx->with_umask(sub {
89                 $im->lock_acquire;
90                 PublicInbox::Import::run_die(['xapian-compact', $old, $new]);
91                 commit_changes($im, $old, $new);
92         });
93 } else {
94         die "Unsupported inbox version: $v\n";
95 }