MongoDB/BulkWriteResult.pm

filter: Statementstmt 34.9% 51 / 146 Branchbran 5.0% 2 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 10 / 20 Podpod 0.0% 0 / 1 totaltotal 27.6% 63 / 228 CC 29 SCAR 50.7
CC
29
Cov
27%
_parse_cmd_result
380.0
_parse_write_op
42.0
_merge_result
30.0
CRAP 159.4
« MongoDB/Op/_BulkWrite.pm ↑ Summary MongoDB/Collection.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 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package MongoDB::BulkWriteResult;
18  
19 # ABSTRACT: MongoDB bulk write result document
20  
21 60 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 60 our $VERSION = 'v2.2.2';
23  
24 # empty superclass for backcompatibility; add a variable to the
25 # package namespace so Perl thinks it's a real package
26 60 $MongoDB::WriteResult::VERSION = $VERSION;
27  
28 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 60 ▶use MongoDB::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 60 ▶use MongoDB::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 60 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
32 ArrayOfHashRef
33 Numish
34 60 );
35 60 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
36 HashRef
37 Undef
38 60 );
39 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # fake empty superclass for backcompat
42 60 our @ISA;
43 60 push @ISA, 'MongoDB::WriteResult';
44  
45 60 with $_ for qw(
46 MongoDB::Role::_PrivateConstructor
47 MongoDB::Role::_WriteResult
48 );
49  
50 60 has [qw/upserted inserted/] => (
51 is => 'ro',
52 required => 1,
53 isa => ArrayOfHashRef,
54 );
55  
56 60 has inserted_ids => (
57 is => 'lazy',
58 builder => '_build_inserted_ids',
59 init_arg => undef,
60 isa => HashRef,
61 );
62  
63 0 ▶sub _build_inserted_ids {
Subroutine _build_inserted_ids: not called CC=1 SCAR=6.9
64 0 my ($self) = @_;
65 0 return { map { $_->{index}, $_->{_id} } @{ $self->inserted } };
66 }
67  
68 60 has upserted_ids => (
69 is => 'lazy',
70 builder => '_build_upserted_ids',
71 init_arg => undef,
72 isa => HashRef,
73 );
74  
75 0 ▶sub _build_upserted_ids {
Subroutine _build_upserted_ids: not called CC=1 SCAR=6.9
76 0 my ($self) = @_;
77 0 return { map { $_->{index}, $_->{_id} } @{ $self->upserted } };
78 }
79  
80 60 for my $attr (qw/inserted_count upserted_count matched_count deleted_count/) {
81 240 has $attr => (
82 is => 'ro',
83 writer => "_set_$attr",
84 required => 1,
85 isa => Numish,
86 );
87 }
88  
89 # This should always be initialized either as a number or as undef so that
90 # merges accumulate correctly. It should be undef if talking to a server < 2.6
91 # or if talking to a mongos and not getting the field back from an update. The
92 # default is undef, which will be sticky and ensure this field stays undef.
93  
94 60 has modified_count => (
95 is => 'ro',
96 writer => '_set_modified_count',
97 required => 1,
98 isa => (Numish|Undef),
99 );
100  
101 0 ▶sub has_modified_count {
Subroutine has_modified_count: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my ($self) = @_;
103 0 return defined( $self->modified_count );
104 }
105  
106 60 has op_count => (
107 is => 'ro',
108 writer => '_set_op_count',
109 required => 1,
110 isa => Numish,
111 );
112  
113 60 has batch_count => (
114 is => 'ro',
115 writer => '_set_batch_count',
116 required => 1,
117 isa => Numish,
118 );
119  
120 #--------------------------------------------------------------------------#
121 # emulate old API
122 #--------------------------------------------------------------------------#
123  
124 60 my %OLD_API_ALIASING = (
125 nInserted => 'inserted_count',
126 nUpserted => 'upserted_count',
127 nMatched => 'matched_count',
128 nModified => 'modified_count',
129 nRemoved => 'deleted_count',
130 writeErrors => 'write_errors',
131 writeConcernErrors => 'write_concern_errors',
132 count_writeErrors => 'count_write_errors',
133 count_writeConcernErrors => 'count_write_concern_errors',
134 );
135  
136 60 ▶while ( my ( $old, $new ) = each %OLD_API_ALIASING ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %OLD_API_ALIASING) 540 60 600
137 60 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
138 540 *{$old} = \&{$new};
139 }
140  
141 #--------------------------------------------------------------------------#
142 # private functions
143 #--------------------------------------------------------------------------#
144  
145 # defines how an logical operation type gets mapped to a result
146 # field from the actual command result
147 my %op_map = (
148 0 ▶ insert => [ inserted_count => sub { $_[0]->{n} } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 ▶ delete => [ deleted_count => sub { $_[0]->{n} } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 ▶ update => [ matched_count => sub { $_[0]->{n} } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 ▶ upsert => [ matched_count => sub { $_[0]->{n} - @{ $_[0]->{upserted} || [] } } ],
Subroutine __ANON__: not called CC=2 SCAR=14.0
Condition0%0/2
$_[0]{'upserted'} || []
l!l
00
MC/DC0%0/1
$_[0]{'upserted'} || []
$_[0]{'upserted'}
Truth table0%0/2
A
A= $_[0]{'upserted'}
Aresult
11
00
152 );
153  
154 60 my @op_map_keys = sort keys %op_map;
155  
156 0 ▶sub _parse_cmd_result {
Subroutine _parse_cmd_result: not called CC=19 SCAR=59.4
157 0 my $class = shift;
158 0 ▶ my $args = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
159  
160 0 ▶ unless ( 2 == grep { exists $args->{$_} } qw/op result/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (2 == grep({exists $args->{$_};} "op", "result")) 0 0 0
161 0 MongoDB::UsageError->throw("parse requires 'op' and 'result' arguments");
162 }
163  
164 my ( $op, $op_count, $batch_count, $result, $cmd_doc, $idx_map ) =
165 0 @{$args}{qw/op op_count batch_count result cmd_doc idx_map/};
166  
167 $result = $result->output
168 0 ▶ if eval { $result->isa("MongoDB::CommandResult") };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $result->isa("MongoDB::CommandResult") } } 0 0 0
169  
170 MongoDB::UsageError->throw("op argument to parse must be one of: @op_map_keys")
171 0 ▶ unless grep { $op eq $_ } @op_map_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$op eq $_;} @op_map_keys 0 0 0
172 0 ▶ MongoDB::UsageError->throw("results argument to parse must be a hash reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" 0 0 0
173 unless ref $result eq 'HASH';
174  
175 0 ▶ my %attrs = (
Branch: 1 branch
BranchTrueFalseTotal
$op_count ? : 0 0 0
Condition0%0/2
$batch_count || 1
l!l
00
MC/DC0%0/1
$batch_count || 1
$batch_count
Truth table0%0/2
A
A= $batch_count
Aresult
11
00
176 batch_count => $batch_count || 1,
177 $op_count ? ( op_count => $op_count ) : (),
178 inserted_count => 0,
179 upserted_count => 0,
180 matched_count => 0,
181 deleted_count => 0,
182 upserted => [],
183 inserted => [],
184 );
185  
186 0 ▶ $attrs{write_errors} = $result->{writeErrors} ? $result->{writeErrors} : [];
Branch: 1 branch
BranchTrueFalseTotal
$result->{'writeErrors'} ? : 0 0 0
187  
188 # rename writeConcernError -> write_concern_errors; coerce it to arrayref
189  
190 $attrs{write_concern_errors} =
191 0 ▶ $result->{writeConcernError} ? [ $result->{writeConcernError} ] : [];
Branch: 1 branch
BranchTrueFalseTotal
$result->{'writeConcernError'} ? : 0 0 0
192  
193 # if we have upserts, change type to calculate differently
194 0 ▶ if ( $result->{upserted} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"upserted"}) 0 0 0
195 0 $op = 'upsert';
196 0 $attrs{upserted} = $result->{upserted};
197 0 $attrs{upserted_count} = @{ $result->{upserted} };
198 }
199  
200 my %error_idx = (
201 0 map { $_->{index} => 1 } @{ $result->{writeErrors} },
202 );
203  
204 # recover _ids from documents
205 0 ▶ if ( exists($result->{n}) && $op eq 'insert' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"n"} and $op eq "insert") 0 0 0
Condition0%0/3
exists $result->{"n"} and $op eq "insert"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $result->{"n"} and $op eq "insert"
exists $result->{"n"} $op eq "insert"
Truth table0%0/3
A and B
A= exists $result->{"n"}B= $op eq "insert"
ABresult
0X0
100
111
206 0 my @pairs;
207 0 my $docs = {@$cmd_doc}->{documents};
208 0 for my $i ( 0 .. $result->{n}-1 ) {
209 0 ▶ next if $error_idx{$i};
Branch: 1 branch
BranchTrueFalseTotal
if $error_idx{$i} 0 0 0
210 0 push @pairs, { index => $i, _id => $docs->[$i]{metadata}{_id} };
211 }
212 0 $attrs{inserted} = \@pairs;
213 }
214  
215 # change 'n' into an op-specific count
216 0 ▶ if ( exists $result->{n} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"n"}) 0 0 0
217 0 my ( $key, $builder ) = @{ $op_map{$op} };
218 0 $attrs{$key} = $builder->($result);
219 }
220  
221 # for an update/upsert we want the exact response whether numeric or undef
222 # so that new undef responses become sticky; for all other updates, we
223 # consider it 0 and let it get sorted out in the merging
224  
225 $attrs{modified_count} = ( $op eq 'update' || $op eq 'upsert' ) ?
226 0 ▶ $result->{nModified} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'update' || $op eq 'upsert' ? : 0 0 0
Condition0%0/3
$op eq "update" or $op eq "upsert"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "update" or $op eq "upsert"
$op eq "update" $op eq "upsert"
Truth table0%0/3
A or B
A= $op eq "update"B= $op eq "upsert"
ABresult
1X1
011
000
227  
228 # Remap all indices back to original queue index
229 # in unordered batches, these numbers can end up pointing to the wrong index
230 0 for my $attr (qw/write_errors upserted inserted/) {
231 0 map { $_->{index} = $idx_map->[$_->{index}] } @{ $attrs{$attr} };
232 }
233  
234 0 return $class->_new(%attrs);
235 }
236  
237 # these are for single results only
238 0 ▶sub _parse_write_op {
Subroutine _parse_write_op: not called CC=6 SCAR=37.4
239 0 my $class = shift;
240 0 my $op = shift;
241  
242 0 my %attrs = (
243 batch_count => 1,
244 op_count => 1,
245 write_errors => $op->write_errors,
246 write_concern_errors => $op->write_concern_errors,
247 inserted_count => 0,
248 upserted_count => 0,
249 matched_count => 0,
250 modified_count => undef,
251 deleted_count => 0,
252 upserted => [],
253 inserted => [],
254 );
255  
256 0 my $has_write_error = @{ $attrs{write_errors} };
257  
258 # parse by type
259 0 my $type = ref($op);
260 0 ▶ if ( $type eq 'MongoDB::InsertOneResult' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'MongoDB::InsertOneResult') { } 0 0 0
elsif ($type eq 'MongoDB::DeleteResult') { } 0 0 0
elsif ($type eq 'MongoDB::UpdateResult') { } 0 0 0
261 0 ▶ if ( $has_write_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_write_error) { } 0 0 0
262 0 $attrs{inserted_count} = 0;
263 0 $attrs{inserted} = [];
264 }
265 else {
266 0 $attrs{inserted_count} = 1;
267 0 $attrs{inserted} = [ { index => 0, _id => $op->inserted_id } ];
268 }
269 }
270 elsif ( $type eq 'MongoDB::DeleteResult' ) {
271 0 $attrs{deleted_count} = $op->deleted_count;
272 }
273 elsif ( $type eq 'MongoDB::UpdateResult' ) {
274 0 ▶ if ( defined $op->upserted_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $op->upserted_id) { } 0 0 0
275 0 my $upsert = { index => 0, _id => $op->upserted_id };
276 0 $attrs{upserted} = [$upsert];
277 0 $attrs{upserted_count} = 1;
278 # modified_count *must* always be defined for 2.6+ servers
279 # matched_count is here for clarity and consistency
280 0 $attrs{matched_count} = 0;
281 0 $attrs{modified_count} = 0;
282 }
283 else {
284 0 $attrs{matched_count} = $op->matched_count;
285 0 $attrs{modified_count} = $op->modified_count;
286 }
287 }
288 else {
289 0 MongoDB::InternalError->throw("can't parse unknown result class $op");
290 }
291  
292 0 return $class->_new(%attrs);
293 }
294  
295 0 ▶sub _merge_result {
Subroutine _merge_result: not called CC=5 SCAR=34.0
296 0 my ( $self, $result ) = @_;
297  
298 # Add simple counters
299 0 for my $attr (qw/inserted_count upserted_count matched_count deleted_count/) {
300 0 my $setter = "_set_$attr";
301 0 $self->$setter( $self->$attr + $result->$attr );
302 }
303  
304 # If modified_count is defined in both results we're merging, then we're
305 # talking to a 2.6+ mongod or we're talking to a 2.6+ mongos and have only
306 # seen responses with modified_count. In any other case, we set
307 # modified_count to undef, which then becomes "sticky"
308  
309 0 ▶ if ( defined $self->modified_count && defined $result->modified_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->modified_count and defined $result->modified_count) { } 0 0 0
Condition0%0/3
defined $self->modified_count and defined $result->modified_count
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->modified_count and defined $result->modified_count
defined $self->modified_count defined $result->modified_count
Truth table0%0/3
A and B
A= defined $self->modified_countB= defined $result->modified_count
ABresult
0X0
100
111
310 0 $self->_set_modified_count( $self->modified_count + $result->modified_count );
311 }
312 else {
313 0 $self->_set_modified_count(undef);
314 }
315  
316 # Append error and upsert docs, index is dealt with in _parse_cmd_result
317 0 for my $attr (qw/write_errors upserted inserted/) {
318 0 push @{ $self->$attr }, @{ $result->$attr };
319 }
320  
321 # Append write concern errors without modification (they have no index)
322 0 push @{ $self->write_concern_errors }, @{ $result->write_concern_errors };
323  
324 0 $self->_set_op_count( $self->op_count + $result->op_count );
325 0 $self->_set_batch_count( $self->batch_count + $result->batch_count );
326  
327 0 return 1;
328 }
329  
330 60 1;
331  
332 __END__
« MongoDB/Op/_BulkWrite.pm ↑ Summary MongoDB/Collection.pm »