]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-extindex
No ext_urls
[public-inbox.git] / script / public-inbox-extindex
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
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...]
9
10   Create and update external (detached) search indices
11
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
25
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.
28 EOF
29 my $opt = { quiet => -1, compact => 0, fsync => 1, scan => 1 };
30 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i
31                 fsync|sync! fast dangerous
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
35                 all C=s@ help|h))
36         or die $help;
37 if ($opt->{help}) { print $help; exit 0 };
38 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
39 require IO::Handle;
40 STDOUT->autoflush(1);
41 STDERR->autoflush(1);
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};
51         } else {
52                 die "E: $help";
53         }
54 }
55 my @ibxs;
56 if ($opt->{gc}) {
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};
60         }
61 } else {
62         @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
63 }
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";
68
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);
76 if ($opt->{gc}) {
77         $eidx->attach_config($cfg);
78         $eidx->eidx_gc($opt);
79 } else {
80         if ($opt->{all}) {
81                 $eidx->attach_config($cfg);
82         } else {
83                 $eidx->attach_config($cfg, \@ibxs);
84         }
85         if ($opt->{watch}) {
86                 $cfg = undef; # save memory only after SIGHUP
87                 $eidx->eidx_watch($opt);
88         } else {
89                 $eidx->eidx_sync($opt);
90         }
91 }