Client/6_0/Async/Scroll.pm untested

filter: Statementstmt 0.0% 0 / 129 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 / 189 CC 13 SCAR 24.7
CC
13
Cov
0%
BUILDARGS
30.0
_first_results
20.0
finish
12.0
CRAP 11.9
1 package Search::Elasticsearch::Client::6_0::Async::Scroll;
2 $Search::Elasticsearch::Client::6_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::6_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 = $results->{hits}{total};
87 if (ref $total) {
88 $total = $total->{value};
89 }
90 $self->_set_total($total);
91 $self->_set_max_score( $results->{hits}{max_score} );
92 $self->_set_aggregations( $results->{aggregations} );
93 $self->_set_facets( $results->{facets} );
94 $self->_set_suggest( $results->{suggest} );
95 $self->_set_took( $results->{took} );
96 $self->_set_total_took( $results->{took} );
97  
98 if ($total) {
99 $self->_set__scroll_id( $results->{_scroll_id} );
100 }
101 else {
102 $self->finish;
103 }
104  
105 $self->on_start && $self->on_start->($self);
106  
107 my $hits = $results->{hits}{hits};
108 return unless @$hits;
109 return $self->_push_results($hits);
110 }
111  
112 #===================================
113 sub _next_results {
114 #===================================
115 my ( $self, $results ) = @_;
116 $self->_set__scroll_id( $results->{_scroll_id} );
117 $self->_set_total_took( $self->total_took + $results->{took} );
118  
119 my $hits = $results->{hits}{hits};
120 return $self->finish
121 unless @$hits;
122 $self->_push_results($hits);
123 }
124  
125 #===================================
126 sub _fetch_loop {
127 #===================================
128 my $self = shift;
129 my $d = deferred;
130  
131 my $weak_loop;
132 my $loop = sub {
133 if ( $self->is_finished ) {
134 return $d->resolve;
135 }
136 $self->scroll_request->then( sub { $self->_next_results(@_) } )
137 ->done( $weak_loop, sub { $d->reject(@_) } );
138 };
139 weaken( $weak_loop = $loop );
140 $loop->();
141 return $d->promise;
142 }
143  
144 #===================================
145 sub _push_results {
146 #===================================
147 my $self = shift;
148 my $it = $self->_results_iterator(@_);
149 my $on_results = $self->on_results;
150  
151 my $deferred = deferred;
152  
153 my $weak_process;
154 my $process = sub {
155 while ( !$self->is_finished ) {
156 my @results = $it->() or last;
157 my @response = $on_results->(@results);
158 my $promise = thenable(@response) or next;
159 return $promise->done( $weak_process,
160 sub { $deferred->reject(@_) } );
161 }
162 $deferred->resolve;
163 };
164 weaken( $weak_process = $process );
165 $process->();
166 return $deferred->promise;
167 }
168  
169 #===================================
170 sub _results_iterator {
171 #===================================
172 my $self = shift;
173 my @results = @{ shift() };
174  
175 $self->one_at_a_time
176 ? sub { splice @results, 0, 1 }
177 : sub { splice @results };
178 }
179  
180 #===================================
181 sub finish {
182 #===================================
183 my $self = shift;
184 $self->_set_is_finished(1);
185  
186 my $scroll_id = $self->_scroll_id;
187 $self->_clear_scroll_id;
188  
189 if ( !$scroll_id || $self->_pid != $$ ) {
190 my $d = deferred;
191 $d->resolve();
192 return $d->promise;
193 }
194  
195 my %args
196 = $self->scroll_in_qs
197 ? ( scroll_id => $scroll_id )
198 : ( body => { scroll_id => $scroll_id } );
199  
200 $self->es->clear_scroll(%args)->then(
201 sub {
202 $self->_clear_on_start;
203 $self->_clear_on_results;
204 $self->_clear_on_error;
205 },
206 sub { }
207 );
208 }
209  
210 1;
211  
212 # ABSTRACT: A helper module for scrolled searches
213  
214 __END__