From: Eric Wong Date: Tue, 15 Oct 2019 01:11:58 +0000 (+0000) Subject: config: simplify lookup* methods X-Git-Tag: v1.2.0~39^2~4 X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=commitdiff_plain;h=b41c19abcf0b0ac8a5f55678bfb0058ad50b3179;hp=1317fb7b4ace03f6d9dfb1a42ee5f9371a1bf913 config: simplify lookup* methods This ensures we always process inboxes in section order and reduces the amount of code we have to maintain for each lookup. Avoiding the cost of inboxes object creation is not worth the code overhead; and we can implement a config cache via Storable easily for large configs and -mda users. --- diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index b7e03af3..2b99346a 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -63,58 +63,24 @@ sub new { $self; } +sub _fill_all ($) { each_inbox($_[0], sub {}) } + +sub _lookup_fill ($$$) { + my ($self, $cache, $key) = @_; + $self->{$cache}->{$key} // do { + _fill_all($self); + $self->{$cache}->{$key}; + } +} + sub lookup { my ($self, $recipient) = @_; - my $addr = lc($recipient); - my $ibx = $self->{-by_addr}->{$addr}; - return $ibx if $ibx; - - my $pfx; - - foreach my $k (keys %$self) { - $k =~ m!\A(publicinbox\.[^/]+)\.address\z! or next; - my $v = $self->{$k}; - if (ref($v) eq "ARRAY") { - foreach my $alias (@$v) { - (lc($alias) eq $addr) or next; - $pfx = $1; - last; - } - } else { - (lc($v) eq $addr) or next; - $pfx = $1; - last; - } - } - defined $pfx or return; - _fill($self, $pfx); + _lookup_fill($self, '-by_addr', lc($recipient)); } sub lookup_list_id { my ($self, $list_id) = @_; - $list_id = lc($list_id); - my $ibx = $self->{-by_list_id}->{$list_id}; - return $ibx if $ibx; - - my $pfx; - - foreach my $k (keys %$self) { - $k =~ /\A(publicinbox\.[\w-]+)\.listid\z/ or next; - my $v = $self->{$k}; - if (ref($v) eq "ARRAY") { - foreach my $alias (@$v) { - (lc($alias) eq $list_id) or next; - $pfx = $1; - last; - } - } else { - (lc($v) eq $list_id) or next; - $pfx = $1; - last; - } - } - defined $pfx or return; - _fill($self, $pfx); + _lookup_fill($self, '-by_list_id', lc($list_id)); } sub lookup_name ($$) { @@ -135,20 +101,7 @@ sub each_inbox { sub lookup_newsgroup { my ($self, $ng) = @_; - $ng = lc($ng); - my $ibx = $self->{-by_newsgroup}->{$ng}; - return $ibx if $ibx; - - foreach my $k (keys %$self) { - $k =~ m!\A(publicinbox\.[^/]+)\.newsgroup\z! or next; - my $v = $self->{$k}; - my $pfx = $1; - if ($v eq $ng) { - $ibx = _fill($self, $pfx); - return $ibx; - } - } - undef; + _lookup_fill($self, '-by_newsgroup', lc($ng)); } sub limiter { @@ -461,7 +414,7 @@ sub _fill { if ($ibx->{obfuscate}) { $ibx->{-no_obfuscate} = $self->{-no_obfuscate}; $ibx->{-no_obfuscate_re} = $self->{-no_obfuscate_re}; - each_inbox($self, sub {}); # noop to populate -no_obfuscate + _fill_all($self); # noop to populate -no_obfuscate } if (my $ibx_code_repos = $ibx->{coderepo}) {