]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
watch: use detect_indexlevel for unconfigured inboxes
authorEric Wong <e@80x24.org>
Thu, 6 Apr 2023 12:39:52 +0000 (12:39 +0000)
committerEric Wong <e@80x24.org>
Thu, 6 Apr 2023 21:20:50 +0000 (21:20 +0000)
I favor leaving the publicinbox.<name>.indexlevel parameter
out of config files to make it easier to alter and reduce
sources of truth.  It worked well in most cases, but
public-inbox-watch also needs to detect the indexlevel.

Moving the sub to InboxWritable (from Admin) probably makes
sense since it's a per-inbox attribute and allows -watch
to reuse it.

lib/PublicInbox/Admin.pm
lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/Watch.pm
script/public-inbox-convert
script/public-inbox-index
t/index-git-times.t
t/indexlevels-mirror.t
t/init.t

index abfcbb9cf531e5cc63967b07a0c75b94f10af913..da34a3bd1dd507bb2824951336106fe040476b14 100644 (file)
@@ -87,33 +87,6 @@ sub resolve_git_dir {
        $dir;
 }
 
-# for unconfigured inboxes
-sub detect_indexlevel ($) {
-       my ($ibx) = @_;
-
-       my $over = $ibx->over;
-       my $srch = $ibx->search;
-       delete @$ibx{qw(over search)}; # don't leave open FDs lying around
-
-       # brand new or never before indexed inboxes default to full
-       return 'full' unless $over;
-       my $l = 'basic';
-       return $l unless $srch;
-       if (my $xdb = $srch->xdb) {
-               $l = 'full';
-               my $m = $xdb->get_metadata('indexlevel');
-               if ($m eq 'medium') {
-                       $l = $m;
-               } elsif ($m ne '') {
-                       warn <<"";
-$ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
-
-               }
-               $ibx->{-skip_docdata} = 1 if $xdb->get_metadata('skip_docdata');
-       }
-       $l;
-}
-
 sub unconfigured_ibx ($$) {
        my ($dir, $i) = @_;
        my $name = "unconfigured-$i";
index 17dfbe18500a6dfa00e87f50209baec4126f61c3..022e2a690984450ebfd83904cc23028a3fc7285b 100644 (file)
@@ -245,4 +245,31 @@ sub git_dir_latest {
                "$self->{inboxdir}/git/$$max.git" : undef;
 }
 
+# for unconfigured inboxes
+sub detect_indexlevel ($) {
+       my ($ibx) = @_;
+
+       my $over = $ibx->over;
+       my $srch = $ibx->search;
+       delete @$ibx{qw(over search)}; # don't leave open FDs lying around
+
+       # brand new or never before indexed inboxes default to full
+       return 'full' unless $over;
+       my $l = 'basic';
+       return $l unless $srch;
+       if (my $xdb = $srch->xdb) {
+               $l = 'full';
+               my $m = $xdb->get_metadata('indexlevel');
+               if ($m eq 'medium') {
+                       $l = $m;
+               } elsif ($m ne '') {
+                       warn <<"";
+$ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
+
+               }
+               $ibx->{-skip_docdata} = 1 if $xdb->get_metadata('skip_docdata');
+       }
+       $l;
+}
+
 1;
index b907772e3b6f685183c9d623f2a18ea90bc727ff..f36c8f97877d3e47d5b238208d806ffc9cf48666 100644 (file)
@@ -1123,7 +1123,7 @@ sub begin_txn_lazy {
 }
 
 # store 'indexlevel=medium' in v2 shard=0 and v1 (only one shard)
-# This metadata is read by Admin::detect_indexlevel:
+# This metadata is read by InboxWritable->detect_indexlevel:
 sub set_metadata_once {
        my ($self) = @_;
 
index 693c71818b5bf8e4580b16fe7227d28bbe9005e5..b87abafed22bc798b29fee782d2a3e4ebc91f60d 100644 (file)
@@ -79,6 +79,8 @@ sub new {
                my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
 
                my $watches = $ibx->{watch} or return;
+
+               $ibx->{indexlevel} //= $ibx->detect_indexlevel;
                $watches = PublicInbox::Config::_array($watches);
                for my $watch (@$watches) {
                        my $uri;
index 5f4f2020c186bcd4b3aa1342ae28a6f97df1e679..750adca475310539ad51dc430db6d30ef7d69b3e 100755 (executable)
@@ -63,7 +63,7 @@ if (delete $old->{-unconfigured}) {
 }
 die "Only conversion from v1 inboxes is supported\n" if $old->version >= 2;
 
-my $detected = PublicInbox::Admin::detect_indexlevel($old);
+my $detected = $old->detect_indexlevel;
 $old->{indexlevel} //= $detected;
 my $env;
 if ($opt->{'index'}) {
index a04be9fcb4f18456777f3eb0e0295c9cd283ce96..f29e7c3c09b3e6b2c036622d5dbfb9fab4e75e64 100755 (executable)
@@ -66,6 +66,7 @@ $opt->{-use_cwd} = 1;
 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
 PublicInbox::Admin::require_or_die('-index');
 unless (@ibxs) { print STDERR $help; exit 1 }
+require PublicInbox::InboxWritable;
 
 my (@eidx, %eidx_seen);
 my $update_extindex = $opt->{'update-extindex'};
@@ -96,8 +97,9 @@ for my $ei_name (@$update_extindex) {
 my $mods = {};
 my @eidx_unconfigured;
 foreach my $ibx (@ibxs) {
+       $ibx = PublicInbox::InboxWritable->new($ibx);
        # detect_indexlevel may also set $ibx->{-skip_docdata}
-       my $detected = PublicInbox::Admin::detect_indexlevel($ibx);
+       my $detected = $ibx->detect_indexlevel;
        # XXX: users can shoot themselves in the foot, with opt->{indexlevel}
        $ibx->{indexlevel} //= $opt->{indexlevel} // ($opt->{xapian_only} ?
                        'full' : $detected);
@@ -117,11 +119,9 @@ $opt->{compact} = 0 if !$mods->{'Search::Xapian'};
 PublicInbox::Admin::require_or_die(keys %$mods);
 my $env = PublicInbox::Admin::index_prepare($opt, $cfg);
 local %ENV = (%ENV, %$env) if $env;
-require PublicInbox::InboxWritable;
 PublicInbox::Xapcmd::check_compact() if $opt->{compact};
 PublicInbox::Admin::progress_prepare($opt);
 for my $ibx (@ibxs) {
-       $ibx = PublicInbox::InboxWritable->new($ibx);
        if ($opt->{compact} >= 2) {
                PublicInbox::Xapcmd::run($ibx, 'compact', $opt->{compact_opt});
        }
index ffe9223cf94db88091387309beba63fd98551a0f..96886c5e75aa3e216b2e4e0e933957e11d236830 100644 (file)
@@ -8,6 +8,7 @@ use PublicInbox::Config;
 use PublicInbox::Admin;
 use PublicInbox::Import;
 use File::Path qw(remove_tree);
+require PublicInbox::InboxWritable;
 
 require_mods(qw(DBD::SQLite Search::Xapian));
 use_ok 'PublicInbox::Over';
@@ -57,7 +58,7 @@ my $smsg;
 {
        my $cfg = PublicInbox::Config->new;
        my $ibx = $cfg->lookup($addr);
-       my $lvl = PublicInbox::Admin::detect_indexlevel($ibx);
+       my $lvl = PublicInbox::InboxWritable::detect_indexlevel($ibx);
        is($lvl, 'medium', 'indexlevel detected');
        is($ibx->{-skip_docdata}, 1, '--skip-docdata flag set on -index');
        $smsg = $ibx->over->get_art(1);
@@ -80,7 +81,7 @@ SKIP: {
        my $check_v2 = sub {
                my $ibx = PublicInbox::Inbox->new({inboxdir => $v2dir,
                                address => $addr});
-               my $lvl = PublicInbox::Admin::detect_indexlevel($ibx);
+               my $lvl = PublicInbox::InboxWritable::detect_indexlevel($ibx);
                is($lvl, 'medium', 'indexlevel detected after convert');
                is($ibx->{-skip_docdata}, 1,
                        '--skip-docdata preserved after convert');
index 463b35becc49ac3ac79be30cc625a049f744b1eb..62411671cae17157526252db83d23ee480b01337 100644 (file)
@@ -5,7 +5,7 @@ use strict;
 use v5.10.1;
 use PublicInbox::TestCommon;
 use PublicInbox::Eml;
-use PublicInbox::Inbox;
+use PublicInbox::InboxWritable;
 require PublicInbox::Admin;
 my $PI_TEST_VERSION = $ENV{PI_TEST_VERSION} || 2;
 require_git('2.6') if $PI_TEST_VERSION == 2;
@@ -110,7 +110,8 @@ my $import_index_incremental = sub {
 
        if ($level ne 'basic') {
                ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
-               is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
+               is(PublicInbox::InboxWritable::detect_indexlevel($ro_mirror),
+                       $level,
                   'indexlevel detectable by Admin after xcpdb v' .$v.$level);
                delete $ro_mirror->{$_} for (qw(over search));
                my $mset = $ro_mirror->search->mset('m:m@2');
@@ -152,7 +153,7 @@ my $import_index_incremental = sub {
        is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
        is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
 
-       is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
+       is(PublicInbox::InboxWritable::detect_indexlevel($ro_mirror), $level,
           'indexlevel detectable by Admin '.$v.$level);
 
        SKIP: {
index 46258e45df4c733d7f52c164331ac3158d64c623..0096ca30796249f9ea7d006d98a623a679202f03 100644 (file)
--- a/t/init.t
+++ b/t/init.t
@@ -6,6 +6,7 @@ use v5.10.1;
 use PublicInbox::Config;
 use PublicInbox::TestCommon;
 use PublicInbox::Admin;
+use PublicInbox::InboxWritable;
 my ($tmpdir, $for_destroy) = tmpdir();
 sub quiet_fail {
        my ($cmd, $msg) = @_;
@@ -147,7 +148,7 @@ SKIP: {
                ok(run_script($cmd), "-init -L $lvl");
                is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
                my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
-               is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
+               is(PublicInbox::InboxWritable::detect_indexlevel($ibx), $lvl,
                        'detected expected level w/o config');
                ok(!$ibx->{-skip_docdata}, 'docdata written by default');
        }
@@ -159,7 +160,7 @@ SKIP: {
                        "$name\@example.com" ];
                ok(run_script($cmd), "-init -V$v --skip-docdata");
                my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
-               is(PublicInbox::Admin::detect_indexlevel($ibx), 'full',
+               is(PublicInbox::InboxWritable::detect_indexlevel($ibx), 'full',
                        "detected default indexlevel -V$v");
                ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
                ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');