6_0/Bulk.pm untested

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