From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS43350 77.247.176.0/21 X-Spam-Status: No, score=-1.5 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,SPF_FAIL,SPF_HELO_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (politkovskaja.torservers.net [77.247.181.165]) by dcvr.yhbt.net (Postfix) with ESMTP id 04EAB20FEA for ; Sat, 2 Jul 2016 21:50:36 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/3] config: introduce each_inbox for iteration Date: Sat, 2 Jul 2016 21:50:28 +0000 Message-Id: <20160702215030.7692-2-e@80x24.org> In-Reply-To: <20160702215030.7692-1-e@80x24.org> References: <20160702215030.7692-1-e@80x24.org> List-Id: This fills in the internal lookup hashes and simplifies callers. --- lib/PublicInbox/Config.pm | 18 ++++++++++++++---- lib/PublicInbox/ExtMsg.pm | 32 +++++++++++++++++--------------- lib/PublicInbox/NNTPD.pm | 21 +++++++++------------ 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index ddb4f6b..d34d11a 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -50,11 +50,21 @@ sub lookup { _fill($self, $pfx); } -sub lookup_name { +sub lookup_name ($$) { my ($self, $name) = @_; - my $rv = $self->{-by_name}->{$name}; - return $rv if $rv; - $rv = _fill($self, "publicinbox.$name") or return; + $self->{-by_name}->{$name} || _fill($self, "publicinbox.$name"); +} + +sub each_inbox { + my ($self, $cb) = @_; + my %seen; + foreach my $k (keys %$self) { + $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.mainrepo\z/ or next; + next if $seen{$1}; + $seen{$1} = 1; + my $ibx = lookup_name($self, $1) or next; + $cb->($ibx); + } } sub lookup_newsgroup { diff --git a/lib/PublicInbox/ExtMsg.pm b/lib/PublicInbox/ExtMsg.pm index 4b9e025..73bd4b1 100644 --- a/lib/PublicInbox/ExtMsg.pm +++ b/lib/PublicInbox/ExtMsg.pm @@ -30,19 +30,16 @@ sub ext_msg { eval { require PublicInbox::Search }; my $have_xap = $@ ? 0 : 1; - my (@nox, @ibx); + my (@nox, @ibx, @found); - foreach my $k (keys %$pi_config) { - $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next; - my $name = $1; - next if $name eq $cur->{name}; - my $other = $pi_config->lookup_name($name) or next; - next unless $other->base_url; + $pi_config->each_inbox(sub { + my ($other) = @_; + return if $other->{name} eq $cur->{name} || !$other->base_url; my $s = $other->search; if (!$s) { push @nox, $other; - next; + return; } # try to find the URL with Xapian to avoid forking @@ -50,17 +47,22 @@ sub ext_msg { if ($@) { # xapian not configured properly for this repo push @nox, $other; - next; + return; } # maybe we found it! - return r302($other, $mid) if defined $doc_id; + if (defined $doc_id) { + push @found, $other; + } else { + # no point in trying the fork fallback if we + # know Xapian is up-to-date but missing the + # message in the current repo + push @ibx, $other; + } + }); - # no point in trying the fork fallback if we - # know Xapian is up-to-date but missing the - # message in the current repo - push @ibx, $other; - } + # TODO: multiple hits + return r302($found[0], $mid) if @found; # Xapian not installed or configured for some repos, # do a full MID check: diff --git a/lib/PublicInbox/NNTPD.pm b/lib/PublicInbox/NNTPD.pm index 50d022b..a67811b 100644 --- a/lib/PublicInbox/NNTPD.pm +++ b/lib/PublicInbox/NNTPD.pm @@ -23,21 +23,18 @@ sub refresh_groups () { my $pi_config = PublicInbox::Config->new; my $new = {}; my @list; - foreach my $k (keys %$pi_config) { - $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next; - my $name = $1; - my $git_dir = $pi_config->{$k}; - my $ngname = $pi_config->{"publicinbox.$name.newsgroup"}; - next unless defined $ngname; - next if ($ngname eq ''); # disabled - my $ng = $pi_config->lookup_newsgroup($ngname) or next; - - # Only valid if msgmap and search works - if ($ng->nntp_usable) { + $pi_config->each_inbox(sub { + my ($ng) = @_; + my $ngname = $ng->{newsgroup} or next; + if (ref $ngname) { + warn 'multiple newsgroups not supported: '. + join(', ', @$ngname). "\n"; + } elsif ($ng->nntp_usable) { + # Only valid if msgmap and search works $new->{$ngname} = $ng; push @list, $ng; } - } + }); @list = sort { $a->{newsgroup} cmp $b->{newsgroup} } @list; $self->{grouplist} = \@list; # this will destroy old groups that got deleted