| 1 | # | ||||||||||||||
| 2 | # This file is part of ElasticSearchX-Model | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # This software is Copyright (c) 2018 by Moritz Onken. | ||||||||||||||
| 5 | # | ||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||
| 7 | # | ||||||||||||||
| 8 | # The (three-clause) BSD License | ||||||||||||||
| 9 | # | ||||||||||||||
| 10 | package ElasticSearchX::Model::Bulk; | ||||||||||||||
| 11 | 13 | $ElasticSearchX::Model::Bulk::VERSION = '2.0.0'; | |||||||||||||
| 12 | 13 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 13 | ▶ | use ElasticSearchX::Model::Document::Types qw(ESBulk); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 13 | has stash => ( | |||||||||||||
| 17 | is => 'ro', | ||||||||||||||
| 18 | isa => ESBulk, | ||||||||||||||
| 19 | handles => { stash_size => '_buffer_count', commit => "flush" }, | ||||||||||||||
| 20 | lazy_build => 1, | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | 13 | has size => ( is => 'ro', isa => 'Int', default => 100 ); | |||||||||||||
| 23 | 13 | has es => ( is => 'ro' ); | |||||||||||||
| 24 | |||||||||||||||
| 25 | sub _build_stash { | ||||||||||||||
| 26 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_stash: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 1 | $self->es->bulk_helper( max_count => $self->size ); | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | sub add { | ||||||||||||||
| 31 | 1 | ▶ | my ( $self, $action, $payload ) = ( shift, %{ $_[0] } ); | ||||||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 32 | 1 | $payload->{source} = delete $payload->{body}; | |||||||||||||
| 33 | 1 | $self->stash->add_action( $action => $payload ); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub update { | ||||||||||||||
| 37 | 0 | ▶ | my ( $self, $doc, $qs ) = @_; | ||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 38 | 0 | ▶ | $self->add( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | { | ||||||||||||||
| 40 | index => ref $doc eq 'HASH' | ||||||||||||||
| 41 | ? $doc | ||||||||||||||
| 42 | : { $doc->_put( $doc->_update($qs) ) } | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | ); | ||||||||||||||
| 45 | 0 | return $self; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | sub create { | ||||||||||||||
| 49 | 0 | ▶ | my ( $self, $doc, $qs ) = @_; | ||||||||||||
|
Subroutine
create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 50 | 0 | ▶ | $self->add( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | { create => ref $doc eq 'HASH' ? $doc : { $doc->_put($qs) } } ); | ||||||||||||||
| 52 | 0 | return $self; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | sub put { | ||||||||||||||
| 56 | 1 | ▶ | my ( $self, $doc, $qs ) = @_; | ||||||||||||
|
Subroutine
put: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||
| 57 | $self->add( | ||||||||||||||
| 58 | { | ||||||||||||||
| 59 | index => ref $doc eq 'HASH' | ||||||||||||||
| 60 | ? $doc | ||||||||||||||
| 61 | 1 | ▶ | : { $doc->_put, %{ $qs || {} } } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | ); | ||||||||||||||
| 64 | 0 | return $self; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | sub delete { | ||||||||||||||
| 68 | 0 | ▶ | my ( $self, $doc, $qs ) = @_; | ||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 69 | 0 | ▶ | $self->add( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | { | ||||||||||||||
| 71 | delete => ref $doc eq 'HASH' | ||||||||||||||
| 72 | ? $doc | ||||||||||||||
| 73 | : { | ||||||||||||||
| 74 | index => $doc->index->name, | ||||||||||||||
| 75 | type => $doc->meta->short_name, | ||||||||||||||
| 76 | id => $doc->_id, | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | ); | ||||||||||||||
| 80 | 0 | return $self; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | sub clear { | ||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 85 | 0 | $self->stash->clear_buffer; | |||||||||||||
| 86 | 0 | return $self; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | sub DEMOLISH { | ||||||||||||||
| 90 | 1 | ▶ | my ($self, $in_gd) = @_; | ||||||||||||
|
Subroutine
DEMOLISH: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||
| 91 | 1 | ▶ | return if $in_gd; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 1 | ▶ | $self->commit if $self->has_stash; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 13 | 1; | |||||||||||||
| 96 | |||||||||||||||
| 97 | __END__ | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =pod | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =encoding UTF-8 | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head1 NAME | ||||||||||||||
| 104 | |||||||||||||||
| 105 | ElasticSearchX::Model::Bulk | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 VERSION | ||||||||||||||
| 108 | |||||||||||||||
| 109 | version 2.0.0 | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 SYNOPSIS | ||||||||||||||
| 112 | |||||||||||||||
| 113 | my $bulk = $model->bulk( size => 10 ); | ||||||||||||||
| 114 | my $document = $model->index('default')->type('tweet')->new_document({ | ||||||||||||||
| 115 | message => 'Hello there!', | ||||||||||||||
| 116 | date => DateTime->now, | ||||||||||||||
| 117 | }); | ||||||||||||||
| 118 | $bulk->put( $document ); | ||||||||||||||
| 119 | $bulk->commit; | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||||||
| 122 | |||||||||||||||
| 123 | This class is a wrapper around L<Search::Elasticsearch>'s bulk helper which adds | ||||||||||||||
| 124 | some convenience. By specifiying a L</size> you set the maximum | ||||||||||||||
| 125 | number of documents that are processed in one request. You can either | ||||||||||||||
| 126 | L</put> or L</delete> documents. Once the C<$bulk> object is out | ||||||||||||||
| 127 | of scope, it will automatically commit its L</stash>. Call L</clear> | ||||||||||||||
| 128 | if before if you don't want that to happen. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =head1 ATTRIBUTES | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =head2 size | ||||||||||||||
| 133 | |||||||||||||||
| 134 | The maximum number of documents that are processed in one request. | ||||||||||||||
| 135 | Once the stash hits that number, a bulk request will be issued | ||||||||||||||
| 136 | automatically and the stash will be cleared. | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head2 stash | ||||||||||||||
| 139 | |||||||||||||||
| 140 | The stash includes the documents that will be processed at the | ||||||||||||||
| 141 | next commit. A commit is either automatically issued if the size | ||||||||||||||
| 142 | of the stash is greater then L</size>, if the C<$bulk> object | ||||||||||||||
| 143 | gets out of scope or if you call L</commit> explicitly. | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head2 es | ||||||||||||||
| 146 | |||||||||||||||
| 147 | The L<Search::Elasticsearch> object. | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =head1 METHODS | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =head2 create | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =head2 update | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head2 put( $doc ) | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =head2 put( $doc, { %qs } ) | ||||||||||||||
| 158 | |||||||||||||||
| 159 | Put a document. Accepts a document object (see | ||||||||||||||
| 160 | L<ElasticSearchX::Model::Document::Set/new_document>) or a | ||||||||||||||
| 161 | HashRef for better performance. | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =head2 delete | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Delete a document. You can either pass a document object or a | ||||||||||||||
| 166 | HashRef that consists of C<index>, C<type> and C<id>. | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head2 commit | ||||||||||||||
| 169 | |||||||||||||||
| 170 | Commits the documents in the stash to ElasticSearch. | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head2 clear | ||||||||||||||
| 173 | |||||||||||||||
| 174 | Clears the stash. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head2 stash_size | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Returns the number of documents in the stash. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =head1 AUTHOR | ||||||||||||||
| 181 | |||||||||||||||
| 182 | Moritz Onken | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 185 | |||||||||||||||
| 186 | This software is Copyright (c) 2018 by Moritz Onken. | ||||||||||||||
| 187 | |||||||||||||||
| 188 | This is free software, licensed under: | ||||||||||||||
| 189 | |||||||||||||||
| 190 | The (three-clause) BSD License | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =cut | ||||||||||||||