From b95053c18df61e95f45e844b1490b7e0959b4739 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Mon, 6 May 2024 03:06:44 +0200 Subject: [PATCH 1/2] move predecessor from document set to query --- lib/MetaCPAN/Document/Release/Set.pm | 11 +---------- lib/MetaCPAN/Query/Release.pm | 22 ++++++++++++++++++++++ lib/MetaCPAN/Server/Controller/Diff.pm | 5 ++--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/MetaCPAN/Document/Release/Set.pm b/lib/MetaCPAN/Document/Release/Set.pm index dda46b2eb..e7e7d1268 100644 --- a/lib/MetaCPAN/Document/Release/Set.pm +++ b/lib/MetaCPAN/Document/Release/Set.pm @@ -25,6 +25,7 @@ has query_release => ( latest_by_author latest_by_distribution modules + predecessor recent requires reverse_dependencies @@ -56,16 +57,6 @@ sub find { return $data; } -sub predecessor { - my ( $self, $name ) = @_; - return $self->filter( { - and => [ - { term => { distribution => $name } }, - { not => { filter => { term => { status => 'latest' } } } }, - ] - } )->sort( [ { date => 'desc' } ] )->first; -} - sub find_github_based { shift->filter( { and => [ diff --git a/lib/MetaCPAN/Query/Release.pm b/lib/MetaCPAN/Query/Release.pm index 0c1d0ac46..64b8252eb 100644 --- a/lib/MetaCPAN/Query/Release.pm +++ b/lib/MetaCPAN/Query/Release.pm @@ -1260,5 +1260,27 @@ sub _numify { version->new($ver)->numify; } +sub predecessor { + my ( $self, $name ) = @_; + + my $res = $self->es->search( + index => $self->index_name, + type => 'release', + body => { + query => { + bool => { + must => [ { term => { distribution => $name } }, ], + must_not => [ { term => { status => 'latest' } }, ], + }, + }, + sort => [ { date => 'desc' } ], + size => 1, + }, + ); + my ($release) = $res->{hits}{hits}[0]; + return unless $release; + return $release->{_source}; +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/MetaCPAN/Server/Controller/Diff.pm b/lib/MetaCPAN/Server/Controller/Diff.pm index 4e92c21d4..1c80d77f5 100644 --- a/lib/MetaCPAN/Server/Controller/Diff.pm +++ b/lib/MetaCPAN/Server/Controller/Diff.pm @@ -33,9 +33,8 @@ sub release : Chained('index') : PathPart('release') : Args(1) { my ( $latest, $previous ); try { - $latest = $c->model('CPAN::Release')->raw->find($name); - $previous - = $c->model('CPAN::Release')->raw->predecessor($name)->{_source}; + $latest = $c->model('CPAN::Release')->raw->find($name); + $previous = $c->model('CPAN::Release')->predecessor($name); } catch { $c->detach('/not_found'); From c0a2b283fb8c7ae64e685fc8aee57814beaee9fb Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Mon, 6 May 2024 03:07:41 +0200 Subject: [PATCH 2/2] move find method from release document set to query --- lib/MetaCPAN/Document/Release/Set.pm | 16 +--------------- lib/MetaCPAN/Query/Release.pm | 24 ++++++++++++++++++++++++ lib/MetaCPAN/Server/Controller/Diff.pm | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/MetaCPAN/Document/Release/Set.pm b/lib/MetaCPAN/Document/Release/Set.pm index e7e7d1268..c052652dd 100644 --- a/lib/MetaCPAN/Document/Release/Set.pm +++ b/lib/MetaCPAN/Document/Release/Set.pm @@ -20,6 +20,7 @@ has query_release => ( by_author by_author_and_name by_author_and_names + find get_contributors get_files latest_by_author @@ -42,21 +43,6 @@ sub _build_query_release { ); } -sub find { - my ( $self, $name ) = @_; - my $file = $self->filter( { - and => [ - { term => { distribution => $name } }, - { term => { status => 'latest' } } - ] - } )->sort( [ { date => 'desc' } ] )->raw->first; - return unless $file; - - my $data = $file->{_source} - || single_valued_arrayref_to_scalar( $file->{fields} ); - return $data; -} - sub find_github_based { shift->filter( { and => [ diff --git a/lib/MetaCPAN/Query/Release.pm b/lib/MetaCPAN/Query/Release.pm index 64b8252eb..512789106 100644 --- a/lib/MetaCPAN/Query/Release.pm +++ b/lib/MetaCPAN/Query/Release.pm @@ -1282,5 +1282,29 @@ sub predecessor { return $release->{_source}; } +sub find { + my ( $self, $name ) = @_; + + my $res = $self->es->search( + index => $self->index_name, + type => 'release', + body => { + query => { + bool => { + must => [ + { term => { distribution => $name } }, + { term => { status => 'latest' } }, + ], + }, + }, + sort => [ { date => 'desc' } ], + size => 1, + }, + ); + my ($file) = $res->{hits}{hits}[0]; + return undef unless $file; + return $file->{_source}; +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/MetaCPAN/Server/Controller/Diff.pm b/lib/MetaCPAN/Server/Controller/Diff.pm index 1c80d77f5..4a6c97181 100644 --- a/lib/MetaCPAN/Server/Controller/Diff.pm +++ b/lib/MetaCPAN/Server/Controller/Diff.pm @@ -33,7 +33,7 @@ sub release : Chained('index') : PathPart('release') : Args(1) { my ( $latest, $previous ); try { - $latest = $c->model('CPAN::Release')->raw->find($name); + $latest = $c->model('CPAN::Release')->find($name); $previous = $c->model('CPAN::Release')->predecessor($name); } catch {