File Coverage

blib/lib/CPAN/MirrorMerger/Algorithm/PreferLatestVersion.pm
Criterion Covered Total %
statement 48 53 90.5
branch 3 6 50.0
condition 3 5 60.0
subroutine 10 11 90.9
pod 0 2 0.0
total 64 77 83.1


line stmt bran cond sub pod time code
1             package CPAN::MirrorMerger::Algorithm::PreferLatestVersion;
2 2     2   13 use strict;
  2         4  
  2         49  
3 2     2   9 use warnings;
  2         5  
  2         97  
4              
5 2     2   12 use Class::Accessor::Lite ro => [qw/mirror_cache logger/];
  2         4  
  2         10  
6              
7 2     2   1085 use List::UtilsBy qw/rev_nsort_by/;
  2         3232  
  2         108  
8 2     2   12 use Time::Moment;
  2         4  
  2         42  
9 2     2   711 use CPAN::MirrorMerger::Index::Merged;
  2         7  
  2         51  
10 2     2   11 use CPAN::MirrorMerger::Logger::Null;
  2         4  
  2         412  
11              
12             sub new {
13 1     1 0 4 my ($class, %args) = @_;
14 1   33     4 $args{logger} ||= CPAN::MirrorMerger::Logger::Null->instance();
15 1         8 return bless \%args => $class;
16             }
17              
18             sub merge {
19 1     1 0 11 my ($self, @mirrors) = @_;
20 1         24 my $now = Time::Moment->now_utc();
21              
22 1         4 my %multiplex_index = ();
23 1         2 for my $mirror (@mirrors) {
24 3         10 my $index = $self->mirror_cache->get_or_fetch_index($mirror);
25 3         5 for my $package_info (@{ $index->packages }) {
  3         10  
26 3         21 $self->logger->debug("add package @{[ $package_info->path ]} from @{[ $mirror->name ]}");
  3         19  
  3         24  
27              
28 3   100     24 my $candidates = ($multiplex_index{$package_info->module} ||= []);
29 3 100       28 if (@$candidates == 0) {
30 2         5 push @$candidates => $package_info;
31 2         6 next;
32             }
33              
34             # optimize for performance
35 1 50       5 if ($candidates->[0]->compareble_version <= $package_info->compareble_version) {
    0          
36 1         3 unshift @$candidates => $package_info;
37 1         3 next;
38             } elsif ($candidates->[-1]->compareble_version >= $package_info->compareble_version) {
39 0         0 push @$candidates => $package_info;
40 0         0 next;
41             }
42              
43 2     2   937 use sort 'stable';
  2         985  
  2         11  
44 0         0 push @$candidates => $package_info;
45 0     0   0 @$candidates = rev_nsort_by { $_->compareble_version } @$candidates;
  0         0  
46             }
47             }
48              
49 1         5 my %headers = (
50             File => '02packages.details.txt',
51             URL => '/modules/02packages.details.txt',
52             Description => 'Merged CPAN Mirrors ('.(join ', ', sort map $_->name, @mirrors).')',
53             Columns => 'package name, version, path',
54             'Intended-For' => 'Automated fetch routines, namespace documentation.',
55             'Written-By' => 'CPAN::MirrorMerger',
56             'Line-Count' => scalar keys %multiplex_index,
57             'Last-Updated' => $now->strftime('%a, %d %b %Y %H:%M:%S %Z'),
58             );
59 1         42 my @packages = map { $multiplex_index{$_}[0] } sort keys %multiplex_index;
  2         5  
60              
61 1         16 return CPAN::MirrorMerger::Index::Merged->new(
62             headers => \%headers,
63             packages => \@packages,
64             mirrors => \@mirrors,
65             mirror_cache => $self->mirror_cache,
66             multiplex_index => \%multiplex_index,
67             logger => $self->logger,
68             );
69             }
70              
71              
72             1;
73             __END__