]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
admin: hoist out resolve_inboxes for -compact and -index
[public-inbox.git] / script / public-inbox-index
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2019 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::Admin;
14 PublicInbox::Admin::require_or_die('-index');
15
16 my $reindex;
17 my $prune;
18 my $jobs = undef;
19 my $indexlevel;
20 my %opts = (
21         '--reindex' => \$reindex,
22         '--jobs|j=i' => \$jobs,
23         '--prune' => \$prune,
24         'L|indexlevel=s' => \$indexlevel,
25 );
26 GetOptions(%opts) or die "bad command-line args\n$usage";
27 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
28
29 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
30
31 # do we really care about this message?  It's annoying...
32 my $warn = 'public-inbox unconfigured for serving, indexing anyways...';
33 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $warn);
34 PublicInbox::Admin::require_or_die('-index');
35 usage() unless @ibxs;
36 my $mods = {};
37 foreach my $ibx (@ibxs) {
38         if (defined $indexlevel && !defined($ibx->{indexlevel})) {
39                 # XXX: users can shoot themselves in the foot, with this...
40                 $ibx->{indexlevel} = $indexlevel;
41         }
42         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
43 }
44
45 PublicInbox::Admin::require_or_die(keys %$mods);
46
47 require PublicInbox::SearchIdx;
48 index_inbox($_) for @ibxs;
49
50 sub index_inbox {
51         my ($repo) = @_;
52         if (ref($repo) && ($repo->{version} || 1) == 2) {
53                 eval { require PublicInbox::V2Writable };
54                 die "v2 requirements not met: $@\n" if $@;
55                 my $v2w = eval {
56                         PublicInbox::V2Writable->new($repo, {nproc=>$jobs});
57                 };
58                 if (defined $jobs) {
59                         if ($jobs == 0) {
60                                 $v2w->{parallel} = 0;
61                         } else {
62                                 my $n = $v2w->{partitions};
63                                 if ($jobs != ($n + 1)) {
64                                         warn
65 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
66                                 }
67                         }
68                 }
69                 local $SIG{__WARN__} = sub {
70                         print STDERR $v2w->{current_info}, ': ', @_;
71                 };
72                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
73         } else {
74                 my $s = PublicInbox::SearchIdx->new($repo, 1);
75                 $s->index_sync({ reindex => $reindex });
76         }
77 }