| 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::WriteConcern; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # ABSTRACT: Encapsulate and validate a write concern | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 60 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 60 | our $VERSION = 'v2.2.2'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 60 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 60 | ▶ | use MongoDB::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 60 | ▶ | use MongoDB::_Types qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | Boolish | ||||||||||||||||||||||||||||
| 28 | 60 | ); | |||||||||||||||||||||||||||
| 29 | 60 | ▶ | use Types::Standard qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | ArrayRef | ||||||||||||||||||||||||||||
| 31 | Int | ||||||||||||||||||||||||||||
| 32 | Str | ||||||||||||||||||||||||||||
| 33 | Maybe | ||||||||||||||||||||||||||||
| 34 | 60 | ); | |||||||||||||||||||||||||||
| 35 | 60 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 60 | ▶ | use boolean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 60 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | #pod =attr w | ||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||
| 41 | #pod Specifies the desired acknowledgement level. If not set, the | ||||||||||||||||||||||||||||
| 42 | #pod server default will be used, which is usually "1". | ||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||
| 44 | #pod =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 60 | has w => ( | |||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => Maybe[Str], | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #pod =attr wtimeout | ||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||
| 53 | #pod Specifies how long to wait for the write concern to be satisfied (in | ||||||||||||||||||||||||||||
| 54 | #pod milliseconds). Defaults to 1000. If you set this to undef, it could block | ||||||||||||||||||||||||||||
| 55 | #pod indefinitely (or until socket timeout is reached). | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 60 | has wtimeout => ( | |||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | isa => Maybe[Int], | ||||||||||||||||||||||||||||
| 62 | default => 1000, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | #pod =attr j | ||||||||||||||||||||||||||||
| 66 | #pod | ||||||||||||||||||||||||||||
| 67 | #pod The j option confirms that the mongod instance has written the data to the | ||||||||||||||||||||||||||||
| 68 | #pod on-disk journal. Defaults to false. | ||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||
| 70 | #pod B<Note>: specifying a write concern that set j to a true value may result in an | ||||||||||||||||||||||||||||
| 71 | #pod error with a mongod or mongos running with --nojournal option now errors. | ||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||
| 73 | #pod =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 60 | has j => ( | |||||||||||||||||||||||||||
| 76 | is => 'ro', | ||||||||||||||||||||||||||||
| 77 | isa => Boolish, | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 60 | has _is_acknowledged => ( | |||||||||||||||||||||||||||
| 81 | is => 'lazy', | ||||||||||||||||||||||||||||
| 82 | isa => Boolish, | ||||||||||||||||||||||||||||
| 83 | reader => 'is_acknowledged', | ||||||||||||||||||||||||||||
| 84 | builder => '_build_is_acknowledged', | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 60 | has _as_args => ( | |||||||||||||||||||||||||||
| 88 | is => 'lazy', | ||||||||||||||||||||||||||||
| 89 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 90 | reader => 'as_args', | ||||||||||||||||||||||||||||
| 91 | builder => '_build_as_args', | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub _build_is_acknowledged { | ||||||||||||||||||||||||||
|
Subroutine
_build_is_acknowledged: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 95 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | return !!( $self->j || $self->_w_is_acknowledged ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->j || $self->_w_is_acknowledged
MC/DC0%0/2
$self->j || $self->_w_is_acknowledged
$self->j
$self->_w_is_acknowledged
Truth table0%0/3
A || B
A= $self->jB= $self->_w_is_acknowledged
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 21 | ▶ | sub _build_as_args { | ||||||||||||||||||||||||||
|
Subroutine
_build_as_args: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 100 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 21 | ▶ | my $wc = { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 103 | ( defined( $self->w ) ? ( w => $self->w ) : () ), | ||||||||||||||||||||||||||||
| 104 | ( defined( $self->wtimeout ) ? ( wtimeout => 0+ $self->wtimeout ) : () ), | ||||||||||||||||||||||||||||
| 105 | ( defined( $self->j ) ? ( j => boolean( $self->j ) ) : () ), | ||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 21 | ▶ | return ( keys %$wc ? [writeConcern => $wc] : [] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 200 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 200 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | 200 | ▶ | if ( ! $self->_w_is_valid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | MongoDB::UsageError->throw("can't use write concern w=" . $self->w ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 199 | ▶ | if ( ! $self->_w_is_acknowledged && $self->j ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->_w_is_acknowledged and $self->j
MC/DC100%2/2
not $self->_w_is_acknowledged and $self->j
not $self->_w_is_acknowledged
$self->j
Truth table100%3/3
not(A) and B
A= not $self->_w_is_acknowledgedB= $self->j
| |||||||||||||||||||||||||||||
| 118 | 3 | MongoDB::UsageError->throw("can't use write concern w=0 with j=" . $self->j ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # cant use nonnegnum earlier in type as errors explode with wrong class | ||||||||||||||||||||||||||||
| 122 | 196 | ▶ | if ( defined($self->wtimeout) && $self->wtimeout < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->wtimeout and $self->wtimeout < 0
MC/DC100%2/2
defined $self->wtimeout and $self->wtimeout < 0
defined $self->wtimeout
$self->wtimeout < 0
Truth table100%3/3
A and B
A= defined $self->wtimeoutB= $self->wtimeout < 0
| |||||||||||||||||||||||||||||
| 123 | 1 | MongoDB::UsageError->throw("wtimeout must be non negative"); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 195 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 200 | ▶ | sub _w_is_valid { | ||||||||||||||||||||||||||
|
Subroutine
_w_is_valid: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | 200 | my ($self) = @_; | |||||||||||||||||||||||||||
| 130 | 200 | ▶ | return 1 if !defined $self->w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 20 | ▶ | return looks_like_number( $self->w ) ? $self->w >= 0 : length $self->w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 199 | ▶ | sub _w_is_acknowledged { | ||||||||||||||||||||||||||
|
Subroutine
_w_is_acknowledged: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 135 | 199 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 199 | ▶ | return 1 if !defined $self->w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 19 | ▶ | return looks_like_number( $self->w ) ? $self->w > 0 : length $self->w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 60 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||