5_0/Role/Bulk.pm untested

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