MongoDB/Role/_ReadPrefModifier.pm

filter: Statementstmt 42.0% 29 / 69 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 72.7% 8 / 11 Podpod n/a totaltotal 26.8% 37 / 138 CC 22 SCAR 40.7
CC
22
Cov
25%
_apply_op_query_read_prefs
90.0
_apply_mongos_read_prefs
90.0
_apply_op_msg_read_prefs
42.0
CRAP 58.8
« MongoDB/Op/_Delete.pm ↑ Summary MongoDB/Role/_DatabaseErrorThrower.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  
18 package MongoDB::Role::_ReadPrefModifier;
19  
20 # MongoDB role to modify OP_QUERY query document or flags to account
21 # for topology-specific treatment of a read-preference (if any)
22  
23 60 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 60 our $VERSION = 'v2.2.2';
25  
26 60 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 60 ▶use MongoDB::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 60 ▶use MongoDB::ReadPreference;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 60 ▶use MongoDB::_Types -types, 'to_IxHash';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 60 requires qw/read_preference/;
35  
36 60 my $PRIMARY = MongoDB::ReadPreference->new()->_as_hashref;
37 60 my $PRIMARYPREFERRED =
38 MongoDB::ReadPreference->new( mode => 'primaryPreferred' )->_as_hashref;
39  
40 0 ▶sub _apply_op_msg_read_prefs {
Subroutine _apply_op_msg_read_prefs: not called CC=6 SCAR=37.4
41 0 my ( $self, $link, $topology_type, $query_flags, $query_ref ) = @_;
42  
43 0 ▶ $topology_type ||= "<undef>";
Condition0%0/2
$topology_type ||= "<undef>"
l!l
00
MC/DC0%0/1
$topology_type ||= "<undef>"
$topology_type
Truth table0%0/2
A
A= $topology_type
Aresult
11
00
44 0 my $read_pref = $self->read_preference;
45 0 ▶ my $read_pref_doc = $read_pref ? $read_pref->_as_hashref : $PRIMARY;
Branch: 1 branch
BranchTrueFalseTotal
$read_pref ? : 0 0 0
46  
47 0 ▶ if ( $topology_type eq 'Single' && ! ($link->server && $link->server->type eq 'Mongos') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topology_type eq "Single" and not $link->server && $link->server->type eq "Mongos") 0 0 0
Condition0%0/6
$topology_type eq "Single" and not $link->server && $link->server->type eq "Mongos"
!ll&&!rl&&r
000
$link->server && $link->server->type eq 'Mongos'
!ll&&!rl&&r
000
MC/DC0%0/3
$topology_type eq "Single" and not $link->server && $link->server->type eq "Mongos"
$topology_type eq "Single" $link->server $link->server->type eq 'Mongos'
Truth table0%0/4
A and not(B && C)
A= $topology_type eq "Single"B= $link->serverC= $link->server->type eq 'Mongos'
ABCresult
0XX0
1110
10X1
1101
48 # For direct connection to a non-mongos single server, allow any server
49 # type, overriding the provided read preference
50 0 $read_pref_doc = $PRIMARYPREFERRED;
51 }
52  
53 0 $$query_ref = to_IxHash($$query_ref);
54 0 ($$query_ref)->Push( '$readPreference' => $read_pref_doc );
55  
56 0 return;
57 }
58  
59 0 ▶sub _apply_op_query_read_prefs {
Subroutine _apply_op_query_read_prefs: not called CC=9 SCAR=45.0
60 0 my ( $self, $link, $topology_type, $query_flags, $query_ref ) = @_;
61  
62 0 ▶ $topology_type ||= "<undef>";
Condition0%0/2
$topology_type ||= "<undef>"
l!l
00
MC/DC0%0/1
$topology_type ||= "<undef>"
$topology_type
Truth table0%0/2
A
A= $topology_type
Aresult
11
00
63 0 my $read_pref = $self->read_preference;
64  
65 0 ▶ if ( $topology_type eq 'Single' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($topology_type eq 'Single') { } 0 0 0
elsif (grep {$topology_type eq $_;} 'ReplicaSetNoPrimary', 'ReplicaSetWithPrimary') { } 0 0 0
elsif ($topology_type eq 'Sharded') { } 0 0 0
66 0 ▶ if ( $link->server && $link->server->type eq 'Mongos' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->server and $link->server->type eq 'Mongos') { } 0 0 0
Condition0%0/3
$link->server and $link->server->type eq "Mongos"
!ll&&!rl&&r
000
MC/DC0%0/2
$link->server and $link->server->type eq "Mongos"
$link->server $link->server->type eq "Mongos"
Truth table0%0/3
A and B
A= $link->serverB= $link->server->type eq "Mongos"
ABresult
0X0
100
111
67 0 $self->_apply_mongos_read_prefs( $read_pref, $query_flags, $query_ref );
68 }
69 else {
70 0 $query_flags->{slave_ok} = 1;
71 }
72 }
73 0 elsif ( grep { $topology_type eq $_ } qw/ReplicaSetNoPrimary ReplicaSetWithPrimary/ )
74 {
75 0 ▶ if ( !$read_pref || $read_pref->mode eq 'primary' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $read_pref or $read_pref->mode eq 'primary') { } 0 0 0
Condition0%0/3
not $read_pref or $read_pref->mode eq "primary"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $read_pref or $read_pref->mode eq "primary"
not $read_pref $read_pref->mode eq "primary"
Truth table0%0/3
not(A) or B
A= not $read_prefB= $read_pref->mode eq "primary"
ABresult
1X1
011
000
76 0 $query_flags->{slave_ok} = 0;
77 }
78 else {
79 0 $query_flags->{slave_ok} = 1;
80 }
81 }
82 elsif ( $topology_type eq 'Sharded' ) {
83 0 $self->_apply_mongos_read_prefs( $read_pref, $query_flags, $query_ref );
84 }
85 else {
86 0 MongoDB::InternalError->throw("can't query topology type '$topology_type'");
87 }
88  
89 0 return;
90 }
91  
92 0 ▶sub _apply_mongos_read_prefs {
Subroutine _apply_mongos_read_prefs: not called CC=9 SCAR=45.0
93 0 my ( $self, $read_pref, $query_flags, $query_ref ) = @_;
94 0 ▶ my $mode = $read_pref ? $read_pref->mode : 'primary';
Branch: 1 branch
BranchTrueFalseTotal
$read_pref ? : 0 0 0
95 0 my $need_read_pref;
96  
97 0 ▶ if ( $mode eq 'primary' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'primary') { } 0 0 0
elsif (grep {$mode eq $_;} 'secondary', 'primaryPreferred', 'nearest') { } 0 0 0
elsif ($mode eq 'secondaryPreferred') { } 0 0 0
98 0 $query_flags->{slave_ok} = 0;
99 }
100 0 elsif ( grep { $mode eq $_ } qw/secondary primaryPreferred nearest/ ) {
101 0 $query_flags->{slave_ok} = 1;
102 0 $need_read_pref = 1;
103 }
104 elsif ( $mode eq 'secondaryPreferred' ) {
105 0 $query_flags->{slave_ok} = 1;
106 0 ▶ $need_read_pref = 1
Branch: 1 branch
BranchTrueFalseTotal
unless $read_pref->has_empty_tag_sets and $read_pref->max_staleness_seconds == -1 0 0 0
Condition0%0/3
$read_pref->has_empty_tag_sets and $read_pref->max_staleness_seconds == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$read_pref->has_empty_tag_sets and $read_pref->max_staleness_seconds == -1
$read_pref->has_empty_tag_sets $read_pref->max_staleness_seconds == -1
Truth table0%0/3
A and B
A= $read_pref->has_empty_tag_setsB= $read_pref->max_staleness_seconds == -1
ABresult
0X0
100
111
107 unless $read_pref->has_empty_tag_sets && $read_pref->max_staleness_seconds == -1;
108 }
109 else {
110 0 MongoDB::InternalError->throw("invalid read preference mode '$mode'");
111 }
112  
113 0 ▶ if ($need_read_pref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_read_pref) 0 0 0
114 0 $$query_ref = to_IxHash($$query_ref);
115 0 ▶ if ( !($$query_ref)->FETCH('$query') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$query_ref->FETCH("\$query")) 0 0 0
116 0 $$query_ref = Tie::IxHash->new( '$query' => $$query_ref );
117 }
118 0 ($$query_ref)->Push( '$readPreference' => $read_pref->_as_hashref );
119 }
120  
121 0 return;
122 }
123  
124 60 1;
« MongoDB/Op/_Delete.pm ↑ Summary MongoDB/Role/_DatabaseErrorThrower.pm »