]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
index: add built-in --help / -?
[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 $usage = 'public-inbox-index [options] INBOX_DIR';
12 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
13 usage: $usage
14
15   Create and update search indices
16
17 options:
18
19   --no-fsync          speed up indexing, risk corruption on power outage
20   --indexlevel=LEVEL  `basic', 'medium', or `full' (default: full)
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   --help | -?         show this help
31
32 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
33 See public-inbox-index(1) man page for full documentation.
34 EOF
35 my $compact_opt;
36 my $opt = { quiet => -1, compact => 0, maxsize => undef, fsync => 1 };
37 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i prune
38                 fsync|sync! xapianonly|xapian-only
39                 indexlevel|L=s maxsize|max-size=s batchsize|batch-size=s
40                 sequentialshard|seq-shard|sequential-shard
41                 help|?))
42         or die "bad command-line args\n$usage";
43 if ($opt->{help}) { print $help; exit 0 };
44 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
45
46 # require lazily to speed up --help
47 require PublicInbox::Admin;
48 PublicInbox::Admin::require_or_die('-index');
49
50 if ($opt->{compact}) {
51         require PublicInbox::Xapcmd;
52         PublicInbox::Xapcmd::check_compact();
53         $compact_opt = { -coarse_lock => 1, compact => 1 };
54         if (defined(my $jobs = $opt->{jobs})) {
55                 $compact_opt->{jobs} = $jobs;
56         }
57 }
58
59 my $cfg = PublicInbox::Config->new; # Config is loaded by Admin
60 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, undef, $cfg);
61 PublicInbox::Admin::require_or_die('-index');
62 unless (@ibxs) { print STDERR "Usage: $usage\n"; exit 1 }
63
64 my $max_size = $opt->{maxsize} // $cfg->{lc('publicInbox.indexMaxSize')};
65 if (defined $max_size) {
66         PublicInbox::Admin::parse_unsigned(\$max_size) or
67                 die "`publicInbox.indexMaxSize=$max_size' not parsed\n";
68 }
69
70 my $bs = $opt->{batchsize} // $cfg->{lc('publicInbox.indexBatchSize')};
71 if (defined $bs) {
72         PublicInbox::Admin::parse_unsigned(\$bs) or
73                 die "`publicInbox.indexBatchSize=$bs' not parsed\n";
74 }
75 no warnings 'once';
76 local $PublicInbox::SearchIdx::BATCH_BYTES = $bs if defined($bs);
77 use warnings 'once';
78
79 # out-of-the-box builds of Xapian 1.4.x are still limited to 32-bit
80 # https://getting-started-with-xapian.readthedocs.io/en/latest/concepts/indexing/limitations.html
81 local $ENV{XAPIAN_FLUSH_THRESHOLD} ||= '4294967295' if defined($bs);
82
83 my $s = $opt->{sequentialshard} //
84                         $cfg->{lc('publicInbox.indexSequentialShard')};
85 if (defined $s) {
86         my $v = $cfg->git_bool($s);
87         defined($v) or
88                 die "`publicInbox.indexSequentialShard=$s' not boolean\n";
89         $opt->{sequentialshard} = $v;
90 }
91
92 my $mods = {};
93 foreach my $ibx (@ibxs) {
94         # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
95         $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapianonly} ?
96                         'full' : PublicInbox::Admin::detect_indexlevel($ibx));
97         $ibx->{index_max_size} = $max_size;
98         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
99 }
100
101 PublicInbox::Admin::require_or_die(keys %$mods);
102 require PublicInbox::InboxWritable;
103 PublicInbox::Admin::progress_prepare($opt);
104 for my $ibx (@ibxs) {
105         $ibx = PublicInbox::InboxWritable->new($ibx);
106         if ($opt->{compact} >= 2) {
107                 PublicInbox::Xapcmd::run($ibx, 'compact', $compact_opt);
108         }
109         $ibx->{-no_fsync} = 1 if !$opt->{fsync};
110
111         my $ibx_opt = $opt;
112         if (defined(my $s = $ibx->{indexsequentialshard})) {
113                 defined(my $v = $cfg->git_bool($s)) or die <<EOL;
114 publicInbox.$ibx->{name}.indexSequentialShard not boolean
115 EOL
116                 $ibx_opt = { %$opt, sequentialshard => $v };
117         }
118         PublicInbox::Admin::index_inbox($ibx, undef, $ibx_opt);
119         if ($compact_opt) {
120                 local $compact_opt->{jobs} = 0 if $ibx_opt->{sequentialshard};
121                 PublicInbox::Xapcmd::run($ibx, 'compact', $compact_opt);
122         }
123 }