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
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
14 Create and update search indices
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)
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.
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))
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";
48 # require lazily to speed up --help
49 require PublicInbox::Admin;
50 PublicInbox::Admin::require_or_die('-index');
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 }
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} ?
64 PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
67 # "Search::Xapian" includes SWIG "Xapian", too:
68 $opt->{compact} = 0 if !$mods->{'Search::Xapian'};
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);
77 $ibx = PublicInbox::InboxWritable->new($ibx);
78 if ($opt->{compact} >= 2) {
79 PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
81 $ibx->{-no_fsync} = 1 if !$opt->{fsync};
82 $ibx->{-skip_docdata} //= $opt->{'skip-docdata'};
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
89 $ibx_opt = { %$opt, sequential_shard => $v };
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);