2 # Copyright (C) 2015-2021 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 per-inbox search indices
18 --no-fsync speed up indexing, risk corruption on power outage
19 -L LEVEL `basic', `medium', or `full' (default: full)
20 -E EXTINDEX update extindex (default: `all')
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)
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.
36 quiet => -1, compact => 0, max_size => undef, fsync => 1,
37 'update-extindex' => [], # ":s@" optional arg sets '' if no arg given
39 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i prune
40 fsync|sync! xapian_only|xapian-only
41 indexlevel|index-level|L=s max_size|max-size=s
42 batch_size|batch-size=s
43 sequential-shard|seq-shard
44 no-update-extindex update-extindex|E=s@
45 fast-noop|F skip-docdata all help|h))
47 if ($opt->{help}) { print $help; exit 0 };
48 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
49 if ($opt->{xapian_only} && !$opt->{reindex}) {
50 die "--xapian-only requires --reindex\n";
52 if ($opt->{reindex} && delete($opt->{'fast-noop'})) {
53 warn "--fast-noop ignored with --reindex\n";
56 # require lazily to speed up --help
57 require PublicInbox::Admin;
58 PublicInbox::Admin::require_or_die('-index');
60 my $cfg = PublicInbox::Config->new; # Config is loaded by Admin
62 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
63 PublicInbox::Admin::require_or_die('-index');
64 unless (@ibxs) { print STDERR $help; exit 1 }
66 my (@eidx, %eidx_seen);
67 my $update_extindex = $opt->{'update-extindex'};
68 if (!scalar(@$update_extindex) && (my $ALL = $cfg->ALL)) {
69 # extindex and normal inboxes may have different owners
70 push(@$update_extindex, 'all') if -w $ALL->{topdir};
72 @$update_extindex = () if $opt->{'no-update-extindex'};
73 if (scalar @$update_extindex) {
74 PublicInbox::Admin::require_or_die('-search');
75 require PublicInbox::ExtSearchIdx;
77 for my $ei_name (@$update_extindex) {
78 my $es = $cfg->lookup_ei($ei_name);
80 if (!$es && -d $ei_name) { # allow dirname or config section name
83 $topdir = $es->{topdir};
85 die "extindex `$ei_name' not configured or found\n";
88 delete $o->{indexlevel} if ($o->{indexlevel}//'') eq 'basic';
89 $eidx_seen{$topdir} //=
90 push(@eidx, PublicInbox::ExtSearchIdx->new($topdir, $o));
93 my @eidx_unconfigured;
94 foreach my $ibx (@ibxs) {
95 # detect_indexlevel may also set $ibx->{-skip_docdata}
96 my $detected = PublicInbox::Admin::detect_indexlevel($ibx);
97 # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
98 $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapian_only} ?
100 PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
101 if (@eidx && $ibx->{-unconfigured}) {
102 push @eidx_unconfigured, " $ibx->{inboxdir}\n";
105 warn <<EOF if @eidx_unconfigured;
106 The following inboxes are unconfigured and will not be updated in
107 @$update_extindex:\n@eidx_unconfigured
110 # "Search::Xapian" includes SWIG "Xapian", too:
111 $opt->{compact} = 0 if !$mods->{'Search::Xapian'};
113 PublicInbox::Admin::require_or_die(keys %$mods);
114 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
115 local %ENV = (%ENV, %$env) if $env;
116 require PublicInbox::InboxWritable;
117 PublicInbox::Xapcmd::check_compact() if $opt->{compact};
118 PublicInbox::Admin::progress_prepare($opt);
119 for my $ibx (@ibxs) {
120 $ibx = PublicInbox::InboxWritable->new($ibx);
121 if ($opt->{compact} >= 2) {
122 PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
124 $ibx->{-no_fsync} = 1 if !$opt->{fsync};
125 $ibx->{-skip_docdata} //= $opt->{'skip-docdata'};
128 if (defined(my $s = $ibx->{lc('indexSequentialShard')})) {
129 defined(my $v = $cfg->git_bool($s)) or die <<EOL;
130 publicInbox.$ibx->{name}.indexSequentialShard not boolean
132 $ibx_opt = { %$opt, 'sequential-shard' => $v };
134 my $nidx = PublicInbox::Admin::index_inbox($ibx, undef, $ibx_opt);
135 last if $ibx_opt->{quit};
136 if (my $copt = $opt->{compact_opt}) {
137 local $copt->{jobs} = 0 if $ibx_opt->{'sequential-shard'};
138 PublicInbox::Xapcmd::run($ibx, 'compact', $copt);
140 last if $ibx_opt->{quit};
141 next if $ibx->{-unconfigured} || !$nidx;
142 for my $eidx (@eidx) {
143 $eidx->attach_inbox($ibx);
146 my $pr = $opt->{-progress};
147 for my $eidx (@eidx) {
148 $pr->("indexing $eidx->{topdir} ...\n") if $pr;
149 $eidx->eidx_sync($opt);
150 last if $opt->{quit};