MongoDB/Role/_SingleBatchDocWrite.pm

filter: Statementstmt 33.9% 37 / 109 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 78.5% 11 / 14 Podpod n/a totaltotal 21.8% 48 / 220 CC 39 SCAR 55.6
CC
39
Cov
19%
_send_legacy_op_with_gle
462.0
_send_write_command
240.0
_send_legacy_op_noreply
30.0
CRAP 258.9
« MongoDB/Op/_ChangeStream.pm ↑ Summary MongoDB/Op/_BulkWrite.pm »
1 # Copyright 2016 - 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::Role::_SingleBatchDocWrite;
18  
19 # MongoDB interface for database insert/update/delete operations
20  
21 59 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 our $VERSION = 'v2.2.2';
23  
24 59 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 ▶use MongoDB::CommandResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 59 ▶use MongoDB::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 59 ▶use MongoDB::UnacknowledgedResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 59 ▶use MongoDB::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 59 ▶use MongoDB::_Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 59 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
32 WriteConcern
33 to_IxHash
34 59 );
35  
36 59 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 59 with $_ for qw(
39 MongoDB::Role::_WriteOp
40 MongoDB::Role::_SessionSupport
41 MongoDB::Role::_CommandMonitoring
42 );
43  
44 59 requires qw/db_name write_concern _parse_cmd _parse_gle/;
45  
46 0 ▶sub _send_legacy_op_with_gle {
Subroutine _send_legacy_op_with_gle: not called CC=21 SCAR=61.4
47 0 my ( $self, $link, $op_bson, $request_id, $op_doc, $result_class, $cmd_name ) = @_;
48  
49 0 my $wc_args = $self->write_concern->as_args();
50 0 ▶ my @write_concern = scalar @$wc_args ? %{ $wc_args->[1] } : ();
Branch: 1 branch
BranchTrueFalseTotal
scalar @$wc_args ? : 0 0 0
51  
52 0 my $gle = $self->bson_codec->encode_one( [ getlasterror => 1, @write_concern ] );
53 0 my ( $gle_bson, $gle_request_id ) =
54 MongoDB::_Protocol::write_query( $self->db_name . '.$cmd', $gle, undef, 0, -1 );
55  
56 # write op sent as a unit with GLE command to ensure GLE applies to the
57 # operation without other operations in between
58 0 $op_bson .= $gle_bson;
59  
60 0 ▶ if ( length($op_bson) > MAX_BSON_WIRE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $op_bson > 16793600) 0 0 0
61 # XXX should this become public?
62 0 MongoDB::_CommandSizeError->throw(
63 message => "database command too large",
64 size => length $op_bson,
65 );
66 }
67  
68 0 ▶ $self->publish_legacy_write_started( $link, $cmd_name, $op_doc, $request_id )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
69 if $self->monitoring_callback;
70  
71 0 my $result;
72 0 eval {
73 0 $link->write( $op_bson ),
74 ( $result = MongoDB::_Protocol::parse_reply( $link->read, $gle_request_id ) );
75 };
76 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
77 0 ▶ $self->publish_command_exception($err) if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
78 0 die $err;
79 }
80  
81 $self->publish_command_reply( $result->{docs} )
82 0 ▶ if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
83  
84 0 my $res = $self->bson_codec->decode_one( $result->{docs} );
85  
86 # errors in the command itself get handled as normal CommandResult
87 0 ▶ if ( !$res->{ok} && ( $res->{errmsg} || $res->{'$err'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}) 0 0 0
Condition0%0/6
not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}
!ll&&!rl&&r
000
$res->{"errmsg"} or $res->{"\$err"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}
not $res->{"ok"} $res->{"errmsg"} $res->{"\$err"}
Truth table0%0/4
not(A) and B or C
A= not $res->{"ok"}B= $res->{"errmsg"}C= $res->{"\$err"}
ABCresult
0XX0
1000
11X1
1011
88 0 return MongoDB::CommandResult->_new(
89 output => $res,
90 address => $link->address,
91 );
92 }
93  
94 # 'ok' false means GLE itself failed
95 # usually we shouldn't check wnote or jnote, but the Bulk API QA test says we should
96 # detect no journal or replication not enabled, so we check for special strings.
97 # These strings were checked back to MongoDB 1.8.5.
98 my $got_error =
99 ( exists( $res->{jnote} ) && $res->{jnote} =~ NO_JOURNAL_RE ) ? $res->{jnote}
100 : ( exists( $res->{wnote} ) && $res->{wnote} =~ NO_REPLICATION_RE ) ? $res->{wnote}
101 0 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $res->{'jnote'} && $res->{'jnote'} =~ /^journaling not enabled/ ? : 0 0 0
exists $res->{'wnote'} && $res->{'wnote'} =~ /^no replication has been enabled/ ? : 0 0 0
Condition0%0/6
exists $res->{"jnote"} and $res->{"jnote"} =~ /^journaling not enabled/
!ll&&!rl&&r
000
exists $res->{"wnote"} and $res->{"wnote"} =~ /^no replication has been enabled/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $res->{"jnote"} and $res->{"jnote"} =~ /^journaling not enabled/
exists $res->{"jnote"} $res->{"jnote"} =~ /^journaling not enabled/
MC/DC0%0/2
exists $res->{"wnote"} and $res->{"wnote"} =~ /^no replication has been enabled/
exists $res->{"wnote"} $res->{"wnote"} =~ /^no replication has been enabled/
Truth table0%0/3
A and B
A= exists $res->{"jnote"}B= $res->{"jnote"} =~ /^journaling not enabled/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= exists $res->{"wnote"}B= $res->{"wnote"} =~ /^no replication has been enabled/
ABresult
0X0
100
111
102  
103 0 ▶ if ($got_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_error) 0 0 0
104 0 MongoDB::DatabaseError->throw(
105 message => $got_error,
106 result => MongoDB::CommandResult->_new(
107 output => $res,
108 address => $link->address,
109 ),
110 );
111 }
112  
113 # otherwise, construct the desired result object, calling back
114 # on class-specific parser to generate additional attributes
115 0 my ( $write_concern_error, $write_error );
116 0 my $errmsg = $res->{err};
117 0 my $wtimeout = $res->{wtimeout};
118  
119 0 ▶ if ($wtimeout) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wtimeout) { } 0 0 0
elsif ($errmsg) { } 0 0 0
120 $write_concern_error = {
121 errmsg => $errmsg,
122 errInfo => { wtimeout => $wtimeout },
123 0 ▶ code => $res->{code} || WRITE_CONCERN_ERROR,
Condition0%0/2
$res->{'code'} || 64
l!l
00
MC/DC0%0/1
$res->{'code'} || 64
$res->{'code'}
Truth table0%0/2
A
A= $res->{'code'}
Aresult
11
00
124 };
125 }
126 elsif ($errmsg) {
127 $write_error = {
128 errmsg => $errmsg,
129 0 ▶ code => $res->{code} || UNKNOWN_ERROR,
Condition0%0/2
$res->{'code'} || 8
l!l
00
MC/DC0%0/1
$res->{'code'} || 8
$res->{'code'}
Truth table0%0/2
A
A= $res->{'code'}
Aresult
11
00
130 index => 0,
131 op => $op_doc,
132 };
133 }
134  
135 0 ▶ return $result_class->_new(
Branch: 2 branches
BranchTrueFalseTotal
$write_error ? : 0 0 0
$write_concern_error ? : 0 0 0
136 acknowledged => 1,
137 write_errors => ( $write_error ? [$write_error] : [] ),
138 write_concern_errors => ( $write_concern_error ? [$write_concern_error] : [] ),
139 $self->_parse_gle( $res, $op_doc ),
140 );
141 }
142  
143 0 ▶sub _send_legacy_op_noreply {
Subroutine _send_legacy_op_noreply: not called CC=5 SCAR=34.0
144 0 my ( $self, $link, $op_bson, $request_id, $op_doc, $result_class, $cmd_name) = @_;
145  
146 0 ▶ $self->publish_legacy_write_started( $link, $cmd_name, $op_doc, $request_id )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
147 if $self->monitoring_callback;
148  
149 0 eval { $link->write($op_bson) };
150 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
151 0 ▶ $self->publish_command_exception($err) if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
152 0 die $err;
153 }
154  
155 0 ▶ $self->publish_command_reply( { ok => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
156 if $self->monitoring_callback;
157  
158 0 return MongoDB::UnacknowledgedResult->_new(
159 $self->_parse_gle( {}, $op_doc ),
160 acknowledged => 0,
161 write_errors => [],
162 write_concern_errors => [],
163 );
164 }
165  
166 0 ▶sub _send_write_command {
Subroutine _send_write_command: not called CC=15 SCAR=54.8
167 0 my ( $self, $link, $cmd, $op_doc, $result_class ) = @_;
168  
169 0 $self->_apply_session_and_cluster_time( $link, \$cmd );
170  
171 0 my ( $op_bson, $request_id );
172 0 ▶ if ( $link->supports_op_msg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->supports_op_msg) { } 0 0 0
173 0 $cmd = to_IxHash( $cmd );
174 0 $cmd->Push( '$db', $self->db_name );
175 0 ( $op_bson, $request_id ) =
176 MongoDB::_Protocol::write_msg( $self->bson_codec, undef, $cmd );
177 } else {
178 # send command and get response document
179 0 my $command = $self->bson_codec->encode_one( $cmd );
180 0 ( $op_bson, $request_id ) =
181 MongoDB::_Protocol::write_query( $self->db_name . '.$cmd',
182 $command, undef, 0, -1, undef );
183 }
184  
185 0 ▶ if ( length($op_bson) > MAX_BSON_WIRE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $op_bson > 16793600) 0 0 0
186 # XXX should this become public?
187 0 MongoDB::_CommandSizeError->throw(
188 message => "database command too large",
189 size => length $op_bson,
190 );
191 }
192  
193 0 ▶ $self->publish_command_started( $link, $cmd, $request_id )
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
194 if $self->monitoring_callback;
195  
196 0 my $result;
197 0 eval {
198 0 $link->write( $op_bson ),
199 ( $result = MongoDB::_Protocol::parse_reply( $link->read, $request_id ) );
200 };
201 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
202 0 $self->_update_session_connection_error( $err );
203 0 ▶ $self->publish_command_exception($err) if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
204 0 die $err;
205 }
206  
207 $self->publish_command_reply( $result->{docs} )
208 0 ▶ if $self->monitoring_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->monitoring_callback 0 0 0
209  
210 0 my $res = $self->bson_codec->decode_one( $result->{docs} );
211  
212 0 $self->_update_session_pre_assert( $res );
213  
214 0 $self->_update_session_and_cluster_time($res);
215  
216 # Error checking depends on write concern
217 0 ▶ if ( $self->_should_use_acknowledged_write ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_should_use_acknowledged_write) { } 0 0 0
218 # errors in the command itself get handled as normal CommandResult
219 0 ▶ if ( !$res->{ok} && ( $res->{errmsg} || $res->{'$err'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}) 0 0 0
Condition0%0/6
not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}
!ll&&!rl&&r
000
$res->{"errmsg"} or $res->{"\$err"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $res->{"ok"} and $res->{"errmsg"} || $res->{"\$err"}
not $res->{"ok"} $res->{"errmsg"} $res->{"\$err"}
Truth table0%0/4
not(A) and B or C
A= not $res->{"ok"}B= $res->{"errmsg"}C= $res->{"\$err"}
ABCresult
0XX0
1000
11X1
1011
220 0 return MongoDB::CommandResult->_new(
221 output => $res,
222 address => $link->address,
223 session => $self->session,
224 );
225 }
226  
227 # if an error occurred, add the op document involved
228 0 ▶ if ( exists($res->{writeErrors}) && @{$res->{writeErrors}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{"writeErrors"} and @{$res->{"writeErrors"};}) 0 0 0
Condition0%0/3
exists $res->{"writeErrors"} and @{$res->{"writeErrors"};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $res->{"writeErrors"} and @{$res->{"writeErrors"};}
exists $res->{"writeErrors"} @{$res->{"writeErrors"};}
Truth table0%0/3
A and B
A= exists $res->{"writeErrors"}B= @{$res->{"writeErrors"};}
ABresult
0X0
100
111
229 0 $res->{writeErrors}[0]{op} = $op_doc;
230 }
231  
232 # otherwise, construct the desired result object, calling back
233 # on class-specific parser to generate additional attributes
234 my $built_result = $result_class->_new(
235 write_errors => ( $res->{writeErrors} ? $res->{writeErrors} : [] ),
236 write_concern_errors =>
237 0 ▶ ( $res->{writeConcernError} ? [ $res->{writeConcernError} ] : [] ),
Branch: 2 branches
BranchTrueFalseTotal
$res->{'writeErrors'} ? : 0 0 0
$res->{'writeConcernError'} ? : 0 0 0
238 $self->_parse_cmd($res),
239 );
240 0 $self->_assert_session_errors( $built_result );
241 0 return $built_result;
242 }
243 else {
244 0 return MongoDB::UnacknowledgedResult->_new(
245 write_errors => [],
246 write_concern_errors => [],
247 );
248 }
249 }
250  
251 59 1;
« MongoDB/Op/_ChangeStream.pm ↑ Summary MongoDB/Op/_BulkWrite.pm »