Skip to content

Commit 42e05fe

Browse files
committed
move find and find_pod methods to query
1 parent cc99e50 commit 42e05fe

File tree

11 files changed

+182
-144
lines changed

11 files changed

+182
-144
lines changed

lib/MetaCPAN/Document/File/Set.pm

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,106 +7,6 @@ use MetaCPAN::Util qw( true false );
77

88
extends 'ElasticSearchX::Model::Document::Set';
99

10-
sub find {
11-
my ( $self, $module ) = @_;
12-
13-
my $query = {
14-
bool => {
15-
must => [
16-
{ term => { indexed => true } },
17-
{ term => { authorized => true } },
18-
{ term => { status => 'latest' } },
19-
{
20-
bool => {
21-
should => [
22-
{ term => { documentation => $module } },
23-
{
24-
nested => {
25-
path => "module",
26-
query => {
27-
bool => {
28-
must => [
29-
{
30-
term => { "module.name" =>
31-
$module }
32-
},
33-
{
34-
bool => { should =>
35-
[
36-
{ term =>
37-
{ "module.authorized"
38-
=> true
39-
} },
40-
{ exists =>
41-
{ field =>
42-
'module.associated_pod'
43-
} },
44-
],
45-
}
46-
},
47-
],
48-
},
49-
},
50-
}
51-
},
52-
]
53-
}
54-
},
55-
],
56-
},
57-
};
58-
59-
my $res = $self->es->search(
60-
es_doc_path('file'),
61-
search_type => 'dfs_query_then_fetch',
62-
body => {
63-
query => $query,
64-
sort => [
65-
'_score',
66-
{ 'version_numified' => { order => 'desc' } },
67-
{ 'date' => { order => 'desc' } },
68-
{ 'mime' => { order => 'asc' } },
69-
{ 'stat.mtime' => { order => 'desc' } }
70-
],
71-
_source => [
72-
qw( documentation module.indexed module.authoried module.name )
73-
],
74-
size => 100,
75-
},
76-
);
77-
78-
my @candidates = @{ $res->{hits}{hits} };
79-
80-
my ($file) = grep {
81-
grep { $_->{indexed} && $_->{authorized} && $_->{name} eq $module }
82-
@{ $_->{module} || [] }
83-
} grep { !$_->{documentation} || $_->{documentation} eq $module }
84-
@candidates;
85-
86-
$file ||= shift @candidates;
87-
return $file ? $self->get( $file->{_id} ) : undef;
88-
}
89-
90-
sub find_pod {
91-
my ( $self, $name ) = @_;
92-
my $file = $self->find($name);
93-
return $file unless ($file);
94-
my ($module)
95-
= grep { $_->indexed && $_->authorized && $_->name eq $name }
96-
@{ $file->module || [] };
97-
if ( $module && ( my $pod = $module->associated_pod ) ) {
98-
my ( $author, $release, @path ) = split( /\//, $pod );
99-
return $self->get( {
100-
author => $author,
101-
release => $release,
102-
path => join( '/', @path ),
103-
} );
104-
}
105-
else {
106-
return $file;
107-
}
108-
}
109-
11010
=head2 history
11111
11212
Find the history of a given module/documentation.

lib/MetaCPAN/Query/File.pm

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,5 +559,121 @@ sub documented_modules {
559559
};
560560
}
561561

562+
sub find_module {
563+
my ( $self, $module, $fields ) = @_;
564+
565+
my $query = {
566+
bool => {
567+
must => [
568+
{ term => { indexed => true } },
569+
{ term => { authorized => true } },
570+
{ term => { status => 'latest' } },
571+
{
572+
bool => {
573+
should => [
574+
{ term => { documentation => $module } },
575+
{
576+
nested => {
577+
path => "module",
578+
query => {
579+
bool => {
580+
must => [
581+
{
582+
term => { "module.name" =>
583+
$module }
584+
},
585+
{
586+
bool => { should =>
587+
[
588+
{ term =>
589+
{ "module.authorized"
590+
=> true
591+
} },
592+
{ exists =>
593+
{ field =>
594+
'module.associated_pod'
595+
} },
596+
],
597+
}
598+
},
599+
],
600+
},
601+
},
602+
}
603+
},
604+
]
605+
}
606+
},
607+
],
608+
},
609+
};
610+
611+
my $res = $self->es->search(
612+
es_doc_path('file'),
613+
search_type => 'dfs_query_then_fetch',
614+
body => {
615+
query => $query,
616+
sort => [
617+
'_score',
618+
{ 'version_numified' => { order => 'desc' } },
619+
{ 'date' => { order => 'desc' } },
620+
{ 'mime' => { order => 'asc' } },
621+
{ 'stat.mtime' => { order => 'desc' } }
622+
],
623+
_source => [
624+
qw( documentation module.indexed module.authoried module.name )
625+
],
626+
size => 100,
627+
},
628+
);
629+
630+
my @candidates = @{ $res->{hits}{hits} };
631+
632+
my ($file) = grep {
633+
grep { $_->{indexed} && $_->{authorized} && $_->{name} eq $module }
634+
@{ $_->{module} || [] }
635+
} grep { !$_->{documentation} || $_->{documentation} eq $module }
636+
@candidates;
637+
638+
$file ||= shift @candidates;
639+
return undef
640+
if !$file;
641+
return $self->es->get_source( es_doc_path('file'), $file->{_id},
642+
( $fields ? _source => $fields : () ),
643+
);
644+
}
645+
646+
sub find_pod {
647+
my ( $self, $name ) = @_;
648+
my $file = $self->find_module($name);
649+
return $file
650+
unless $file;
651+
my ($module)
652+
= grep { $_->{indexed} && $_->{authorized} && $_->{name} eq $name }
653+
@{ $file->{module} || [] };
654+
if ( $module && ( my $pod = $module->{associated_pod} ) ) {
655+
my ( $author, $release, @path ) = split( /\//, $pod );
656+
my $query = {
657+
bool => {
658+
must => [
659+
{ term => { author => $author } },
660+
{ term => { release => $release } },
661+
{ term => { path => join( '/', @path ) } },
662+
],
663+
},
664+
};
665+
my $pod_file = $self->es->search(
666+
es_doc_path('file'),
667+
body => {
668+
query => $query,
669+
},
670+
);
671+
return $pod_file->{hits}{hits}[0];
672+
}
673+
else {
674+
return $file;
675+
}
676+
}
677+
562678
__PACKAGE__->meta->make_immutable;
563679
1;

lib/MetaCPAN/Server/Controller/Module.pm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ has '+type' => ( default => 'file' );
1212

1313
sub get : Path('') : Args(1) {
1414
my ( $self, $c, $name ) = @_;
15-
my $file = $self->model($c)->raw->find($name);
15+
my $file
16+
= $c->model('ESQuery')->file->find_module( $name, $c->req->fields );
1617
if ( !defined $file ) {
1718
$c->detach( '/not_found', [] );
1819
}
19-
$c->stash( $file->{_source}
20-
|| single_valued_arrayref_to_scalar( $file->{fields} ) )
21-
|| $c->detach( '/not_found',
22-
['The requested field(s) could not be found'] );
20+
$c->stash($file);
2321
}
2422

2523
__PACKAGE__->meta->make_immutable();

lib/MetaCPAN/Server/Controller/Pod.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ sub find : Path('') {
3636

3737
sub get : Path('') : Args(1) {
3838
my ( $self, $c, $module ) = @_;
39-
$module = $c->model('ESModel')->doc('file')->find_pod($module)
39+
$module = $c->model('ESQuery')->file->find_pod($module)
4040
or $c->detach( '/not_found', [] );
41-
$c->forward( 'find', [ map { $module->$_ } qw(author release path) ] );
41+
$c->forward( 'find', [ map { $module->{$_} } qw(author release path) ] );
4242
}
4343

4444
sub find_dist_links {

lib/MetaCPAN/Server/Controller/Source.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ sub module : Chained('index') : PathPart('') : Args(1) {
6363

6464
$c->cdn_never_cache(1);
6565

66-
$module = $c->model('ESModel')->doc('file')->find($module)
66+
my $file
67+
= $c->model('ESQuery')
68+
->file->find_module( $module, [qw(author release path)] )
6769
or $c->detach( '/not_found', [] );
68-
$c->forward( 'get', [ map { $module->$_ } qw(author release path) ] );
70+
71+
$c->forward( 'get', [ map { $file->{$_} } qw(author release path) ] );
6972
}
7073

7174
1;

lib/MetaCPAN/Server/Role/Request.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ around [qw(content_type header)] => sub {
1414
: $header;
1515
};
1616

17+
sub fields {
18+
my $self = shift;
19+
my @fields = map { split /,/ } $self->param('fields');
20+
return @fields ? \@fields : undef;
21+
}
22+
1723
1;

t/lib/MetaCPAN/Server/Test.pm

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package MetaCPAN::Server::Test;
22

33
use strict;
44
use warnings;
5+
use feature qw(state);
56

6-
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
7-
use MetaCPAN::Model ();
8-
use MetaCPAN::Server ();
9-
use MetaCPAN::Server::Config ();
10-
use Plack::Test; ## no perlimports
7+
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
8+
use MetaCPAN::Model ();
9+
use MetaCPAN::Server ();
10+
use MetaCPAN::Server::Config ();
11+
use MooseX::Types::ElasticSearch qw( ES );
12+
use Plack::Test; ## no perlimports
1113

1214
use base 'Exporter';
1315
our @EXPORT_OK = qw(
1416
POST GET DELETE
17+
es
1518
model
1619
test_psgi app
20+
query
1721
);
1822

1923
# Begin the load-order dance.
@@ -38,9 +42,19 @@ sub app {
3842
return $app;
3943
}
4044

45+
sub es {
46+
state $es = do {
47+
my $c = MetaCPAN::Server::Config::config();
48+
ES->assert_coerce($c);
49+
};
50+
}
51+
4152
sub model {
42-
my $c = MetaCPAN::Server::Config::config();
43-
MetaCPAN::Model->new( es => $c->{elasticsearch_servers} );
53+
state $model = MetaCPAN::Model->new( es => es() );
54+
}
55+
56+
sub query {
57+
state $query = MetaCPAN::Query->new( es => es() );
4458
}
4559

4660
1;

t/release/moose.t

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ ok(
5151

5252
is( $ppport->name, 'ppphdoc', 'name doesn\'t contain a dot' );
5353

54-
ok( my $moose = $model->doc('file')->find('Moose'), 'find Moose module' );
55-
56-
is( $moose->name, 'Moose.pm', 'defined in Moose.pm' );
57-
58-
is( $moose->module->[0]->associated_pod, 'DOY/Moose-0.02/lib/Moose.pm' );
59-
6054
my $signature;
6155
$signature = $model->doc('file')->query( {
6256
bool => {

0 commit comments

Comments
 (0)