File Coverage

blib/lib/MongoDB/Op/_BatchInsert.pm
Criterion Covered Total %
statement 31 50 62.0
branch 0 8 0.0
condition n/a
subroutine 11 13 84.6
pod 0 1 0.0
total 42 72 58.3


line stmt bran cond sub pod time code
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 58     58   397 use strict;
  58         181  
  58         1966  
16 58     58   322 use warnings;
  58         128  
  58         2038  
17             package MongoDB::Op::_BatchInsert;
18              
19             # Encapsulate a multi-document insert operation; returns a
20             # MongoDB::InsertManyResult
21              
22 58     58   363 use version;
  58         130  
  58         367  
23             our $VERSION = 'v2.2.0';
24              
25 58     58   4500 use Moo;
  58         141  
  58         368  
26              
27 58     58   40636 use MongoDB::InsertManyResult;
  58         182  
  58         2036  
28 58     58   467 use Tie::IxHash;
  58         174  
  58         1569  
29 58         333 use MongoDB::_Types qw(
30             Boolish
31 58     58   315 );
  58         129  
32 58         418 use Types::Standard qw(
33             ArrayRef
34 58     58   33651 );
  58         149  
35              
36 58     58   43566 use namespace::clean;
  58         159  
  58         316  
37              
38             # may or may not have _id; will be added if check_keys is true
39             has documents => (
40             is => 'ro',
41             required => 1,
42             isa => ArrayRef,
43             );
44              
45             has ordered => (
46             is => 'ro',
47             required => 1,
48             isa => Boolish,
49             );
50              
51             has check_keys => (
52             is => 'ro',
53             required => 1,
54             isa => Boolish,
55             );
56              
57             # starts empty and gets initialized during operations
58             has _doc_ids => (
59             is => 'ro',
60             writer => '_set_doc_ids',
61             init_arg => undef,
62             isa => ArrayRef,
63             );
64              
65             with $_ for qw(
66             MongoDB::Role::_PrivateConstructor
67             MongoDB::Role::_CollectionOp
68             MongoDB::Role::_SingleBatchDocWrite
69             MongoDB::Role::_InsertPreEncoder
70             );
71              
72             sub execute {
73 0     0 0   my ( $self, $link ) = @_;
74              
75 0           my $documents = $self->documents;
76 0 0         my $invalid_chars = $self->check_keys ? '.' : '';
77              
78 0           my (@insert_docs, @ids);
79              
80 0           my $last_idx = $#$documents;
81 0           for ( my $i = 0; $i <= $last_idx; $i++ ) {
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 ) ),
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 0         MongoDB::_Protocol::write_insert( $self->full_name, join( "", map { $_->{bson} } @insert_docs ) ),
  0 0          
114             \@insert_docs,
115             "MongoDB::InsertManyResult",
116             "insert",
117             )->assert
118             );
119             }
120              
121             sub _parse_cmd {
122 0     0     my ( $self, $res ) = @_;
123 0 0         return unless $res->{ok};
124 0           my $inserted = $self->_doc_ids;
125 0           my $ids = [ map +{ index => $_, _id => $inserted->[$_] }, 0 .. $#{$inserted} ];
  0            
126 0           return ( inserted_count => scalar @$inserted, inserted => $ids );
127             }
128              
129             BEGIN {
130 58     58   39291 no warnings 'once';
  58         155  
  58         2452  
131 58     58   2069 *_parse_gle = \&_parse_cmd;
132             }
133              
134             1;