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

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