File Coverage

blib/lib/MongoDB/_TransactionOptions.pm
Criterion Covered Total %
statement 33 61 54.1
branch 0 8 0.0
condition 0 17 0.0
subroutine 11 15 73.3
pod n/a
total 44 101 43.5


line stmt bran cond sub pod time code
1             # Copyright 2018 - 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     59   418 use strict;
  59         146  
  59         1831  
16 59     59   308 use warnings;
  59         123  
  59         2086  
17             package MongoDB::_TransactionOptions;
18              
19             # MongoDB options for transactions
20              
21 59     59   319 use version;
  59         126  
  59         433  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4924 use MongoDB::Error;
  59         128  
  59         5155  
25              
26 59     59   360 use Moo;
  59         122  
  59         1706  
27 59     59   38975 use MongoDB::ReadConcern;
  59         208  
  59         4083  
28 59     59   24998 use MongoDB::WriteConcern;
  59         179  
  59         1872  
29 59     59   27130 use MongoDB::ReadPreference;
  59         178  
  59         2382  
30 59         379 use MongoDB::_Types qw(
31             MongoDBClient
32             WriteConcern
33             ReadConcern
34             ReadPreference
35 59     59   445 );
  59         147  
36 59         756 use Types::Standard qw(
37             HashRef
38             Any
39             Maybe
40             Int
41 59     59   55271 );
  59         147  
42 59     59   62252 use namespace::clean -except => 'meta';
  59         136  
  59         304  
43              
44             # Options provided during start transaction
45             has options => (
46             is => 'ro',
47             required => 1,
48             isa => HashRef,
49             );
50              
51             # Options provided during start session
52             has default_options => (
53             is => 'ro',
54             required => 1,
55             isa => HashRef,
56             );
57              
58             # needed for defaults
59             has client => (
60             is => 'ro',
61             required => 1,
62             isa => MongoDBClient,
63             );
64              
65             has write_concern => (
66             # must error on start_transaction, so is built immediately
67             is => 'ro',
68             isa => WriteConcern,
69             init_arg => undef,
70             builder => '_build_write_concern',
71             );
72              
73             sub _build_write_concern {
74 0     0     my $self = shift;
75              
76 0           my $options = $self->options->{writeConcern};
77 0   0       $options ||= $self->default_options->{writeConcern};
78 0   0       $options ||= {};
79              
80             # Merge in client default to pass through forced undef wtimeout etc.
81 0           $options = {
82             $self->client->_write_concern_options,
83             %$options,
84             };
85 0           my $write_concern = MongoDB::WriteConcern->new( $options );
86              
87 0 0         unless ( $write_concern->is_acknowledged ) {
88 0           MongoDB::ConfigurationError->throw(
89             'transactions do not support unacknowledged write concerns' );
90             }
91              
92 0           return $write_concern;
93             }
94              
95             has read_concern => (
96             is => 'lazy',
97             isa => ReadConcern,
98             init_arg => undef,
99             builder => '_build_read_concern',
100             );
101              
102             # Read concern errors are returned by the database, so no need to check for
103             # errors
104             sub _build_read_concern {
105 0     0     my $self = shift;
106              
107 0           my $options = $self->options->{readConcern};
108 0   0       $options ||= $self->default_options->{readConcern};
109              
110 0 0         return MongoDB::ReadConcern->new( $options ) if defined $options;
111 0           return $self->client->read_concern;
112             }
113              
114             has read_preference => (
115             is => 'lazy',
116             isa => ReadPreference,
117             init_arg => undef,
118             builder => '_build_read_preference',
119             );
120              
121             # Read preferences must be primary at present, so check after building it
122             sub _build_read_preference {
123 0     0     my $self = shift;
124              
125 0           my $options = $self->options->{readPreference};
126 0   0       $options ||= $self->default_options->{readPreference};
127              
128 0           my $read_pref;
129 0 0         $read_pref = MongoDB::ReadPreference->new( $options ) if defined $options;
130 0   0       $read_pref ||= $self->client->read_preference;
131              
132 0 0         if ( $read_pref->mode ne 'primary' ) {
133 0           MongoDB::ConfigurationError->throw(
134             "read preference in a transaction must be primary" );
135             }
136              
137 0           return $read_pref;
138             }
139              
140             has max_commit_time_ms => (
141             is => 'lazy',
142             isa => Maybe[Int],
143             init_arg => undef,
144             builder => '_build_max_commit_time_ms',
145             );
146              
147             sub _build_max_commit_time_ms {
148 0     0     my $self = shift;
149 0           my ( $opts, $default_opts ) = ( $self->options, $self->default_options );
150 0           my $max_time_ms = $opts->{'maxCommitTimeMS'};
151 0   0       $max_time_ms ||= $default_opts->{'maxCommitTimeMS'};
152 0           return $max_time_ms;
153             }
154              
155             1;