Filename | /opt/flows/lib/lib/perl5/Search/Elasticsearch/Util/API/Path.pm |
Statements | Executed 34 statements in 642µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 323µs | 572µs | BEGIN@5 | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 25µs | 53µs | path_handler | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 16µs | 30µs | BEGIN@3 | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 11µs | 23µs | BEGIN@18 | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 11µs | 249µs | BEGIN@7 | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 9µs | 14µs | BEGIN@4 | Search::Elasticsearch::Util::API::Path::
1 | 1 | 1 | 9µs | 147µs | BEGIN@6 | Search::Elasticsearch::Util::API::Path::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Search::Elasticsearch::Util::API::Path; | ||||
2 | 1 | 1µs | $Search::Elasticsearch::Util::API::Path::VERSION = '2.02'; | ||
3 | 2 | 31µs | 2 | 44µs | # spent 30µs (16+14) within Search::Elasticsearch::Util::API::Path::BEGIN@3 which was called:
# once (16µs+14µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 3 # spent 30µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@3
# spent 14µs making 1 call to strict::import |
4 | 2 | 29µs | 2 | 19µs | # spent 14µs (9+5) within Search::Elasticsearch::Util::API::Path::BEGIN@4 which was called:
# once (9µs+5µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 4 # spent 14µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@4
# spent 5µs making 1 call to warnings::import |
5 | 2 | 114µs | 2 | 614µs | # spent 572µs (323+249) within Search::Elasticsearch::Util::API::Path::BEGIN@5 which was called:
# once (323µs+249µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 5 # spent 572µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@5
# spent 42µs making 1 call to Exporter::import |
6 | 2 | 43µs | 2 | 285µs | # spent 147µs (9+138) within Search::Elasticsearch::Util::API::Path::BEGIN@6 which was called:
# once (9µs+138µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 6 # spent 147µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@6
# spent 138µs making 1 call to Sub::Exporter::__ANON__[/opt/flows/lib/lib/perl5/Sub/Exporter.pm:337] |
7 | 2 | 80µs | 2 | 487µs | # spent 249µs (11+238) within Search::Elasticsearch::Util::API::Path::BEGIN@7 which was called:
# once (11µs+238µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 7 # spent 249µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@7
# spent 238µs making 1 call to Sub::Exporter::__ANON__[/opt/flows/lib/lib/perl5/Sub/Exporter.pm:337] |
8 | |||||
9 | #=================================== | ||||
10 | # spent 53µs (25+28) within Search::Elasticsearch::Util::API::Path::path_handler which was called:
# once (25µs+28µs) by Search::Elasticsearch::Role::Client::Direct::_parse_path at line 46 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Client/Direct.pm | ||||
11 | #=================================== | ||||
12 | 1 | 300ns | my ( $defn, $params ) = @_; | ||
13 | 1 | 400ns | my $paths = $defn->{paths}; | ||
14 | 1 | 300ns | my $parts = $defn->{parts}; | ||
15 | |||||
16 | 1 | 200ns | my %args; | ||
17 | 1 | 600ns | keys %$parts; | ||
18 | 2 | 313µs | 2 | 34µs | # spent 23µs (11+12) within Search::Elasticsearch::Util::API::Path::BEGIN@18 which was called:
# once (11µs+12µs) by Search::Elasticsearch::Role::Client::Direct::BEGIN@5 at line 18 # spent 23µs making 1 call to Search::Elasticsearch::Util::API::Path::BEGIN@18
# spent 12µs making 1 call to warnings::unimport |
19 | 1 | 3µs | while ( my ( $key, $req ) = each %$parts ) { | ||
20 | 1 | 800ns | my $val = delete $params->{$key}; | ||
21 | 1 | 400ns | if ( ref $val eq 'ARRAY' ) { | ||
22 | die "Param ($key) must contain a single value\n" | ||||
23 | if @$val > 1 and not $req->{multi}; | ||||
24 | $val = join ",", @$val; | ||||
25 | } | ||||
26 | 1 | 400ns | if ( !length $val ) { | ||
27 | die "Missing required param ($key)\n" | ||||
28 | if $req->{required}; | ||||
29 | next; | ||||
30 | } | ||||
31 | 1 | 6µs | 1 | 1µs | utf8::encode($val); # spent 1µs making 1 call to utf8::encode |
32 | 1 | 2µs | 1 | 27µs | $args{$key} = uri_escape($val); # spent 27µs making 1 call to URI::Escape::uri_escape |
33 | } | ||||
34 | 1 | 600ns | PATH: for my $path (@$paths) { | ||
35 | 1 | 2µs | my @keys = keys %{ $path->[0] }; | ||
36 | 1 | 700ns | next PATH unless @keys == keys %args; | ||
37 | 1 | 600ns | for (@keys) { | ||
38 | 1 | 1µs | next PATH unless exists $args{$_}; | ||
39 | } | ||||
40 | 1 | 600ns | my ( $pos, @parts ) = @$path; | ||
41 | 1 | 800ns | for ( keys %$pos ) { | ||
42 | 1 | 1µs | $parts[ $pos->{$_} ] = $args{$_}; | ||
43 | } | ||||
44 | 1 | 6µs | return join "/", '', @parts; | ||
45 | } | ||||
46 | |||||
47 | die "Param (index) required when (type) specified\n" | ||||
48 | if $defn->{index_when_type} && $args{type} && !$args{index}; | ||||
49 | |||||
50 | throw( | ||||
51 | 'Internal', | ||||
52 | "Couldn't determine path", | ||||
53 | { params => $params, defn => $defn } | ||||
54 | ); | ||||
55 | } | ||||
56 | |||||
57 | 1 | 3µs | 1; | ||
58 | |||||
59 | =pod | ||||
60 | |||||
61 | =encoding UTF-8 | ||||
62 | |||||
63 | =head1 NAME | ||||
64 | |||||
65 | Search::Elasticsearch::Util::API::Path - A utility class for converting path templates into real paths | ||||
66 | |||||
67 | =head1 VERSION | ||||
68 | |||||
69 | version 2.02 | ||||
70 | |||||
71 | =head1 DESCRIPTION | ||||
72 | |||||
73 | This module converts path templates in L<Search::Elasticsearch::Role::API> such as | ||||
74 | C</{index}/{type}/{id}> into real paths such as C</my_index/my_type/123>. | ||||
75 | |||||
76 | =head1 EXPORTS | ||||
77 | |||||
78 | =head2 C<path_init()> | ||||
79 | |||||
80 | use Search::Elasticsearch::Util::API::Path qw(path_init); | ||||
81 | |||||
82 | $handler = path_init($template); | ||||
83 | $path = $handler->(\%params); | ||||
84 | |||||
85 | The C<path_init()> sub accepts a path template and returns an anonymous sub | ||||
86 | which converts C<\%params> into a real path, removing the keys that it | ||||
87 | has used from C<%params>, eg: | ||||
88 | |||||
89 | $handler = path_init('/{indices}/_search'); | ||||
90 | $params = { index => ['foo','bar'], size => 10 }; | ||||
91 | $path = $handler->($params); | ||||
92 | |||||
93 | Would result in: | ||||
94 | |||||
95 | $path: '/foo,bar/_search'; | ||||
96 | $params: { size => 10 }; | ||||
97 | |||||
98 | =head1 AUTHOR | ||||
99 | |||||
100 | Clinton Gormley <drtech@cpan.org> | ||||
101 | |||||
102 | =head1 COPYRIGHT AND LICENSE | ||||
103 | |||||
104 | This software is Copyright (c) 2016 by Elasticsearch BV. | ||||
105 | |||||
106 | This is free software, licensed under: | ||||
107 | |||||
108 | The Apache License, Version 2.0, January 2004 | ||||
109 | |||||
110 | =cut | ||||
111 | |||||
112 | __END__ |