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