]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
lazy load Xapian and make it optional for v2
[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 $indexlevel;
30 my %opts = (
31         '--reindex' => \$reindex,
32         '--jobs|j=i' => \$jobs,
33         '--prune' => \$prune,
34         'L|indexlevel=s' => \$indexlevel,
35 );
36 GetOptions(%opts) or die "bad command-line args\n$usage";
37 die "--jobs must be positive\n" if defined $jobs && $jobs < 0;
38
39 my @dirs;
40
41 if (@ARGV) {
42         @dirs = map { resolve_repo_dir($_) } @ARGV;
43 } else {
44         @dirs = (resolve_repo_dir());
45 }
46
47 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
48 usage() unless @dirs;
49
50 defined($config) and $config->each_inbox(sub {
51         my ($ibx) = @_;
52
53         for my $i (0..$#dirs) {
54                 next if $dirs[$i] ne $ibx->{mainrepo};
55                 $dirs[$i] = $ibx;
56         }
57 });
58
59 foreach my $dir (@dirs) {
60         if (!ref($dir)) {
61                 unless (-d $dir) {
62                         die "$dir does not appear to be an inbox repository\n";
63                 }
64                 my $ibx = {
65                         mainrepo => $dir,
66                         name => 'unnamed',
67                         indexlevel => $indexlevel,
68                         version => -f "$dir/inbox.lock" ? 2 : 1,
69                 };
70                 $dir = PublicInbox::Inbox->new($ibx);
71         } elsif (defined $indexlevel && !defined($dir->{indexlevel})) {
72                 # XXX: users can shoot themselves in the foot, with this...
73                 $dir->{indexlevel} = $indexlevel;
74         }
75
76         index_inbox($dir);
77 }
78
79 sub index_inbox {
80         my ($repo) = @_;
81         if (ref($repo) && ($repo->{version} || 1) == 2) {
82                 eval { require PublicInbox::V2Writable };
83                 die "v2 requirements not met: $@\n" if $@;
84                 my $v2w = eval {
85                         PublicInbox::V2Writable->new($repo, {nproc=>$jobs});
86                 };
87                 if (defined $jobs) {
88                         if ($jobs == 0) {
89                                 $v2w->{parallel} = 0;
90                         } else {
91                                 my $n = $v2w->{partitions};
92                                 if ($jobs != ($n + 1)) {
93                                         warn
94 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
95                                 }
96                         }
97                 }
98                 local $SIG{__WARN__} = sub {
99                         print STDERR $v2w->{current_info}, ': ', @_;
100                 };
101                 $v2w->index_sync({ reindex => $reindex, prune => $prune });
102         } else {
103                 my $s = PublicInbox::SearchIdx->new($repo, 1);
104                 $s->index_sync({ reindex => $reindex });
105         }
106 }