Client/Core/3_0/Role/Helper/Bulk.pm untested

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