Async/Scroll.pm untested

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