Async/Scroll.pm untested

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