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 59     59   398 use strict;
  59         140  
  59         1674  
16 59     59   303 use warnings;
  59         149  
  59         1904  
17             package MongoDB::Op::_BatchInsert;
18              
19             # Encapsulate a multi-document insert operation; returns a
20             # MongoDB::InsertManyResult
21              
22 59     59   329 use version;
  59         133  
  59         310  
23             our $VERSION = 'v2.2.1';
24              
25 59     59   4507 use Moo;
  59         168  
  59         399  
26              
27 59     59   42003 use MongoDB::InsertManyResult;
  59         201  
  59         1965  
28 59     59   474 use Tie::IxHash;
  59         140  
  59         1570  
29 59         341 use MongoDB::_Types qw(
30             Boolish
31 59     59   334 );
  59         140  
32 59         387 use Types::Standard qw(
33             ArrayRef
34 59     59   33825 );
  59         144  
35              
36 59     59   44410 use namespace::clean;
  59         149  
  59         293  
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 59     59   39330 no warnings 'once';
  59         158  
  59         2564  
131 59     59   2030 *_parse_gle = \&_parse_cmd;
132             }
133              
134             1;