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 --verbose | -v increase verbosity (may be repeated)
22 --dry-run | -n dry-run on --dedupe
24 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
25 See public-inbox-extindex(1) man page for full documentation.
27 my $opt = { quiet => -1, compact => 0, fsync => 1, scan => 1 };
28 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i
30 indexlevel|index-level|L=s max_size|max-size=s
31 batch_size|batch-size=s
32 dedupe:s@ gc commit-interval=i watch scan! dry-run|n
35 if ($opt->{help}) { print $help; exit 0 };
36 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
40 local $SIG{USR1} = 'IGNORE'; # to be overridden in eidx_sync
41 # require lazily to speed up --help
42 require PublicInbox::Admin;
43 my $cfg = PublicInbox::Config->new;
44 my $eidx_dir = shift(@ARGV);
45 unless (defined $eidx_dir) {
46 if ($opt->{all} && $cfg->ALL) {
47 $eidx_dir = $cfg->ALL->{topdir};
54 die "E: inbox paths must not be specified with --gc\n" if @ARGV;
55 for my $sw (qw(all watch dry-run dedupe)) {
56 die "E: --$sw is not compatible with --gc\n" if $opt->{$sw};
59 @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
61 if ($opt->{'dry-run'} && !$opt->{dedupe}) {
62 die "E: --dry-run only affects --dedupe\n";
65 PublicInbox::Admin::require_or_die(qw(-search));
66 PublicInbox::Config::json() or die "Cpanel::JSON::XS or similar missing\n";
67 PublicInbox::Admin::progress_prepare($opt);
68 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
69 local %ENV = (%ENV, %$env) if $env;
70 require PublicInbox::ExtSearchIdx;
71 my $eidx = PublicInbox::ExtSearchIdx->new($eidx_dir, $opt);
73 $eidx->attach_config($cfg);
77 $eidx->attach_config($cfg);
79 $eidx->attach_config($cfg, \@ibxs);
82 $cfg = undef; # save memory only after SIGHUP
83 $eidx->eidx_watch($opt);
85 $eidx->eidx_sync($opt);