]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
script/*: fold $usage into $help, support `-h' instead of -?
[public-inbox.git] / script / public-inbox-index
1 #!perl -w
2 # Copyright (C) 2015-2020 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 public-inbox.
5 # Usage with libeatmydata <https://www.flamingspork.com/projects/libeatmydata/>
6 # highly recommended: eatmydata public-inbox-index INBOX_DIR
7
8 use strict;
9 use v5.10.1;
10 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
11 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
12 usage: public-inbox-index [options] INBOX_DIR
13
14   Create and update search indices
15
16 options:
17
18   --no-fsync          speed up indexing, risk corruption on power outage
19   -L LEVEL            `basic', `medium', or `full' (default: full)
20   --all               index all configured inboxes
21   --compact | -c      run public-inbox-compact(1) after indexing
22   --sequential-shard  index Xapian shards sequentially for slow storage
23   --jobs=NUM          set or disable parallelization (NUM=0)
24   --batch-size=BYTES  flush changes to OS after a given number of bytes
25   --max-size=BYTES    do not index messages larger than the given size
26   --reindex           index previously indexed data (if upgrading)
27   --rethread          regenerate thread IDs (if upgrading, use sparingly)
28   --prune             prune git storage on discontiguous history
29   --verbose | -v      increase verbosity (may be repeated)
30
31 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
32 See public-inbox-index(1) man page for full documentation.
33 EOF
34 my $opt = { quiet => -1, compact => 0, max_size => undef, fsync => 1 };
35 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i prune
36                 fsync|sync! xapian_only|xapian-only
37                 indexlevel|index-level|L=s max_size|max-size=s
38                 batch_size|batch-size=s
39                 sequential_shard|seq-shard|sequential-shard
40                 skip-docdata all help|h))
41         or die $help;
42 if ($opt->{help}) { print $help; exit 0 };
43 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
44 if ($opt->{xapian_only} && !$opt->{reindex}) {
45         die "--xapian-only requires --reindex\n";
46 }
47
48 # require lazily to speed up --help
49 require PublicInbox::Admin;
50 PublicInbox::Admin::require_or_die('-index');
51
52 my $cfg = PublicInbox::Config->new; # Config is loaded by Admin
53 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
54 PublicInbox::Admin::require_or_die('-index');
55 unless (@ibxs) { print STDERR $help; exit 1 }
56
57 my $mods = {};
58 foreach my $ibx (@ibxs) {
59         # detect_indexlevel may also set $ibx->{-skip_docdata}
60         my $detected = PublicInbox::Admin::detect_indexlevel($ibx);
61         # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
62         $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapian_only} ?
63                         'full' : $detected);
64         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
65 }
66
67 # "Search::Xapian" includes SWIG "Xapian", too:
68 $opt->{compact} = 0 if !$mods->{'Search::Xapian'};
69
70 PublicInbox::Admin::require_or_die(keys %$mods);
71 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
72 local %ENV = (%ENV, %$env) if $env;
73 require PublicInbox::InboxWritable;
74 PublicInbox::Xapcmd::check_compact() if $opt->{compact};
75 PublicInbox::Admin::progress_prepare($opt);
76 for my $ibx (@ibxs) {
77         $ibx = PublicInbox::InboxWritable->new($ibx);
78         if ($opt->{compact} >= 2) {
79                 PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
80         }
81         $ibx->{-no_fsync} = 1 if !$opt->{fsync};
82         $ibx->{-skip_docdata} //= $opt->{'skip-docdata'};
83
84         my $ibx_opt = $opt;
85         if (defined(my $s = $ibx->{lc('indexSequentialShard')})) {
86                 defined(my $v = $cfg->git_bool($s)) or die <<EOL;
87 publicInbox.$ibx->{name}.indexSequentialShard not boolean
88 EOL
89                 $ibx_opt = { %$opt, sequential_shard => $v };
90         }
91         PublicInbox::Admin::index_inbox($ibx, undef, $ibx_opt);
92         if (my $copt = $opt->{compact_opt}) {
93                 local $copt->{jobs} = 0 if $ibx_opt->{sequential_shard};
94                 PublicInbox::Xapcmd::run($ibx, 'compact', $copt);
95         }
96 }