]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-extindex
5f27988ffda80e4d6f3f97a34e4946947100fed9
[public-inbox.git] / script / public-inbox-extindex
1 #!perl -w
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.
5 use strict;
6 use v5.10.1;
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...]
10
11   Create and update external (detached) search indices
12
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)
22
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.
25 EOF
26 my $opt = { quiet => -1, compact => 0, fsync => 1, scan => 1 };
27 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i
28                 fsync|sync!
29                 indexlevel|index-level|L=s max_size|max-size=s
30                 batch_size|batch-size=s
31                 gc commit-interval=i watch scan!
32                 all help|h))
33         or die $help;
34 if ($opt->{help}) { print $help; exit 0 };
35 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
36 require IO::Handle;
37 STDOUT->autoflush(1);
38 STDERR->autoflush(1);
39 local $SIG{USR1} = 'IGNORE'; # to be overridden in eidx_sync
40 # require lazily to speed up --help
41 require PublicInbox::Admin;
42 my $cfg = PublicInbox::Config->new;
43 my $eidx_dir = shift(@ARGV);
44 unless (defined $eidx_dir) {
45         if ($opt->{all} && $cfg->ALL) {
46                 $eidx_dir = $cfg->ALL->{topdir};
47         } else {
48                 die "E: $help";
49         }
50 }
51 my @ibxs;
52 if ($opt->{gc}) {
53         die "E: inbox paths must not be specified with --gc\n" if @ARGV;
54         die "E: --all not compatible with --gc\n" if $opt->{all};
55         die "E: --watch is not compatible with --gc\n" if $opt->{watch};
56 } else {
57         @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
58 }
59 PublicInbox::Admin::require_or_die(qw(-search));
60 PublicInbox::Config::json() or die "Cpanel::JSON::XS or similar missing\n";
61 PublicInbox::Admin::progress_prepare($opt);
62 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
63 local %ENV = (%ENV, %$env) if $env;
64 require PublicInbox::ExtSearchIdx;
65 my $eidx = PublicInbox::ExtSearchIdx->new($eidx_dir, $opt);
66 if ($opt->{gc}) {
67         $eidx->attach_config($cfg);
68         $eidx->eidx_gc($opt);
69 } else {
70         if ($opt->{all}) {
71                 $eidx->attach_config($cfg);
72         } else {
73                 $eidx->attach_inbox($_) for @ibxs;
74         }
75         if ($opt->{watch}) {
76                 $cfg = undef; # save memory only after SIGHUP
77                 $eidx->eidx_watch($opt);
78         } else {
79                 $eidx->eidx_sync($opt);
80         }
81 }