2 # Copyright (C) 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.
7 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
8 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
9 usage: public-inbox-extindex [options] EXTINDEX_DIR [INBOX_DIR]
11 Create and update external (detached) search indices
13 --no-fsync speed up indexing, risk corruption on power outage
14 --watch run persistently and watch for inbox updates
15 -L LEVEL `medium', or `full' (default: full)
16 --all index all configured inboxes
17 --jobs=NUM set or disable parallelization (NUM=0)
18 --batch-size=BYTES flush changes to OS after a given number of bytes
19 --max-size=BYTES do not index messages larger than the given size
20 --gc perform garbage collection instead of indexing
21 --verbose | -v increase verbosity (may be repeated)
23 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
24 See public-inbox-extindex(1) man page for full documentation.
26 my $opt = { quiet => -1, compact => 0, fsync => 1, scan => 1 };
27 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i
29 indexlevel|index-level|L=s max_size|max-size=s
30 batch_size|batch-size=s
31 gc commit-interval=i watch scan!
34 if ($opt->{help}) { print $help; exit 0 };
35 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
39 # require lazily to speed up --help
40 my $eidx_dir = shift(@ARGV) // die "E: $help";
41 local $SIG{USR1} = 'IGNORE'; # to be overridden in eidx_sync
42 require PublicInbox::Admin;
43 my $cfg = PublicInbox::Config->new;
46 die "E: inbox paths must not be specified with --gc\n" if @ARGV;
47 die "E: --all not compatible with --gc\n" if $opt->{all};
48 die "E: --watch is not compatible with --gc\n" if $opt->{watch};
50 @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
52 PublicInbox::Admin::require_or_die(qw(-search));
53 PublicInbox::Config::json() or die "Cpanel::JSON::XS or similar missing\n";
54 PublicInbox::Admin::progress_prepare($opt);
55 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
56 local %ENV = (%ENV, %$env) if $env;
57 require PublicInbox::ExtSearchIdx;
58 my $eidx = PublicInbox::ExtSearchIdx->new($eidx_dir, $opt);
60 $eidx->attach_config($cfg);
64 $eidx->attach_config($cfg);
66 $eidx->attach_inbox($_) for @ibxs;
69 $cfg = undef; # save memory only after SIGHUP
70 $eidx->eidx_watch($opt);
72 $eidx->eidx_sync($opt);