Skip to content

Commit

Permalink
move find method from release document set to query
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed May 6, 2024
1 parent b95053c commit c0a2b28
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
16 changes: 1 addition & 15 deletions lib/MetaCPAN/Document/Release/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 => [
Expand Down
24 changes: 24 additions & 0 deletions lib/MetaCPAN/Query/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Server/Controller/Diff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c0a2b28

Please sign in to comment.