Async/Scroll.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 184 SCAR 48.8
CC
11
Cov
0%
BUILDARGS
20.0
_first_results
20.0
_next_results
6.0
CRAP 132.0
1 # Licensed to Elasticsearch B.V. under one or more contributor
2 # license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright
4 # ownership. Elasticsearch B.V. licenses this file to you under
5 # the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package Search::Elasticsearch::Client::8_0::Async::Scroll;
19 $Search::Elasticsearch::Client::8_0::Async::Scroll::VERSION = '8.00';
20 use Moo;
21 use Search::Elasticsearch::Util qw(parse_params throw);
22 use Search::Elasticsearch::Async::Util qw(thenable);
23 use Scalar::Util qw(weaken blessed);
24 use Promises qw(deferred);
25 use namespace::clean;
26  
27 has 'one_at_a_time' => ( is => 'ro' );
28 has 'on_start' => ( is => 'ro', clearer => '_clear_on_start' );
29 has 'on_results' => ( is => 'ro', clearer => '_clear_on_results' );
30 has 'on_error' => ( is => 'lazy', clearer => '_clear_on_error' );
31 has '_guard' => ( is => 'rwp', clearer => '_clear__guard' );
32  
33 with 'Search::Elasticsearch::Role::Is_Async',
34 'Search::Elasticsearch::Client::8_0::Role::Scroll';
35  
36 #===================================
37 sub BUILDARGS {
38 #===================================
39 my ( $class, $search_params ) = parse_params(@_);
40  
41 my %params;
42 for (qw(es on_start on_result on_results on_error)) {
43 my $val = delete $search_params->{$_};
44 next unless defined $val;
45 $params{$_} = $val;
46 }
47  
48 $params{scroll} = $search_params->{scroll} ||= '1m';
49 $params{search_params} = $search_params;
50  
51 if ( $params{on_result} ) {
52 $params{on_results} = delete $params{on_result};
53 $params{one_at_a_time} = 1;
54 }
55 elsif ( !$params{on_results} ) {
56 throw( 'Param', 'Missing required param: on_results or on_result' );
57 }
58 return \%params;
59 }
60  
61 #===================================
62 sub _build_on_error {
63 #===================================
64 sub { warn "Scroll error: @_"; die @_ }
65 }
66  
67 #===================================
68 sub start {
69 #===================================
70 my $self = shift;
71 $self->_set__guard($self);
72  
73 $self->es->search( $self->search_params )->then(
74 sub {
75 $self->_first_results(@_);
76 }
77 )->then(
78 sub {
79 $self->_fetch_loop;
80 }
81 )->catch(
82 sub {
83 $self->on_error->(@_);
84 @_;
85 }
86 )->finally(
87 sub {
88 $self->finish;
89 $self->_clear__guard;
90 }
91 );
92 }
93  
94 #===================================
95 sub _first_results {
96 #===================================
97 my ( $self, $results ) = @_;
98  
99 my $total = $results->{hits}{total};
100 if (ref $total) {
101 $total = $total->{value};
102 }
103 $self->_set_total($total);
104 $self->_set_max_score( $results->{hits}{max_score} );
105 $self->_set_aggregations( $results->{aggregations} );
106 $self->_set_facets( $results->{facets} );
107 $self->_set_suggest( $results->{suggest} );
108 $self->_set_took( $results->{took} );
109 $self->_set_total_took( $results->{took} );
110  
111 if ($total) {
112 $self->_set__scroll_id( $results->{_scroll_id} );
113 }
114 else {
115 $self->finish;
116 }
117  
118 $self->on_start && $self->on_start->($self);
119  
120 my $hits = $results->{hits}{hits};
121 return unless @$hits;
122 return $self->_push_results($hits);
123 }
124  
125 #===================================
126 sub _next_results {
127 #===================================
128 my ( $self, $results ) = @_;
129 $self->_set__scroll_id( $results->{_scroll_id} );
130 $self->_set_total_took( $self->total_took + $results->{took} );
131  
132 my $hits = $results->{hits}{hits};
133 return $self->finish
134 unless @$hits;
135 $self->_push_results($hits);
136 }
137  
138 #===================================
139 sub _fetch_loop {
140 #===================================
141 my $self = shift;
142 my $d = deferred;
143  
144 my $weak_loop;
145 my $loop = sub {
146 if ( $self->is_finished ) {
147 return $d->resolve;
148 }
149 $self->scroll_request->then( sub { $self->_next_results(@_) } )
150 ->done( $weak_loop, sub { $d->reject(@_) } );
151 };
152 weaken( $weak_loop = $loop );
153 $loop->();
154 return $d->promise;
155 }
156  
157 #===================================
158 sub _push_results {
159 #===================================
160 my $self = shift;
161 my $it = $self->_results_iterator(@_);
162 my $on_results = $self->on_results;
163  
164 my $deferred = deferred;
165  
166 my $weak_process;
167 my $process = sub {
168 while ( !$self->is_finished ) {
169 my @results = $it->() or last;
170 my @response = $on_results->(@results);
171 my $promise = thenable(@response) or next;
172 return $promise->done( $weak_process,
173 sub { $deferred->reject(@_) } );
174 }
175 $deferred->resolve;
176 };
177 weaken( $weak_process = $process );
178 $process->();
179 return $deferred->promise;
180 }
181  
182 #===================================
183 sub _results_iterator {
184 #===================================
185 my $self = shift;
186 my @results = @{ shift() };
187  
188 $self->one_at_a_time
189 ? sub { splice @results, 0, 1 }
190 : sub { splice @results };
191 }
192  
193 #===================================
194 sub finish {
195 #===================================
196 my $self = shift;
197 $self->_set_is_finished(1);
198  
199 my $scroll_id = $self->_scroll_id;
200 $self->_clear_scroll_id;
201  
202 if ( !$scroll_id || $self->_pid != $$ ) {
203 my $d = deferred;
204 $d->resolve();
205 return $d->promise;
206 }
207  
208 my %args = ( body => { scroll_id => $scroll_id } );
209  
210 $self->es->clear_scroll(%args)->then(
211 sub {
212 $self->_clear_on_start;
213 $self->_clear_on_results;
214 $self->_clear_on_error;
215 },
216 sub { }
217 );
218 }
219  
220 1;
221  
222 # ABSTRACT: A helper module for scrolled searches
223  
224 __END__