2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
7 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
8 usage: public-inbox-extindex [options] [EXTINDEX_DIR] [INBOX_DIR...]
10 Create and update external (detached) search indices
12 --no-fsync speed up indexing, risk corruption on power outage
13 --watch run persistently and watch for inbox updates
14 -L LEVEL `medium', or `full' (default: full)
15 --all index all configured inboxes
16 --jobs=NUM set or disable parallelization (NUM=0)
17 --batch-size=BYTES flush changes to OS after a given number of bytes
18 --max-size=BYTES do not index messages larger than the given size
19 --gc perform garbage collection instead of indexing
20 --dedupe[=MSGID] fix prior deduplication errors (may be repeated)
21 --reindex index previously indexed inboxes
22 --fast only reindex unseen/stale messages
23 --verbose | -v increase verbosity (may be repeated)
24 --dry-run | -n dry-run on --dedupe
26 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
27 See public-inbox-extindex(1) man page for full documentation.
29 my $opt = { quiet => -1, compact => 0, fsync => 1, scan => 1 };
30 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i
32 indexlevel|index-level|L=s max_size|max-size=s
33 batch_size|batch-size=s
34 dedupe:s@ gc commit-interval=i watch scan! dry-run|n
37 if ($opt->{help}) { print $help; exit 0 };
38 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
42 local $SIG{USR1} = 'IGNORE'; # to be overridden in eidx_sync
43 # require lazily to speed up --help
44 require PublicInbox::Admin;
45 PublicInbox::Admin::do_chdir(delete $opt->{C});
46 my $cfg = PublicInbox::Config->new;
47 my $eidx_dir = shift(@ARGV);
48 unless (defined $eidx_dir) {
49 if ($opt->{all} && $cfg->ALL) {
50 $eidx_dir = $cfg->ALL->{topdir};
57 die "E: inbox paths must not be specified with --gc\n" if @ARGV;
58 for my $sw (qw(all watch dry-run dedupe)) {
59 die "E: --$sw is not compatible with --gc\n" if $opt->{$sw};
62 @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
64 $opt->{'dry-run'} && !$opt->{dedupe} and
65 die "E: --dry-run only affects --dedupe\n";
66 $opt->{fast} && !$opt->{reindex} and
67 die "E: --fast only affects --reindex\n";
69 PublicInbox::Admin::require_or_die(qw(-search));
70 PublicInbox::Config::json() or die "Cpanel::JSON::XS or similar missing\n";
71 PublicInbox::Admin::progress_prepare($opt);
72 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
73 local %ENV = (%ENV, %$env) if $env;
74 require PublicInbox::ExtSearchIdx;
75 my $eidx = PublicInbox::ExtSearchIdx->new($eidx_dir, $opt);
77 $eidx->attach_config($cfg);
81 $eidx->attach_config($cfg);
83 $eidx->attach_config($cfg, \@ibxs);
86 $cfg = undef; # save memory only after SIGHUP
87 $eidx->eidx_watch($opt);
89 $eidx->eidx_sync($opt);