5_0/Bulk.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 89 CC 6 SCAR 27.4
CC
6
Cov
0%
flush
30.0
add_action
6.0
BEGIN
2.0
CRAP 15.5
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::5_0::Bulk;
6 $Search::Elasticsearch::Client::5_0::Bulk::VERSION = '7.30_1'; # TRIAL
7  
8 $Search::Elasticsearch::Client::5_0::Bulk::VERSION = '7.301';
9 use Moo;
10 with 'Search::Elasticsearch::Client::5_0::Role::Bulk',
11 'Search::Elasticsearch::Role::Is_Sync';
12 use Search::Elasticsearch::Util qw(parse_params throw);
13 use Try::Tiny;
14 use namespace::clean;
15  
16 #===================================
17 sub add_action {
18 #===================================
19 my $self = shift;
20 my $buffer = $self->_buffer;
21 my $max_size = $self->max_size;
22 my $max_count = $self->max_count;
23 my $max_time = $self->max_time;
24  
25 while (@_) {
26 my @json = $self->_encode_action( splice( @_, 0, 2 ) );
27  
28 push @$buffer, @json;
29  
30 my $size = $self->_buffer_size;
31 $size += length($_) + 1 for @json;
32 $self->_buffer_size($size);
33  
34 my $count = $self->_buffer_count( $self->_buffer_count + 1 );
35  
36 $self->flush
37 if ( $max_size and $size >= $max_size )
38 || ( $max_count and $count >= $max_count )
39 || ( $max_time and time >= $self->_last_flush + $max_time );
40 }
41 return 1;
42 }
43  
44 #===================================
45 sub flush {
46 #===================================
47 my $self = shift;
48 $self->_last_flush(time);
49  
50 return { items => [] }
51 unless $self->_buffer_size;
52  
53 if ( $self->verbose ) {
54 local $| = 1;
55 print ".";
56 }
57 my $buffer = $self->_buffer;
58 my $results = try {
59 my $res = $self->es->bulk( %{ $self->_bulk_args }, body => $buffer );
60 $self->clear_buffer;
61 return $res;
62 }
63 catch {
64 my $error = $_;
65 $self->clear_buffer
66 unless $error->is( 'Cxn', 'NoNodes' );
67  
68 die $error;
69 };
70 $self->_report( $buffer, $results );
71 return defined wantarray ? $results : undef;
72 }
73  
74 1;
75  
76 =pod
77  
78 =encoding UTF-8
79  
80 =head1 NAME
81  
82 Search::Elasticsearch::Client::5_0::Bulk - A helper module for the Bulk API
83  
84 =head1 VERSION
85  
86 version 7.30_1
87  
88 =head1 SYNOPSIS
89  
90 use Search::Elasticsearch;
91  
92 my $es = Search::Elasticsearch->new;
93 my $bulk = $es->bulk_helper(
94 index => 'my_index',
95 type => 'my_type'
96 );
97  
98 # Index docs:
99 $bulk->index({ id => 1, source => { foo => 'bar' }});
100 $bulk->add_action( index => { id => 1, source => { foo=> 'bar' }});
101  
102 # Create docs:
103 $bulk->create({ id => 1, source => { foo => 'bar' }});
104 $bulk->add_action( create => { id => 1, source => { foo=> 'bar' }});
105 $bulk->create_docs({ foo => 'bar' })
106  
107 # Delete docs:
108 $bulk->delete({ id => 1});
109 $bulk->add_action( delete => { id => 1 });
110 $bulk->delete_ids(1,2,3)
111  
112 # Update docs:
113 $bulk->update({ id => 1, script => '...' });
114 $bulk->add_action( update => { id => 1, script => '...' });
115  
116 # Manual flush
117 $bulk->flush;
118  
119 =head1 DESCRIPTION
120  
121 This module provides a wrapper for the L<Search::Elasticsearch::Client::5_0::Direct/bulk()>
122 method which makes it easier to run multiple create, index, update or delete
123 actions in a single request.
124  
125 The L<Search::Elasticsearch::Client::5_0::Bulk> module acts as a queue, buffering up actions
126 until it reaches a maximum count of actions, or a maximum size of JSON request
127 body, at which point it issues a C<bulk()> request.
128  
129 Once you have finished adding actions, call L</flush()> to force the final
130 C<bulk()> request on the items left in the queue.
131  
132 This class does L<Search::Elasticsearch::Client::5_0::Role::Bulk> and
133 L<Search::Elasticsearch::Role::Is_Sync>.
134  
135 =head1 CREATING A NEW INSTANCE
136  
137 =head2 C<new()>
138  
139 my $bulk = $es->bulk_helper(
140  
141 index => 'default_index', # optional
142 type => 'default_type', # optional
143 %other_bulk_params # optional
144  
145 max_count => 1_000, # optional
146 max_size => 1_000_000, # optional
147 max_time => 5, # optional
148  
149 verbose => 0 | 1, # optional
150  
151 on_success => sub {...}, # optional
152 on_error => sub {...}, # optional
153 on_conflict => sub {...}, # optional
154  
155  
156 );
157  
158 The C<new()> method returns a new C<$bulk> object. You must pass your
159 Search::Elasticsearch client as the C<es> argument.
160  
161 The C<index> and C<type> parameters provide default values for
162 C<index> and C<type>, which can be overridden in each action.
163 You can also pass any other values which are accepted
164 by the L<bulk()|Search::Elasticsearch::Client::5_0::Direct/bulk()> method.
165  
166 See L</flush()> for more information about the other parameters.
167  
168 =head1 FLUSHING THE BUFFER
169  
170 =head2 C<flush()>
171  
172 $result = $bulk->flush;
173  
174 The C<flush()> method sends all buffered actions to Elasticsearch using
175 a L<bulk()|Search::Elasticsearch::Client::5_0::Direct/bulk()> request.
176  
177 =head2 Auto-flushing
178  
179 An automatic L</flush()> is triggered whenever the C<max_count>, C<max_size>,
180 or C<max_time> threshold is breached. This causes all actions in the buffer to be
181 sent to Elasticsearch.
182  
183 =over
184  
185 =item * C<max_count>
186  
187 The maximum number of actions to allow before triggering a L</flush()>.
188 This can be disabled by setting C<max_count> to C<0>. Defaults to
189 C<1,000>.
190  
191 =item * C<max_size>
192  
193 The maximum size of JSON request body to allow before triggering a
194 L</flush()>. This can be disabled by setting C<max_size> to C<0>. Defaults
195 to C<1_000,000> bytes.
196  
197 =item * C<max_time>
198  
199 The maximum number of seconds to wait before triggering a flush. Defaults
200 to C<0> seconds, which means that it is disabled. B<Note:> This timeout
201 is only triggered when new items are added to the queue, not in the background.
202  
203 =back
204  
205 =head2 Errors when flushing
206  
207 There are two types of error which can be thrown when L</flush()>
208 is called, either manually or automatically.
209  
210 =over
211  
212 =item * Temporary Elasticsearch errors
213  
214 A C<Cxn> error like a C<NoNodes> error which indicates that your cluster is down.
215 These errors do not clear the buffer, as they can be retried later on.
216  
217 =item * Action errors
218  
219 Individual actions may fail. For instance, a C<create> action will fail
220 if a document with the same C<index>, C<type> and C<id> already exists.
221 These action errors are reported via L<callbacks|/Using callbacks>.
222  
223 =back
224  
225 =head2 Using callbacks
226  
227 By default, any I<Action errors> (see above) cause warnings to be
228 written to C<STDERR>. However, you can use the C<on_error>, C<on_conflict>
229 and C<on_success> callbacks for more fine-grained control.
230  
231 All callbacks receive the following arguments:
232  
233 =over
234  
235 =item C<$action>
236  
237 The name of the action, ie C<index>, C<create>, C<update> or C<delete>.
238  
239 =item C<$response>
240  
241 The response that Elasticsearch returned for this action.
242  
243 =item C<$i>
244  
245 The index of the action, ie the first action in the flush request
246 will have C<$i> set to C<0>, the second will have C<$i> set to C<1> etc.
247  
248 =back
249  
250 =head3 C<on_success>
251  
252 my $bulk = $es->bulk_helper(
253 on_success => sub {
254 my ($action,$response,$i) = @_;
255 # do something
256 },
257 );
258  
259 The C<on_success> callback is called for every action that has a successful
260 response.
261  
262 =head3 C<on_conflict>
263  
264 my $bulk = $es->bulk_helper(
265 on_conflict => sub {
266 my ($action,$response,$i,$version) = @_;
267 # do something
268 },
269 );
270  
271 The C<on_conflict> callback is called for actions that have triggered
272 a C<Conflict> error, eg trying to C<create> a document which already
273 exists. The C<$version> argument will contain the version number
274 of the document currently stored in Elasticsearch (if found).
275  
276 =head3 C<on_error>
277  
278 my $bulk = $es->bulk_helper(
279 on_error => sub {
280 my ($action,$response,$i) = @_;
281 # do something
282 },
283 );
284  
285 The C<on_error> callback is called for any error (unless the C<on_conflict>)
286 callback has already been called).
287  
288 =head2 Disabling callbacks and autoflush
289  
290 If you want to be in control of flushing, and you just want to receive
291 the raw response that Elasticsearch sends instead of using callbacks,
292 then you can do so as follows:
293  
294 my $bulk = $es->bulk_helper(
295 max_count => 0,
296 max_size => 0,
297 on_error => undef
298 );
299  
300 $bulk->add_actions(....);
301 $response = $bulk->flush;
302  
303 =head1 CREATE, INDEX, UPDATE, DELETE
304  
305 =head2 C<add_action()>
306  
307 $bulk->add_action(
308 create => { ...params... },
309 index => { ...params... },
310 update => { ...params... },
311 delete => { ...params... }
312 );
313  
314 The C<add_action()> method allows you to add multiple C<create>, C<index>,
315 C<update> and C<delete> actions to the queue. The first value is the action
316 type, and the second value is the parameters that describe that action.
317 See the individual helper methods below for details.
318  
319 B<Note:> Parameters like C<index> or C<type> can be specified as C<index> or as
320 C<_index>, so the following two lines are equivalent:
321  
322 index => { index => 'index', type => 'type', id => 1, source => {...}},
323 index => { _index => 'index', _type => 'type', _id => 1, source => {...}},
324  
325 B<Note:> The C<index> and C<type> parameters can be specified in the
326 params for any action, but if not specified, will default to the C<index>
327 and C<type> values specified in L</new()>. These are required parameters:
328 they must be specified either in L</new()> or in every action.
329  
330 =head2 C<create()>
331  
332 $bulk->create(
333 { index => 'custom_index', source => { doc body }},
334 { type => 'custom_type', id => 1, source => { doc body }},
335 ...
336 );
337  
338 The C<create()> helper method allows you to add multiple C<create> actions.
339 It accepts the same parameters as L<Search::Elasticsearch::Client::5_0::Direct/create()>
340 except that the document body should be passed as the C<source> or C<_source>
341 parameter, instead of as C<body>.
342  
343 =head2 C<create_docs()>
344  
345 $bulk->create_docs(
346 { doc body },
347 { doc body },
348 ...
349 );
350  
351 The C<create_docs()> helper is a shorter form of L</create()> which can be used
352 when you are using the default C<index> and C<type> as set in L</new()>
353 and you are not specifying a custom C<id> per document. In this case,
354 you can just pass the individual document bodies.
355  
356 =head2 C<index()>
357  
358 $bulk->index(
359 { index => 'custom_index', source => { doc body }},
360 { type => 'custom_type', id => 1, source => { doc body }},
361 ...
362 );
363  
364 The C<index()> helper method allows you to add multiple C<index> actions.
365 It accepts the same parameters as L<Search::Elasticsearch::Client::5_0::Direct/index()>
366 except that the document body should be passed as the C<source> or C<_source>
367 parameter, instead of as C<body>.
368  
369 =head2 C<delete()>
370  
371 $bulk->delete(
372 { index => 'custom_index', id => 1},
373 { type => 'custom_type', id => 2},
374 ...
375 );
376  
377 The C<delete()> helper method allows you to add multiple C<delete> actions.
378 It accepts the same parameters as L<Search::Elasticsearch::Client::5_0::Direct/delete()>.
379  
380 =head2 C<delete_ids()>
381  
382 $bulk->delete_ids(1,2,3...)
383  
384 The C<delete_ids()> helper method can be used when all of the documents you
385 want to delete have the default C<index> and C<type> as set in L</new()>.
386 In this case, all you have to do is to pass in a list of IDs.
387  
388 =head2 C<update()>
389  
390 $bulk->update(
391 { id => 1,
392 doc => { partial doc },
393 doc_as_upsert => 1
394 },
395 { id => 2,
396 script => { script }
397 upsert => { upsert doc }
398 },
399 ...
400 );
401  
402 The C<update()> helper method allows you to add multiple C<update> actions.
403 It accepts the same parameters as L<Search::Elasticsearch::Client::5_0::Direct/update()>.
404 An update can either use a I<partial doc> which gets merged with an existing
405 doc (example 1 above), or can use a C<script> to update an existing doc
406 (example 2 above). More information on C<script> can be found here:
407 L<Search::Elasticsearch::Client::5_0::Direct/update()>.
408  
409 =head1 AUTHOR
410  
411 Enrico Zimuel <enrico.zimuel@elastic.co>
412  
413 =head1 COPYRIGHT AND LICENSE
414  
415 This software is Copyright (c) 2020 by Elasticsearch BV.
416  
417 This is free software, licensed under:
418  
419 The Apache License, Version 2.0, January 2004
420  
421 =cut
422  
423 __END__