]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-index
add xt/mem-nntpd-tls maintainer test
[public-inbox.git] / script / public-inbox-index
1 #!perl -w
2 # Copyright (C) 2015-2021 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 # Usage with libeatmydata <https://www.flamingspork.com/projects/libeatmydata/>
6 # highly recommended: eatmydata public-inbox-index INBOX_DIR
7
8 use strict;
9 use v5.10.1;
10 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
11 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
12 usage: public-inbox-index [options] INBOX_DIR
13
14   Create and update per-inbox search indices
15
16 options:
17
18   --no-fsync          speed up indexing, risk corruption on power outage
19   -L LEVEL            `basic', `medium', or `full' (default: full)
20   -E EXTINDEX         update extindex (default: `all')
21   --all               index all configured inboxes
22   --compact | -c      run public-inbox-compact(1) after indexing
23   --sequential-shard  index Xapian shards sequentially for slow storage
24   --jobs=NUM          set or disable parallelization (NUM=0)
25   --batch-size=BYTES  flush changes to OS after a given number of bytes
26   --max-size=BYTES    do not index messages larger than the given size
27   --reindex           index previously indexed data (if upgrading)
28   --since=DATE        limit --reindex to changes after DATE
29   --until=DATE        limit --reindex to changes before DATE
30   --rethread          regenerate thread IDs (if upgrading, use sparingly)
31   --prune             prune git storage on discontiguous history
32   --verbose | -v      increase verbosity (may be repeated)
33
34 BYTES may use `k', `m', and `g' suffixes (e.g. `10m' for 10 megabytes)
35 See public-inbox-index(1) man page for full documentation.
36 EOF
37 my $opt = {
38         quiet => -1, compact => 0, max_size => undef, fsync => 1,
39         'update-extindex' => [], # ":s@" optional arg sets '' if no arg given
40 };
41 GetOptions($opt, qw(verbose|v+ reindex rethread compact|c+ jobs|j=i prune
42                 fsync|sync! xapian_only|xapian-only dangerous
43                 indexlevel|index-level|L=s max_size|max-size=s
44                 batch_size|batch-size=s
45                 since|after=s until|before=s
46                 sequential-shard|seq-shard
47                 no-update-extindex update-extindex|E=s@
48                 fast-noop|F skip-docdata all C=s@ help|h))
49         or die $help;
50 if ($opt->{help}) { print $help; exit 0 };
51 die "--jobs must be >= 0\n" if defined $opt->{jobs} && $opt->{jobs} < 0;
52 if ($opt->{xapian_only} && !$opt->{reindex}) {
53         die "--xapian-only requires --reindex\n";
54 }
55 if ($opt->{reindex} && delete($opt->{'fast-noop'})) {
56         warn "--fast-noop ignored with --reindex\n";
57 }
58
59 # require lazily to speed up --help
60 require PublicInbox::Admin;
61 PublicInbox::Admin::require_or_die('-index');
62 PublicInbox::Admin::do_chdir(delete $opt->{C});
63
64 my $cfg = PublicInbox::Config->new; # Config is loaded by Admin
65 $opt->{-use_cwd} = 1;
66 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
67 PublicInbox::Admin::require_or_die('-index');
68 unless (@ibxs) { print STDERR $help; exit 1 }
69
70 my (@eidx, %eidx_seen);
71 my $update_extindex = $opt->{'update-extindex'};
72 if (!scalar(@$update_extindex) && (my $ALL = $cfg->ALL)) {
73         # extindex and normal inboxes may have different owners
74         push(@$update_extindex, 'all') if -w $ALL->{topdir};
75 }
76 @$update_extindex = () if $opt->{'no-update-extindex'};
77 if (scalar @$update_extindex) {
78         PublicInbox::Admin::require_or_die('-search');
79         require PublicInbox::ExtSearchIdx;
80 }
81 for my $ei_name (@$update_extindex) {
82         my $es = $cfg->lookup_ei($ei_name);
83         my $topdir;
84         if (!$es && -d $ei_name) { # allow dirname or config section name
85                 $topdir = $ei_name;
86         } elsif ($es) {
87                 $topdir = $es->{topdir};
88         } else {
89                 die "extindex `$ei_name' not configured or found\n";
90         }
91         my $o = { %$opt };
92         delete $o->{indexlevel} if ($o->{indexlevel}//'') eq 'basic';
93         $eidx_seen{$topdir} //=
94                 push(@eidx, PublicInbox::ExtSearchIdx->new($topdir, $o));
95 }
96 my $mods = {};
97 my @eidx_unconfigured;
98 foreach my $ibx (@ibxs) {
99         # detect_indexlevel may also set $ibx->{-skip_docdata}
100         my $detected = PublicInbox::Admin::detect_indexlevel($ibx);
101         # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
102         $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapian_only} ?
103                         'full' : $detected);
104         PublicInbox::Admin::scan_ibx_modules($mods, $ibx);
105         if (@eidx && $ibx->{-unconfigured}) {
106                 push @eidx_unconfigured, "  $ibx->{inboxdir}\n";
107         }
108 }
109 warn <<EOF if @eidx_unconfigured;
110 The following inboxes are unconfigured and will not be updated in
111 @$update_extindex:\n@eidx_unconfigured
112 EOF
113
114 # "Search::Xapian" includes SWIG "Xapian", too:
115 $opt->{compact} = 0 if !$mods->{'Search::Xapian'};
116
117 PublicInbox::Admin::require_or_die(keys %$mods);
118 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
119 local %ENV = (%ENV, %$env) if $env;
120 require PublicInbox::InboxWritable;
121 PublicInbox::Xapcmd::check_compact() if $opt->{compact};
122 PublicInbox::Admin::progress_prepare($opt);
123 for my $ibx (@ibxs) {
124         $ibx = PublicInbox::InboxWritable->new($ibx);
125         if ($opt->{compact} >= 2) {
126                 PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
127         }
128         $ibx->{-no_fsync} = 1 if !$opt->{fsync};
129         $ibx->{-dangerous} = 1 if $opt->{dangerous};
130         $ibx->{-skip_docdata} //= $opt->{'skip-docdata'};
131
132         my $ibx_opt = $opt;
133         if (defined(my $s = $ibx->{lc('indexSequentialShard')})) {
134                 defined(my $v = $cfg->git_bool($s)) or die <<EOL;
135 publicInbox.$ibx->{name}.indexSequentialShard not boolean
136 EOL
137                 $ibx_opt = { %$opt, 'sequential-shard' => $v };
138         }
139         my $nidx = PublicInbox::Admin::index_inbox($ibx, undef, $ibx_opt);
140         last if $ibx_opt->{quit};
141         if (my $copt = $opt->{compact_opt}) {
142                 local $copt->{jobs} = 0 if $ibx_opt->{'sequential-shard'};
143                 PublicInbox::Xapcmd::run($ibx, 'compact', $copt);
144         }
145         last if $ibx_opt->{quit};
146         next if $ibx->{-unconfigured} || !$nidx;
147         for my $eidx (@eidx) {
148                 $eidx->attach_inbox($ibx);
149         }
150 }
151 my $pr = $opt->{-progress};
152 for my $eidx (@eidx) {
153         $pr->("indexing $eidx->{topdir} ...\n") if $pr;
154         $eidx->eidx_sync($opt);
155         last if $opt->{quit};
156 }