Async/Scroll.pm untested

filter: Statementstmt 0.0% 0 / 129 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 / 185 CC 11 SCAR 22.0
CC
11
Cov
0%
BUILDARGS
20.0
_first_results
20.0
_next_results
6.0
CRAP 9.0
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::7_0::Async::Scroll;
6 $Search::Elasticsearch::Client::7_0::Async::Scroll::VERSION = '7.30_1'; # TRIAL
7  
8 $Search::Elasticsearch::Client::7_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::7_0::Role::Scroll';
24  
25 #===================================
26 sub BUILDARGS {
27 #===================================
28 my ( $class, $search_params ) = parse_params(@_);
29  
30 my %params;
31 for (qw(es on_start on_result on_results on_error)) {
32 my $val = delete $search_params->{$_};
33 next unless defined $val;
34 $params{$_} = $val;
35 }
36  
37 $params{scroll} = $search_params->{scroll} ||= '1m';
38 $params{search_params} = $search_params;
39  
40 if ( $params{on_result} ) {
41 $params{on_results} = delete $params{on_result};
42 $params{one_at_a_time} = 1;
43 }
44 elsif ( !$params{on_results} ) {
45 throw( 'Param', 'Missing required param: on_results or on_result' );
46 }
47 return \%params;
48 }
49  
50 #===================================
51 sub _build_on_error {
52 #===================================
53 sub { warn "Scroll error: @_"; die @_ }
54 }
55  
56 #===================================
57 sub start {
58 #===================================
59 my $self = shift;
60 $self->_set__guard($self);
61  
62 $self->es->search( $self->search_params )->then(
63 sub {
64 $self->_first_results(@_);
65 }
66 )->then(
67 sub {
68 $self->_fetch_loop;
69 }
70 )->catch(
71 sub {
72 $self->on_error->(@_);
73 @_;
74 }
75 )->finally(
76 sub {
77 $self->finish;
78 $self->_clear__guard;
79 }
80 );
81 }
82  
83 #===================================
84 sub _first_results {
85 #===================================
86 my ( $self, $results ) = @_;
87  
88 my $total = $results->{hits}{total};
89 if (ref $total) {
90 $total = $total->{value};
91 }
92 $self->_set_total($total);
93 $self->_set_max_score( $results->{hits}{max_score} );
94 $self->_set_aggregations( $results->{aggregations} );
95 $self->_set_facets( $results->{facets} );
96 $self->_set_suggest( $results->{suggest} );
97 $self->_set_took( $results->{took} );
98 $self->_set_total_took( $results->{took} );
99  
100 if ($total) {
101 $self->_set__scroll_id( $results->{_scroll_id} );
102 }
103 else {
104 $self->finish;
105 }
106  
107 $self->on_start && $self->on_start->($self);
108  
109 my $hits = $results->{hits}{hits};
110 return unless @$hits;
111 return $self->_push_results($hits);
112 }
113  
114 #===================================
115 sub _next_results {
116 #===================================
117 my ( $self, $results ) = @_;
118 $self->_set__scroll_id( $results->{_scroll_id} );
119 $self->_set_total_took( $self->total_took + $results->{took} );
120  
121 my $hits = $results->{hits}{hits};
122 return $self->finish
123 unless @$hits;
124 $self->_push_results($hits);
125 }
126  
127 #===================================
128 sub _fetch_loop {
129 #===================================
130 my $self = shift;
131 my $d = deferred;
132  
133 my $weak_loop;
134 my $loop = sub {
135 if ( $self->is_finished ) {
136 return $d->resolve;
137 }
138 $self->scroll_request->then( sub { $self->_next_results(@_) } )
139 ->done( $weak_loop, sub { $d->reject(@_) } );
140 };
141 weaken( $weak_loop = $loop );
142 $loop->();
143 return $d->promise;
144 }
145  
146 #===================================
147 sub _push_results {
148 #===================================
149 my $self = shift;
150 my $it = $self->_results_iterator(@_);
151 my $on_results = $self->on_results;
152  
153 my $deferred = deferred;
154  
155 my $weak_process;
156 my $process = sub {
157 while ( !$self->is_finished ) {
158 my @results = $it->() or last;
159 my @response = $on_results->(@results);
160 my $promise = thenable(@response) or next;
161 return $promise->done( $weak_process,
162 sub { $deferred->reject(@_) } );
163 }
164 $deferred->resolve;
165 };
166 weaken( $weak_process = $process );
167 $process->();
168 return $deferred->promise;
169 }
170  
171 #===================================
172 sub _results_iterator {
173 #===================================
174 my $self = shift;
175 my @results = @{ shift() };
176  
177 $self->one_at_a_time
178 ? sub { splice @results, 0, 1 }
179 : sub { splice @results };
180 }
181  
182 #===================================
183 sub finish {
184 #===================================
185 my $self = shift;
186 $self->_set_is_finished(1);
187  
188 my $scroll_id = $self->_scroll_id;
189 $self->_clear_scroll_id;
190  
191 if ( !$scroll_id || $self->_pid != $$ ) {
192 my $d = deferred;
193 $d->resolve();
194 return $d->promise;
195 }
196  
197 my %args = ( body => { scroll_id => $scroll_id } );
198  
199 $self->es->clear_scroll(%args)->then(
200 sub {
201 $self->_clear_on_start;
202 $self->_clear_on_results;
203 $self->_clear_on_error;
204 },
205 sub { }
206 );
207 }
208  
209 1;
210  
211 # ABSTRACT: A helper module for scrolled searches
212  
213 __END__