File Coverage

blib/lib/MongoDB/WriteConcern.pm
Criterion Covered Total %
statement 47 49 95.9
branch 22 22 100.0
condition 6 9 66.6
subroutine 14 15 93.3
pod 0 1 0.0
total 89 96 92.7


line stmt bran cond sub pod time code
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     60   910 use strict;
  60         97  
  60         1712  
16 60     60   265 use warnings;
  60         100  
  60         1764  
17             package MongoDB::WriteConcern;
18              
19             # ABSTRACT: Encapsulate and validate a write concern
20              
21 60     60   648 use version;
  60         1840  
  60         298  
22             our $VERSION = 'v2.2.2';
23              
24 60     60   4408 use Moo;
  60         9922  
  60         270  
25 60     60   19336 use MongoDB::Error;
  60         138  
  60         5695  
26 60         387 use MongoDB::_Types qw(
27             Boolish
28 60     60   360 );
  60         100  
29 60         325 use Types::Standard qw(
30             ArrayRef
31             Int
32             Str
33             Maybe
34 60     60   53775 );
  60         111  
35 60     60   54242 use Scalar::Util qw/looks_like_number/;
  60         160  
  60         2658  
36 60     60   317 use boolean;
  60         102  
  60         440  
37 60     60   3497 use namespace::clean -except => 'meta';
  60         96  
  60         357  
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             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             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: 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             has j => (
76             is => 'ro',
77             isa => Boolish,
78             );
79              
80             has _is_acknowledged => (
81             is => 'lazy',
82             isa => Boolish,
83             reader => 'is_acknowledged',
84             builder => '_build_is_acknowledged',
85             );
86              
87             has _as_args => (
88             is => 'lazy',
89             isa => ArrayRef,
90             reader => 'as_args',
91             builder => '_build_as_args',
92             );
93              
94             sub _build_is_acknowledged {
95 0     0   0 my ($self) = @_;
96 0   0     0 return !!( $self->j || $self->_w_is_acknowledged );
97             }
98              
99             sub _build_as_args {
100 21     21   905 my ($self) = @_;
101              
102 21 100       132 my $wc = {
    100          
    100          
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 100       398 return ( keys %$wc ? [writeConcern => $wc] : [] );
109             }
110              
111             sub BUILD {
112 200     200 0 239032 my ($self) = @_;
113 200 100       672 if ( ! $self->_w_is_valid ) {
114 1         9 MongoDB::UsageError->throw("can't use write concern w=" . $self->w );
115             }
116              
117 199 100 100     589 if ( ! $self->_w_is_acknowledged && $self->j ) {
118 3         41 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 100 100     1193 if ( defined($self->wtimeout) && $self->wtimeout < 0 ) {
123 1         7 MongoDB::UsageError->throw("wtimeout must be non negative");
124             }
125 195         3464 return;
126             }
127              
128             sub _w_is_valid {
129 200     200   434 my ($self) = @_;
130 200 100       1079 return 1 if !defined $self->w;
131 20 100       108 return looks_like_number( $self->w ) ? $self->w >= 0 : length $self->w;
132             }
133              
134             sub _w_is_acknowledged {
135 199     199   451 my ($self) = @_;
136 199 100       1437 return 1 if !defined $self->w;
137 19 100       132 return looks_like_number( $self->w ) ? $self->w > 0 : length $self->w;
138             }
139              
140              
141             1;
142              
143             __END__