From 48b21cb662c1e17b7219612bff6ea14b98c85221 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 14 May 2016 06:10:36 +0000 Subject: declare Inbox object for reusability From the beginning, we've avoided objects here in favor of faster startup time; but it may not be worth it since a persistent httpd/nntpd is faster and -mda isn't hit as often. --- lib/PublicInbox/Config.pm | 51 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'lib/PublicInbox/Config.pm') diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index 331d25c5..3f3707ec 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -7,6 +7,7 @@ use strict; use warnings; use base qw/Exporter/; our @EXPORT_OK = qw/try_cat/; +require PublicInbox::Inbox; use File::Path::Expand qw/expand_filename/; # returns key-value pairs of config directives in a hash @@ -14,12 +15,18 @@ use File::Path::Expand qw/expand_filename/; sub new { my ($class, $file) = @_; $file = default_file() unless defined($file); - bless git_config_dump($file), $class; + my $self = bless git_config_dump($file), $class; + $self->{-by_addr} = {}; + $self->{-by_name} = {}; + $self; } sub lookup { my ($self, $recipient) = @_; my $addr = lc($recipient); + my $inbox = $self->{-by_addr}->{$addr}; + return $inbox if $inbox; + my $pfx; foreach my $k (keys %$self) { @@ -37,20 +44,15 @@ sub lookup { last; } } - defined $pfx or return; + _fill($self, $pfx); +} - my %rv; - foreach my $k (qw(mainrepo address filter)) { - my $v = $self->{"$pfx.$k"}; - $rv{$k} = $v if defined $v; - } - my $inbox = $pfx; - $inbox =~ s/\Apublicinbox\.//; - $rv{inbox} = $inbox; - my $v = $rv{address}; - $rv{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v; - \%rv; +sub lookup_name { + my ($self, $name) = @_; + my $rv = $self->{-by_name}->{$name}; + return $rv if $rv; + $self->{-by_name}->{$name} = _fill($self, "publicinbox.$name"); } sub get { @@ -105,4 +107,27 @@ sub try_cat { $rv; } +sub _fill { + my ($self, $pfx) = @_; + my $rv = {}; + + foreach my $k (qw(mainrepo address filter url)) { + my $v = $self->{"$pfx.$k"}; + $rv->{$k} = $v if defined $v; + } + my $inbox = $pfx; + $inbox =~ s/\Apublicinbox\.//; + $rv->{name} = $inbox; + my $v = $rv->{address} ||= 'public-inbox@example.com'; + $rv->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v; + $rv = PublicInbox::Inbox->new($rv); + if (ref($v) eq 'ARRAY') { + $self->{-by_addr}->{lc($_)} = $rv foreach @$v; + } else { + $self->{-by_addr}->{lc($v)} = $rv; + } + $rv; +} + + 1; -- cgit v1.2.3-24-ge0c7