Elasticsearch/Client/7_0/Role/Bulk.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 259 CC 26 SCAR 37.7
CC
26
Cov
0%
_encode_action
90.0
_doc_transformer
72.0
_report
42.0
CRAP 43.4
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::Role::Bulk;
6 $Search::Elasticsearch::Client::7_0::Role::Bulk::VERSION = '7.30_1'; # TRIAL
7  
8 $Search::Elasticsearch::Client::7_0::Role::Bulk::VERSION = '7.301';
9 use Moo::Role;
10 requires 'add_action', 'flush';
11  
12 use Search::Elasticsearch::Util qw(parse_params throw);
13 use namespace::clean;
14  
15 has 'es' => ( is => 'ro', required => 1 );
16 has 'max_count' => ( is => 'rw', default => 1_000 );
17 has 'max_size' => ( is => 'rw', default => 1_000_000 );
18 has 'max_time' => ( is => 'rw', default => 0 );
19  
20 has 'on_success' => ( is => 'ro', default => 0 );
21 has 'on_error' => ( is => 'lazy' );
22 has 'on_conflict' => ( is => 'ro', default => 0 );
23 has 'verbose' => ( is => 'rw' );
24  
25 has '_buffer' => ( is => 'ro', default => sub { [] } );
26 has '_buffer_size' => ( is => 'rw', default => 0 );
27 has '_buffer_count' => ( is => 'rw', default => 0 );
28 has '_serializer' => ( is => 'lazy' );
29 has '_bulk_args' => ( is => 'ro' );
30 has '_last_flush' => ( is => 'rw', default => sub {time} );
31 has '_metadata_params' => ( is => 'ro' );
32 has '_update_params' => ( is => 'ro' );
33 has '_required_params' => ( is => 'ro' );
34  
35 our %Actions = (
36 'index' => 1,
37 'create' => 1,
38 'update' => 1,
39 'delete' => 1
40 );
41  
42 #===================================
43 sub _build__serializer { shift->es->transport->serializer }
44 #===================================
45  
46 #===================================
47 sub _build_on_error {
48 #===================================
49 my $self = shift;
50 my $serializer = $self->_serializer;
51 return sub {
52 my ( $action, $result, $src ) = @_;
53 warn( "Bulk error [$action]: " . $serializer->encode($result) );
54 };
55 }
56  
57 #===================================
58 sub BUILDARGS {
59 #===================================
60 my ( $class, $params ) = parse_params(@_);
61 my $es = $params->{es} or throw( 'Param', 'Missing required param <es>' );
62 $params->{_metadata_params} = $es->api('bulk.metadata')->{params};
63 $params->{_update_params} = $es->api('bulk.update')->{params};
64 $params->{_required_params} = $es->api('bulk.required')->{params};
65 my $bulk_spec = $es->api('bulk');
66 my %args;
67 for ( keys %{ $bulk_spec->{qs} }, keys %{ $bulk_spec->{parts} } ) {
68 $args{$_} = delete $params->{$_}
69 if exists $params->{$_};
70 }
71 $params->{_bulk_args} = \%args;
72 return $params;
73 }
74  
75 #===================================
76 sub index {
77 #===================================
78 shift->add_action( map { ( 'index' => $_ ) } @_ );
79 }
80  
81 #===================================
82 sub create {
83 #===================================
84 shift->add_action( map { ( 'create' => $_ ) } @_ );
85 }
86  
87 #===================================
88 sub delete {
89 #===================================
90 shift->add_action( map { ( 'delete' => $_ ) } @_ );
91 }
92  
93 #===================================
94 sub update {
95 #===================================
96 shift->add_action( map { ( 'update' => $_ ) } @_ );
97 }
98  
99 #===================================
100 sub create_docs {
101 #===================================
102 my $self = shift;
103 $self->add_action( map { ( 'create' => { source => $_ } ) } @_ );
104 }
105  
106 #===================================
107 sub delete_ids {
108 #===================================
109 my $self = shift;
110 $self->add_action( map { ( 'delete' => { _id => $_ } ) } @_ );
111 }
112  
113 #===================================
114 sub _encode_action {
115 #===================================
116 my $self = shift;
117 my $action = shift || '';
118 my $orig = shift;
119  
120 throw( 'Param', "Unrecognised action <$action>" )
121 unless $Actions{$action};
122  
123 throw( 'Param', "Missing <params> for action <$action>" )
124 unless ref($orig) eq 'HASH';
125  
126 my %metadata;
127 my $params = {%$orig};
128 my $serializer = $self->_serializer;
129  
130 my $meta_params = $self->_metadata_params;
131 for ( keys %$meta_params ) {
132 next unless exists $params->{$_};
133 $metadata{ $meta_params->{$_} } = delete $params->{$_};
134 }
135  
136 for ( @{ $self->_required_params } ) {
137 throw( 'Param', "Missing required param <$_>" )
138 unless $metadata{"_$_"} || $self->_bulk_args->{$_};
139 }
140  
141 my $source;
142 if ( $action eq 'update' ) {
143 for ( @{ $self->_update_params } ) {
144 $source->{$_} = delete $params->{$_}
145 if exists $params->{$_};
146 }
147 }
148 elsif ( $action ne 'delete' ) {
149 $source = delete $params->{source}
150 || throw( 'Param',
151 "Missing <source> for action <$action>: "
152 . $serializer->encode($orig) );
153 }
154  
155 throw( "Unknown params <"
156 . ( join ',', sort keys %$params )
157 . "> in <$action>: "
158 . $serializer->encode($orig) )
159 if keys %$params;
160  
161 return map { $serializer->encode($_) }
162 grep {$_} ( { $action => \%metadata }, $source );
163 }
164  
165 #===================================
166 sub _report {
167 #===================================
168 my ( $self, $buffer, $results ) = @_;
169 my $on_success = $self->on_success;
170 my $on_error = $self->on_error;
171 my $on_conflict = $self->on_conflict;
172  
173 # assume errors if key not present, bwc
174 $results->{errors} = 1 unless exists $results->{errors};
175  
176 return
177 unless $on_success
178 || ( $results->{errors} and $on_error || $on_conflict );
179  
180 my $serializer = $self->_serializer;
181  
182 my $j = 0;
183  
184 for my $item ( @{ $results->{items} } ) {
185 my ( $action, $result ) = %$item;
186 my @args = ($action);
187 if ( my $error = $result->{error} ) {
188 if ($on_conflict) {
189 my ( $is_conflict, $version )
190 = $self->_is_conflict_error($error);
191 if ($is_conflict) {
192 $on_conflict->( $action, $result, $j, $version );
193 next;
194 }
195 }
196 $on_error && $on_error->( $action, $result, $j );
197 }
198 else {
199 $on_success && $on_success->( $action, $result, $j );
200 }
201 $j++;
202 }
203 }
204  
205 #===================================
206 sub _is_conflict_error {
207 #===================================
208 my ( $self, $error ) = @_;
209 my $version;
210 if ( ref($error) eq 'HASH' ) {
211 return 1 if $error->{type} eq 'document_already_exists_exception';
212 return unless $error->{type} eq 'version_conflict_engine_exception';
213 $error->{reason} =~ /version.conflict,.current.(?:version.)?\[(\d+)\]/;
214 return ( 1, $1 );
215 }
216 return unless $error =~ /
217 DocumentAlreadyExistsException
218 |version.conflict,.current.\[(\d+)\]
219 /x;
220 return ( 1, $1 );
221 }
222  
223 #===================================
224 sub clear_buffer {
225 #===================================
226 my $self = shift;
227 @{ $self->_buffer } = ();
228 $self->_buffer_size(0);
229 $self->_buffer_count(0);
230 }
231  
232 #===================================
233 sub _doc_transformer {
234 #===================================
235 my ( $self, $params ) = @_;
236  
237 my $bulk_args = $self->_bulk_args;
238 my %allowed = map { $_ => 1, "_$_" => 1 }
239 ( @{ $self->_metadata_params }, 'source' );
240 $allowed{fields} = 1;
241  
242 delete @allowed{ 'index', '_index' } if $bulk_args->{index};
243 delete @allowed{ 'type', '_type' } if $bulk_args->{type};
244  
245 my $version_type = $params->{version_type};
246 my $transform = $params->{transform};
247  
248 return sub {
249 my %doc = %{ shift() };
250 for ( keys %doc ) {
251 delete $doc{$_} unless $allowed{$_};
252 }
253  
254 if ( my $fields = delete $doc{fields} ) {
255 for (qw(_routing routing _parent parent)) {
256 $doc{$_} = $fields->{$_}
257 if exists $fields->{$_};
258 }
259 }
260 $doc{_version_type} = $version_type if $version_type;
261  
262 return \%doc unless $transform;
263 return $transform->( \%doc );
264 };
265 }
266  
267 1;
268  
269 # ABSTRACT: Provides common functionality to L<Elasticseach::Client::7_0::Bulk> and L<Search::Elasticsearch::Client::7_0::Async::Bulk>
270  
271 __END__