]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
index|compact|xcpdb: support --all switch
[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   -L LEVEL            `basic', `medium', or `full' (default: full)
21   --all               index all configured inboxes
22   --compact | -c      run public-inbox-compact(1) after indexing
23   --sequential-shard  index Xapian shards sequentially for slow storage
24   --jobs=NUM          set or disable parallelization (NUM=0)
25   --batch-size=BYTES  flush changes to OS after a given number of bytes
26   --max-size=BYTES    do not index messages larger than the given size
27   --reindex           index previously indexed data (if upgrading)
28   --rethread          regenerate thread IDs (if upgrading, use sparingly)
29   --prune             prune git storage on discontiguous history
30   --verbose | -v      increase verbosity (may be repeated)
31   --help | -?         show this help
32
33 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
34 See public-inbox-index(1) man page for full documentation.
35 EOF
36 my $opt = { quiet => -1, compact => 0, max_size => undef, fsync => 1 };
37 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i prune
38                 fsync|sync! xapian_only|xapian-only
39                 indexlevel|index-level|L=s max_size|max-size=s
40                 batch_size|batch-size=s
41                 sequential_shard|seq-shard|sequential-shard
42                 all help|?))
43         or die "bad command-line args\n$usage";
44 if ($opt->{help}) { print $help; exit 0 };
45 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
46 if ($opt->{xapian_only} && !$opt->{reindex}) {
47         die "--xapian-only requires --reindex\n";
48 }
49
50 # require lazily to speed up --help
51 require PublicInbox::Admin;
52 PublicInbox::Admin::require_or_die('-index');
53
54 my $cfg = PublicInbox::Config->new; # Config is loaded by Admin
55 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
56 PublicInbox::Admin::require_or_die('-index');
57 unless (@ibxs) { print STDERR "Usage: $usage\n"; exit 1 }
58
59 my $mods = {};
60 foreach my $ibx (@ibxs) {
61         # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
62         $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapian_only} ?
63                         'full' : PublicInbox::Admin::detect_indexlevel($ibx));
64         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
65 }
66
67 PublicInbox::Admin::require_or_die(keys %$mods);
68 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
69 local %ENV = (%ENV, %$env) if $env;
70 require PublicInbox::InboxWritable;
71 PublicInbox::Admin::progress_prepare($opt);
72 for my $ibx (@ibxs) {
73         $ibx = PublicInbox::InboxWritable->new($ibx);
74         if ($opt->{compact} >= 2) {
75                 PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
76         }
77         $ibx->{-no_fsync} = 1 if !$opt->{fsync};
78
79         my $ibx_opt = $opt;
80         if (defined(my $s = $ibx->{lc('indexSequentialShard')})) {
81                 defined(my $v = $cfg->git_bool($s)) or die <<EOL;
82 publicInbox.$ibx->{name}.indexSequentialShard not boolean
83 EOL
84                 $ibx_opt = { %$opt, sequential_shard => $v };
85         }
86         PublicInbox::Admin::index_inbox($ibx, undef, $ibx_opt);
87         if (my $copt = $opt->{compact_opt}) {
88                 local $copt->{jobs} = 0 if $ibx_opt->{sequential_shard};
89                 PublicInbox::Xapcmd::run($ibx, 'compact', $copt);
90         }
91 }