| 1 | # OpenSearch::Client is an unofficial client for OpenSearch. | ||
| 2 | # It is derived from Search::Elasticsearch version 7.714 | ||
| 3 | # License details from the original work are contained in the | ||
| 4 | # NOTICE file distributed with this work. | ||
| 5 | # | ||
| 6 | #----------------------------------------------------------------------- | ||
| 7 | # OpenSearch::Client | ||
| 8 | #----------------------------------------------------------------------- | ||
| 9 | # Copyright 2026 Mark Dootson | ||
| 10 | # | ||
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 12 | # you may not use this file except in compliance with the License. | ||
| 13 | # You may obtain a copy of the License at | ||
| 14 | # | ||
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 16 | # | ||
| 17 | # Unless required by applicable law or agreed to in writing, software | ||
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 20 | # See the License for the specific language governing permissions and | ||
| 21 | # limitations under the License. | ||
| 22 | |||
| 23 | package OpenSearch::Client::Core::3_0::Helper::Bulk; | ||
| 24 | $OpenSearch::Client::Core::3_0::Helper::Bulk::VERSION = '3.007011'; | ||
| 25 | use Moo; | ||
| 26 | with 'OpenSearch::Client::Core::3_0::Role::Helper::Bulk', | ||
| 27 | 'OpenSearch::Client::Role::Is_Sync'; | ||
| 28 | use OpenSearch::Client::Util qw(parse_params throw); | ||
| 29 | use Try::Tiny; | ||
| 30 | use namespace::clean; | ||
| 31 | |||
| 32 | #=================================== | ||
| 33 | sub add_action { | ||
| 34 | #=================================== | ||
| 35 | my $self = shift; | ||
| 36 | my $buffer = $self->_buffer; | ||
| 37 | my $max_size = $self->max_size; | ||
| 38 | my $max_count = $self->max_count; | ||
| 39 | my $max_time = $self->max_time; | ||
| 40 | |||
| 41 | while (@_) { | ||
| 42 | my @json = $self->_encode_action( splice( @_, 0, 2 ) ); | ||
| 43 | |||
| 44 | push @$buffer, @json; | ||
| 45 | |||
| 46 | my $size = $self->_buffer_size; | ||
| 47 | $size += length($_) + 1 for @json; | ||
| 48 | $self->_buffer_size($size); | ||
| 49 | |||
| 50 | my $count = $self->_buffer_count( $self->_buffer_count + 1 ); | ||
| 51 | |||
| 52 | $self->flush | ||
| 53 | if ( $max_size and $size >= $max_size ) | ||
| 54 | || ( $max_count and $count >= $max_count ) | ||
| 55 | || ( $max_time and time >= $self->_last_flush + $max_time ); | ||
| 56 | } | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | #=================================== | ||
| 61 | sub flush { | ||
| 62 | #=================================== | ||
| 63 | my $self = shift; | ||
| 64 | $self->_last_flush(time); | ||
| 65 | |||
| 66 | return { items => [] } | ||
| 67 | unless $self->_buffer_size; | ||
| 68 | |||
| 69 | if ( $self->verbose ) { | ||
| 70 | local $| = 1; | ||
| 71 | print "."; | ||
| 72 | } | ||
| 73 | my $buffer = $self->_buffer; | ||
| 74 | my $results = try { | ||
| 75 | my $res = $self->os->bulk( %{ $self->_bulk_args }, body => $buffer ); | ||
| 76 | $self->clear_buffer; | ||
| 77 | return $res; | ||
| 78 | } | ||
| 79 | catch { | ||
| 80 | my $error = $_; | ||
| 81 | $self->clear_buffer | ||
| 82 | unless $error->is( 'Cxn', 'NoNodes' ); | ||
| 83 | |||
| 84 | die $error; | ||
| 85 | }; | ||
| 86 | $self->_report( $buffer, $results ); | ||
| 87 | return defined wantarray ? $results : undef; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |