| 1 | package Elastic::Model::Bulk; | ||
| 2 | $Elastic::Model::Bulk::VERSION = '0.52'; | ||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | use Data::Dumper; | ||
| 6 | our $Conflict = qr/ | ||
| 7 | DocumentAlreadyExistsException | ||
| 8 | | :.version.conflict,.current.\[(\d+)\] | ||
| 9 | /x; | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | |||
| 13 | #=================================== | ||
| 14 | has 'on_success' => ( | ||
| 15 | #=================================== | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'CodeRef', | ||
| 18 | ); | ||
| 19 | |||
| 20 | #=================================== | ||
| 21 | has 'on_conflict' => ( | ||
| 22 | #=================================== | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'CodeRef', | ||
| 25 | ); | ||
| 26 | |||
| 27 | #=================================== | ||
| 28 | has 'on_error' => ( | ||
| 29 | #=================================== | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'CodeRef', | ||
| 32 | ); | ||
| 33 | |||
| 34 | #=================================== | ||
| 35 | has 'size' => ( | ||
| 36 | #=================================== | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Int', | ||
| 39 | default => sub {1000} | ||
| 40 | ); | ||
| 41 | |||
| 42 | #=================================== | ||
| 43 | has '_actions' => ( | ||
| 44 | #=================================== | ||
| 45 | is => 'ro', | ||
| 46 | isa => 'ArrayRef', | ||
| 47 | traits => ['Array'], | ||
| 48 | writer => '_set_actions', | ||
| 49 | default => sub { [] }, | ||
| 50 | handles => { _push_action => 'push' } | ||
| 51 | ); | ||
| 52 | |||
| 53 | #=================================== | ||
| 54 | has '_docs' => ( | ||
| 55 | #=================================== | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'ArrayRef', | ||
| 58 | traits => ['Array'], | ||
| 59 | writer => '_set_docs', | ||
| 60 | default => sub { [] }, | ||
| 61 | handles => { | ||
| 62 | _push_doc => 'push', | ||
| 63 | count => 'count', | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | no Moose; | ||
| 68 | |||
| 69 | #=================================== | ||
| 70 | sub save { | ||
| 71 | #=================================== | ||
| 72 | my $self = shift; | ||
| 73 | my $doc = shift; | ||
| 74 | |||
| 75 | return if $doc->_can_inflate; | ||
| 76 | |||
| 77 | my $meta = Class::MOP::class_of($doc); | ||
| 78 | die "Cannot bulk index class (" | ||
| 79 | . $doc->original_class | ||
| 80 | . ") because it contains unique keys" | ||
| 81 | if $meta->unique_keys; | ||
| 82 | |||
| 83 | $doc->touch; | ||
| 84 | |||
| 85 | my %args = @_; | ||
| 86 | my $uid = $doc->uid; | ||
| 87 | |||
| 88 | croak "Cannot save partial doc type (" | ||
| 89 | . $uid->type | ||
| 90 | . ") id (" | ||
| 91 | . $uid->id . ")" | ||
| 92 | if $uid->is_partial; | ||
| 93 | |||
| 94 | my $data = $self->model->deflate_object($doc); | ||
| 95 | my $version = delete $args{version}; | ||
| 96 | |||
| 97 | my $action | ||
| 98 | = ( $uid->from_store or $uid->id and defined $version ) | ||
| 99 | ? 'index' | ||
| 100 | : 'create'; | ||
| 101 | |||
| 102 | %args = ( %args, %{ $uid->write_params } ); | ||
| 103 | $args{version} = $version | ||
| 104 | if defined $version; | ||
| 105 | |||
| 106 | for ( keys %args ) { | ||
| 107 | $args{"_$_"} = delete $args{$_}; | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->_push_action( { $action => {%args} }, $data ); | ||
| 111 | $self->_push_doc($doc); | ||
| 112 | $self->commit if $self->count >= $self->size; | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | #=================================== | ||
| 117 | sub overwrite { | ||
| 118 | #=================================== | ||
| 119 | my $self = shift; | ||
| 120 | $self->save( @_, version => 0 ); | ||
| 121 | } | ||
| 122 | |||
| 123 | #=================================== | ||
| 124 | sub commit { | ||
| 125 | #=================================== | ||
| 126 | my $self = shift; | ||
| 127 | return unless $self->count; | ||
| 128 | |||
| 129 | my $actions = $self->_actions; | ||
| 130 | my $docs = $self->_docs; | ||
| 131 | my $on_success = $self->on_success; | ||
| 132 | my $on_conflict = $self->on_conflict; | ||
| 133 | my $on_error = $self->on_error; | ||
| 134 | |||
| 135 | $self->clear; | ||
| 136 | |||
| 137 | my $response = $self->model->store->bulk( body => $actions ); | ||
| 138 | my $results = $response->{items}; | ||
| 139 | my $model = $self->model; | ||
| 140 | my $scope = $model->current_scope; | ||
| 141 | |||
| 142 | my @unhandled; | ||
| 143 | |||
| 144 | my $i = 0; | ||
| 145 | |||
| 146 | for my $item (@$results) { | ||
| 147 | my ( $action, $result ) = %$item; | ||
| 148 | |||
| 149 | my $doc = $docs->[ $i++ ]; | ||
| 150 | |||
| 151 | if ( my $error = $result->{error} ) { | ||
| 152 | if ( $on_conflict and $error =~ /$Conflict/ ) { | ||
| 153 | my $uid | ||
| 154 | = $1 | ||
| 155 | ? Elastic::Model::UID->new( %{ $doc->uid->read_params }, | ||
| 156 | version => $1 ) | ||
| 157 | : $doc->uid->clone; | ||
| 158 | my $new = $self->model->get_doc( uid => $uid ); | ||
| 159 | $on_conflict->( $doc, $new ); | ||
| 160 | } | ||
| 161 | elsif ($on_error) { | ||
| 162 | $on_error->( $doc, $error ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | push @unhandled, $result; | ||
| 166 | } | ||
| 167 | next; | ||
| 168 | } | ||
| 169 | |||
| 170 | my $uid = $doc->uid; | ||
| 171 | $uid->update_from_store($result); | ||
| 172 | $doc->_set_source( $result->{data} ); | ||
| 173 | if ($scope) { | ||
| 174 | my $ns = $model->namespace_for_domain( $result->{_index} ); | ||
| 175 | $scope->store_object( $ns->name, $doc ); | ||
| 176 | } | ||
| 177 | $on_success->($doc) if $on_success; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (@unhandled) { | ||
| 181 | local $Data::Dumper::Terse = 1; | ||
| 182 | local $Data::Dumper::Indent = 1; | ||
| 183 | |||
| 184 | my @errors = splice @unhandled, 0, 2; | ||
| 185 | die "Uncaught errors while commiting Bulk:" | ||
| 186 | . Dumper( \@errors ) | ||
| 187 | . ( @unhandled ? "\nand " . ( 0 + @unhandled ) . " more" : '' ); | ||
| 188 | } | ||
| 189 | return 1; | ||
| 190 | |||
| 191 | } | ||
| 192 | |||
| 193 | #=================================== | ||
| 194 | sub clear { | ||
| 195 | #=================================== | ||
| 196 | my $self = shift; | ||
| 197 | $self->_set_actions( [] ); | ||
| 198 | $self->_set_docs( [] ); | ||
| 199 | } | ||
| 200 | |||
| 201 | #=================================== | ||
| 202 | sub DEMOLISH { shift->commit } | ||
| 203 | #=================================== | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | =pod | ||
| 208 | |||
| 209 | =encoding UTF-8 | ||
| 210 | |||
| 211 | =head1 NAME | ||
| 212 | |||
| 213 | Elastic::Model::Bulk - Bulk-saving of multiple docs for increased throughput | ||
| 214 | |||
| 215 | =head1 VERSION | ||
| 216 | |||
| 217 | version 0.52 | ||
| 218 | |||
| 219 | =head1 SYNOPSIS | ||
| 220 | |||
| 221 | $bulk = $model->bulk( | ||
| 222 | size => 1000, | ||
| 223 | on_conflict => sub {...}, | ||
| 224 | on_error => sub {...} | ||
| 225 | ); | ||
| 226 | |||
| 227 | $bulk->save($doc); | ||
| 228 | $bulk->overwrite($doc); | ||
| 229 | ... | ||
| 230 | |||
| 231 | $bulk->commit; | ||
| 232 | |||
| 233 | =head1 DESCRIPTION | ||
| 234 | |||
| 235 | If you need to create or update multiple docs at once, then bulk indexing is | ||
| 236 | the way to go. It batches up the documents and saves C<size> (default 1000) | ||
| 237 | documents in a single request, which is much faster than writing each | ||
| 238 | doc individually. | ||
| 239 | |||
| 240 | Once you are finished adding docs to the C<$bulk> indexer, call L</commit()> | ||
| 241 | to save any docs that haven't been saved yet. If C<$bulk> goes out of scope, | ||
| 242 | then L</commit()> will be called for you, but it is safer to call it yourself. | ||
| 243 | |||
| 244 | B<Note:> Bulk indexing is not supported for classes which have | ||
| 245 | L<unique key constraints|Elastic::Manual::Attributes/unique_key>. | ||
| 246 | |||
| 247 | =head1 ATTRIBUTES | ||
| 248 | |||
| 249 | =head2 size | ||
| 250 | |||
| 251 | The number of docs that will be saved in a single request. Defaults to 1000. | ||
| 252 | |||
| 253 | =head2 on_success | ||
| 254 | |||
| 255 | An optional callback which will be called when a document has been | ||
| 256 | indexed successfully. It is called with a single argument: the current | ||
| 257 | document. | ||
| 258 | |||
| 259 | =head2 on_conflict | ||
| 260 | |||
| 261 | A callback which will be called if there is any conflict when saving a doc, for | ||
| 262 | instance, trying to create a doc that already exists, or trying to save a doc | ||
| 263 | when a newer version already exists in Elasticsearch. | ||
| 264 | |||
| 265 | The callback is called with two arguments: | ||
| 266 | |||
| 267 | =over | ||
| 268 | |||
| 269 | =item * | ||
| 270 | |||
| 271 | The doc you are trying to save | ||
| 272 | |||
| 273 | =item * | ||
| 274 | |||
| 275 | The current version of the doc which exists in Elasticsearch | ||
| 276 | |||
| 277 | =back | ||
| 278 | |||
| 279 | See L<Elastic::Model::Role::Doc/save()> for more. | ||
| 280 | |||
| 281 | =head2 on_error | ||
| 282 | |||
| 283 | The C<on_error> callback will be called for any non-conflict error (or | ||
| 284 | for conflict errors if no L</on_conflict> handler has been specified). | ||
| 285 | It is called with two arguments: | ||
| 286 | |||
| 287 | =over | ||
| 288 | |||
| 289 | =item * | ||
| 290 | |||
| 291 | The doc you are trying to save | ||
| 292 | |||
| 293 | =item * | ||
| 294 | |||
| 295 | The error string returned by Elasticsearch | ||
| 296 | |||
| 297 | =back | ||
| 298 | |||
| 299 | If no C<on_error> handler is specified, then bulk indexing will die with | ||
| 300 | an error message. | ||
| 301 | |||
| 302 | =head1 METHODS | ||
| 303 | |||
| 304 | =head2 save() | ||
| 305 | |||
| 306 | $bulk->save($doc); | ||
| 307 | |||
| 308 | Adds a doc to the internal queue to be saved later. | ||
| 309 | |||
| 310 | =head2 overwrite() | ||
| 311 | |||
| 312 | $bulk->overwrite($doc); | ||
| 313 | |||
| 314 | Adds a doc to the interal queue to be overwritten later. In other words, | ||
| 315 | no version checking is done - if a newer version of the doc exists in | ||
| 316 | Elasticsearch, it will be overwritten. | ||
| 317 | |||
| 318 | =head2 commit() | ||
| 319 | |||
| 320 | $bulk->commit() | ||
| 321 | |||
| 322 | Writes all docs in the queue to Elasticsearch. This is called automatically | ||
| 323 | when there are L</size> docs in the queue, or when the C<$bulk> instance | ||
| 324 | goes out of scope, although you should call L</commit()> yourself once | ||
| 325 | you are finished adding docs, just to be on the safe side. | ||
| 326 | |||
| 327 | =head2 clear() | ||
| 328 | |||
| 329 | $bulk->clear() | ||
| 330 | |||
| 331 | Clears any docs that are still in the queue. | ||
| 332 | |||
| 333 | =head1 AUTHOR | ||
| 334 | |||
| 335 | Clinton Gormley <drtech@cpan.org> | ||
| 336 | |||
| 337 | =head1 COPYRIGHT AND LICENSE | ||
| 338 | |||
| 339 | This software is copyright (c) 2015 by Clinton Gormley. | ||
| 340 | |||
| 341 | This is free software; you can redistribute it and/or modify it under | ||
| 342 | the same terms as the Perl 5 programming language system itself. | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | __END__ |