MongoDB/Op/_Update.pm

filter: Statementstmt 58.6% 44 / 75 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 1 totaltotal 34.1% 55 / 161 CC 31 SCAR 45.3
CC
31
Cov
32%
execute
210.0
_parse_gle
72.0
_find_id
72.0
CRAP 93.0
« MongoDB/_Dispatcher.pm ↑ Summary MongoDB/ClientSession.pm »
1 # Copyright 2014 - 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::_Update;
18  
19 # Encapsulate an update operation; returns a MongoDB::UpdateResult
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 ▶use MongoDB::UpdateResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 59 ▶use MongoDB::_Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 59 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
29 Boolish
30 Document
31 59 );
32 59 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
33 Maybe
34 ArrayRef
35 59 );
36 59 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 59 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 59 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 59 has filter => (
42 is => 'ro',
43 required => 1,
44 isa => Document,
45 );
46  
47 59 has update => (
48 is => 'ro',
49 required => 1,
50 );
51  
52 59 has is_replace => (
53 is => 'ro',
54 required => 1,
55 isa => Boolish,
56 );
57  
58 59 has multi => (
59 is => 'ro',
60 required => 1,
61 isa => Boolish,
62 );
63  
64 59 has upsert => (
65 is => 'ro',
66 );
67  
68 59 has collation => (
69 is => 'ro',
70 isa => Maybe( [Document] ),
71 );
72  
73 59 has arrayFilters => (
74 is => 'ro',
75 isa => Maybe( [ArrayRef[Document]] ),
76 );
77  
78 59 with $_ for qw(
79 MongoDB::Role::_PrivateConstructor
80 MongoDB::Role::_CollectionOp
81 MongoDB::Role::_SingleBatchDocWrite
82 MongoDB::Role::_UpdatePreEncoder
83 MongoDB::Role::_BypassValidation
84 );
85  
86 # cached
87 59 my ($true, $false) = (true, false);
88  
89 0 ▶sub execute {
Subroutine execute: not called CC=14 SCAR=53.5
Pod undocumented
90 0 my ( $self, $link ) = @_;
91  
92 0 ▶ if ( defined $self->collation ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->collation) 0 0 0
93 0 ▶ MongoDB::UsageError->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $link->supports_collation 0 0 0
94 "MongoDB host '" . $link->address . "' doesn't support collation" )
95 if !$link->supports_collation;
96 0 ▶ MongoDB::UsageError->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_use_acknowledged_write 0 0 0
97 "Unacknowledged updates that specify a collation are not allowed")
98 if ! $self->_should_use_acknowledged_write;
99 }
100  
101 0 ▶ if ( defined $self->arrayFilters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->arrayFilters) 0 0 0
102 0 ▶ MongoDB::UsageError->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $link->supports_arrayFilters 0 0 0
103 "MongoDB host '" . $link->address . "' doesn't support arrayFilters" )
104 if !$link->supports_arrayFilters;
105 0 ▶ MongoDB::UsageError->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_use_acknowledged_write 0 0 0
106 "Unacknowledged updates that specify arrayFilters are not allowed")
107 if ! $self->_should_use_acknowledged_write;
108 }
109  
110 my $orig_op = {
111 q => (
112 ref( $self->filter ) eq 'ARRAY'
113 0 ▶ ? { @{ $self->filter } }
Branch: 5 branches
BranchTrueFalseTotal
ref $self->filter eq 'ARRAY' ? : 0 0 0
$self->multi ? : 0 0 0
$self->upsert ? : 0 0 0
defined $self->collation ? : 0 0 0
defined $self->arrayFilters ? : 0 0 0
114 : $self->filter
115 ),
116 u => $self->_pre_encode_update( $link->max_bson_object_size,
117 $self->update, $self->is_replace ),
118 multi => $self->multi ? $true : $false,
119 upsert => $self->upsert ? $true : $false,
120 ( defined $self->collation ? ( collation => $self->collation ) : () ),
121 ( defined $self->arrayFilters ? ( arrayFilters => $self->arrayFilters ) : () ),
122 };
123  
124 return $self->_send_legacy_op_noreply(
125 $link,
126 MongoDB::_Protocol::write_update(
127 $self->full_name,
128 $self->bson_codec->encode_one( $orig_op->{q}, { invalid_chars => '' } ),
129 $self->_pre_encode_update( $link->max_bson_object_size,
130 $orig_op->{u}, $self->is_replace )->{bson},
131 {
132 upsert => $orig_op->{upsert},
133 multi => $orig_op->{multi},
134 },
135 0 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_use_acknowledged_write 0 0 0
136 $orig_op,
137 "MongoDB::UpdateResult",
138 "update",
139 ) if ! $self->_should_use_acknowledged_write;
140  
141 return $self->_send_write_command(
142 $link,
143 $self->_maybe_bypass(
144 $link->supports_document_validation,
145 [
146 update => $self->coll_name,
147 updates => [
148 {
149 %$orig_op,
150 u => $self->_pre_encode_update(
151 $link->max_bson_object_size,
152 $orig_op->{u}, $self->is_replace
153 ),
154 }
155 ],
156 0 ▶ @{ $self->write_concern->as_args },
Branch: 1 branch
BranchTrueFalseTotal
if $link->supports_write_commands 0 0 0
157 ],
158 ),
159 $orig_op,
160 "MongoDB::UpdateResult"
161 )->assert
162 if $link->supports_write_commands;
163  
164 return $self->_send_legacy_op_with_gle(
165 $link,
166 MongoDB::_Protocol::write_update(
167 $self->full_name,
168 $self->bson_codec->encode_one( $orig_op->{q}, { invalid_chars => '' } ),
169 $self->_pre_encode_update( $link->max_bson_object_size,
170 $orig_op->{u}, $self->is_replace )->{bson},
171 {
172 upsert => $orig_op->{upsert},
173 multi => $orig_op->{multi},
174 },
175 0 ),
176 $orig_op,
177 "MongoDB::UpdateResult",
178 "update",
179 )->assert;
180 }
181  
182 0 ▶sub _parse_cmd {
Subroutine _parse_cmd: not called CC=4 SCAR=30.0
183 0 my ( $self, $res ) = @_;
184  
185 return (
186 0 ▶ matched_count => ($res->{n} || 0) - @{ $res->{upserted} || [] },
Branch: 1 branch
BranchTrueFalseTotal
$res->{'upserted'} ? : 0 0 0
Condition0%0/2
$res->{'upserted'} || []
l!l
00
MC/DC0%0/1
$res->{'upserted'} || []
$res->{'upserted'}
Truth table0%0/2
A
A= $res->{'upserted'}
Aresult
11
00
187 modified_count => $res->{nModified},
188 upserted_id => $res->{upserted} ? $res->{upserted}[0]{_id} : undef,
189 0 ▶ );
Condition0%0/2
$res->{'n'} || 0
l!l
00
MC/DC0%0/1
$res->{'n'} || 0
$res->{'n'}
Truth table0%0/2
A
A= $res->{'n'}
Aresult
11
00
190 }
191  
192 0 ▶sub _parse_gle {
Subroutine _parse_gle: not called CC=8 SCAR=42.8
193 0 my ( $self, $res, $orig_doc ) = @_;
194  
195 # For 2.4 and earlier, 'upserted' has _id only if the _id is an OID.
196 # Otherwise, we have to pick it out of the update document or query
197 # document when we see updateExisting is false but the number of docs
198 # affected is 1
199  
200 0 my $upserted = $res->{upserted};
201 0 ▶ if (! defined( $upserted )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $upserted and exists $res->{"updatedExisting"} and not $res->{"updatedExisting"} and $res->{"n"} == 1) 0 0 0
Condition0%0/9
not defined $upserted and exists $res->{"updatedExisting"} and not $res->{"updatedExisting"} and $res->{"n"} == 1
!ll&&!rl&&r
000
not defined $upserted and exists $res->{"updatedExisting"} and not $res->{"updatedExisting"}
!ll&&!rl&&r
000
not defined $upserted and exists $res->{"updatedExisting"}
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $upserted and exists $res->{"updatedExisting"} and not $res->{"updatedExisting"} and $res->{"n"} == 1
not defined $upserted exists $res->{"updatedExisting"} not $res->{"updatedExisting"} $res->{"n"} == 1
Truth table0%0/5
not(A) and B and not(C) and D
A= not defined $upsertedB= exists $res->{"updatedExisting"}C= not $res->{"updatedExisting"}D= $res->{"n"} == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
202 && exists( $res->{updatedExisting} )
203 && !$res->{updatedExisting}
204 && $res->{n} == 1 )
205 {
206 0 $upserted = $self->_find_id( $orig_doc->{u} );
207 0 ▶ $upserted = $self->_find_id( $orig_doc->{q} ) unless defined $upserted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upserted 0 0 0
208 }
209  
210 return (
211 0 ▶ matched_count => ($upserted ? 0 : $res->{n} || 0),
Branch: 1 branch
BranchTrueFalseTotal
$upserted ? : 0 0 0
Condition0%0/2
$res->{"n"} or 0
l!l
00
MC/DC0%0/1
$res->{"n"} or 0
$res->{"n"}
Truth table0%0/2
A
A= $res->{"n"}
Aresult
11
00
212 modified_count => undef,
213 upserted_id => $upserted,
214 );
215 }
216  
217 0 ▶sub _find_id {
Subroutine _find_id: not called CC=8 SCAR=42.8
218 0 my ($self, $doc) = @_;
219 0 ▶ if (ref($doc) eq "BSON::Raw") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc eq "BSON::Raw") 0 0 0
220 0 $doc = $self->bson_codec->decode_one($doc);
221 }
222 0 my $type = ref($doc);
223 return (
224 $type eq 'HASH' ? $doc->{_id}
225 : $type eq 'ARRAY' ? do {
226 0 my $i;
227 0 ▶ for ( $i = 0; $i < @$doc; $i++ ) { last if $doc->[$i] eq '_id' }
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @$doc) 0 0 0
if $doc->[$i] eq "_id" 0 0 0
228 0 ▶ $i < $#$doc ? $doc->[ $i + 1 ] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$i < $doc->$#* ? : 0 0 0
$type eq 'Tie::IxHash' ? : 0 0 0
229 }
230 : $type eq 'Tie::IxHash' ? $doc->FETCH('_id')
231 : $doc->{_id} # hashlike?
232 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'HASH' ? : 0 0 0
$type eq 'ARRAY' ? : 0 0 0
233 }
234  
235 59 1;
« MongoDB/_Dispatcher.pm ↑ Summary MongoDB/ClientSession.pm »