]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
Merge remote-tracking branch 'origin/purge'
[public-inbox.git] / script / public-inbox-index
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Basic tool to create a Xapian search index for a git repository
5 # configured for public-inbox.
6 # Usage with libeatmydata <https://www.flamingspork.com/projects/libeatmydata/>
7 # highly recommended: eatmydata public-inbox-index REPO_DIR
8
9 use strict;
10 use warnings;
11 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
12 my $usage = "public-inbox-index REPO_DIR";
13 use PublicInbox::Config;
14 use PublicInbox::Admin qw(resolve_repo_dir);
15
16 my $config = eval { PublicInbox::Config->new } || eval {
17         warn "public-inbox unconfigured for serving, indexing anyways...\n";
18         undef;
19 };
20 eval { require PublicInbox::SearchIdx };
21 if ($@) {
22         print STDERR "Search::Xapian required for $0\n";
23         exit 1;
24 }
25
26 my $reindex;
27 my $prune;
28 my $jobs = undef;
29 my %opts = (
30         '--reindex' => \$reindex,
31         '--jobs|j=i' => \$jobs,
32         '--prune' => \$prune,
33 );
34 GetOptions(%opts) or die "bad command-line args\n$usage";
35 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
36
37 my @dirs;
38
39 if (@ARGV) {
40         @dirs = map { resolve_repo_dir($_) } @ARGV;
41 } else {
42         @dirs = (resolve_repo_dir());
43 }
44
45 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
46 usage() unless @dirs;
47
48 defined($config) and $config->each_inbox(sub {
49         my ($ibx) = @_;
50
51         for my $i (0..$#dirs) {
52                 next if $dirs[$i] ne $ibx->{mainrepo};
53                 $dirs[$i] = $ibx;
54         }
55 });
56
57 foreach my $dir (@dirs) {
58         if (!ref($dir) && -f "$dir/inbox.lock") { # v2
59                 my $ibx = { mainrepo => $dir, name => 'unnamed' };
60                 $dir = PublicInbox::Inbox->new($ibx);
61         }
62         index_dir($dir);
63 }
64
65 sub index_dir {
66         my ($repo) = @_;
67         if (!ref $repo && ! -d $repo) {
68                 die "$repo does not appear to be an inbox repository\n";
69         }
70         if (ref($repo) && ($repo->{version} || 1) == 2) {
71                 eval { require PublicInbox::V2Writable };
72                 die "v2 requirements not met: $@\n" if $@;
73                 my $v2w = eval {
74                         $jobs and local $ENV{NPROC} = $jobs;
75                         PublicInbox::V2Writable->new($repo);
76                 };
77                 if (defined $jobs) {
78                         if ($jobs == 0) {
79                                 $v2w->{parallel} = 0;
80                         } else {
81                                 my $n = $v2w->{partitions};
82                                 if ($jobs != ($n + 1)) {
83                                         warn
84 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
85                                 }
86                         }
87                 }
88                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
89         } else {
90                 my $s = PublicInbox::SearchIdx->new($repo, 1);
91                 $s->index_sync({ reindex => $reindex });
92         }
93 }