5_0/Bulk.pm untested

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