| 1 | # Copyright 2015 - present MongoDB, Inc. | ||||||||||
| 2 | # | ||||||||||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| 4 | # you may not use this file except in compliance with the License. | ||||||||||
| 5 | # You may obtain a copy of the License at | ||||||||||
| 6 | # | ||||||||||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| 8 | # | ||||||||||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||||||||||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| 12 | # See the License for the specific language governing permissions and | ||||||||||
| 13 | # limitations under the License. | ||||||||||
| 14 | |||||||||||
| 15 | 59 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 59 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | package MongoDB::Op::_BatchInsert; | ||||||||||
| 18 | |||||||||||
| 19 | # Encapsulate a multi-document insert operation; returns a | ||||||||||
| 20 | # MongoDB::InsertManyResult | ||||||||||
| 21 | |||||||||||
| 22 | 59 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 59 | our $VERSION = 'v2.2.2'; | |||||||||
| 24 | |||||||||||
| 25 | 59 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 59 | ▶ | use MongoDB::InsertManyResult; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 59 | ▶ | use Tie::IxHash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 59 | ▶ | use MongoDB::_Types qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | Boolish | ||||||||||
| 31 | 59 | ); | |||||||||
| 32 | 59 | ▶ | use Types::Standard qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | ArrayRef | ||||||||||
| 34 | 59 | ); | |||||||||
| 35 | |||||||||||
| 36 | 59 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | # may or may not have _id; will be added if check_keys is true | ||||||||||
| 39 | 59 | has documents => ( | |||||||||
| 40 | is => 'ro', | ||||||||||
| 41 | required => 1, | ||||||||||
| 42 | isa => ArrayRef, | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 59 | has ordered => ( | |||||||||
| 46 | is => 'ro', | ||||||||||
| 47 | required => 1, | ||||||||||
| 48 | isa => Boolish, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | 59 | has check_keys => ( | |||||||||
| 52 | is => 'ro', | ||||||||||
| 53 | required => 1, | ||||||||||
| 54 | isa => Boolish, | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | # starts empty and gets initialized during operations | ||||||||||
| 58 | 59 | has _doc_ids => ( | |||||||||
| 59 | is => 'ro', | ||||||||||
| 60 | writer => '_set_doc_ids', | ||||||||||
| 61 | init_arg => undef, | ||||||||||
| 62 | isa => ArrayRef, | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | 59 | with $_ for qw( | |||||||||
| 66 | MongoDB::Role::_PrivateConstructor | ||||||||||
| 67 | MongoDB::Role::_CollectionOp | ||||||||||
| 68 | MongoDB::Role::_SingleBatchDocWrite | ||||||||||
| 69 | MongoDB::Role::_InsertPreEncoder | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 73 | 0 | my ( $self, $link ) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 0 | my $documents = $self->documents; | |||||||||
| 76 | 0 | ▶ | my $invalid_chars = $self->check_keys ? '.' : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | 0 | my (@insert_docs, @ids); | |||||||||
| 79 | |||||||||||
| 80 | 0 | my $last_idx = $#$documents; | |||||||||
| 81 | 0 | ▶ | for ( my $i = 0; $i <= $last_idx; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | push @insert_docs, $self->_pre_encode_insert( $link->max_bson_object_size, $documents->[$i], $invalid_chars ); | |||||||||
| 83 | 0 | push @ids, $insert_docs[-1]{metadata}{_id}; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | $self->_set_doc_ids(\@ids); | |||||||||
| 87 | |||||||||||
| 88 | # XXX have to check size of docs to insert and possibly split it | ||||||||||
| 89 | # | ||||||||||
| 90 | return ! $self->_should_use_acknowledged_write | ||||||||||
| 91 | ? ( | ||||||||||
| 92 | $self->_send_legacy_op_noreply( $link, | ||||||||||
| 93 | 0 | ▶ | MongoDB::_Protocol::write_insert( $self->full_name, join( "", map { $_->{bson} } @insert_docs ) ), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | \@insert_docs, | ||||||||||
| 95 | "MongoDB::UnacknowledgedResult", | ||||||||||
| 96 | "insert", | ||||||||||
| 97 | ) | ||||||||||
| 98 | ) | ||||||||||
| 99 | : $link->supports_write_commands | ||||||||||
| 100 | ? ( | ||||||||||
| 101 | $self->_send_write_command( $link, | ||||||||||
| 102 | Tie::IxHash->new( | ||||||||||
| 103 | insert => $self->coll_name, | ||||||||||
| 104 | documents => \@insert_docs, | ||||||||||
| 105 | 0 | @{ $self->write_concern->as_args }, | |||||||||
| 106 | ), | ||||||||||
| 107 | undef, | ||||||||||
| 108 | "MongoDB::InsertManyResult", | ||||||||||
| 109 | )->assert | ||||||||||
| 110 | ) | ||||||||||
| 111 | : ( | ||||||||||
| 112 | $self->_send_legacy_op_with_gle( $link, | ||||||||||
| 113 | 0 | ▶ | MongoDB::_Protocol::write_insert( $self->full_name, join( "", map { $_->{bson} } @insert_docs ) ), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | \@insert_docs, | ||||||||||
| 115 | "MongoDB::InsertManyResult", | ||||||||||
| 116 | "insert", | ||||||||||
| 117 | )->assert | ||||||||||
| 118 | ); | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub _parse_cmd { | ||||||||
|
Subroutine
_parse_cmd: not called CC=2 SCAR=17.9
| |||||||||||
| 122 | 0 | my ( $self, $res ) = @_; | |||||||||
| 123 | 0 | ▶ | return unless $res->{ok}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | my $inserted = $self->_doc_ids; | |||||||||
| 125 | 0 | my $ids = [ map +{ index => $_, _id => $inserted->[$_] }, 0 .. $#{$inserted} ]; | |||||||||
| 126 | 0 | return ( inserted_count => scalar @$inserted, inserted => $ids ); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 59 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 130 | 59 | ▶ | no warnings 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 131 | 59 | *_parse_gle = \&_parse_cmd; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 59 | 1; | |||||||||