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 58     58   432 use strict;
  58         138  
  58         1934  
16 58     58   319 use warnings;
  58         140  
  58         2069  
17             package MongoDB::_TransactionOptions;
18              
19             # MongoDB options for transactions
20              
21 58     58   346 use version;
  58         117  
  58         462  
22             our $VERSION = 'v2.2.0';
23              
24 58     58   4869 use MongoDB::Error;
  58         133  
  58         5058  
25              
26 58     58   346 use Moo;
  58         122  
  58         1707  
27 58     58   36811 use MongoDB::ReadConcern;
  58         200  
  58         4153  
28 58     58   23879 use MongoDB::WriteConcern;
  58         173  
  58         1861  
29 58     58   43614 use MongoDB::ReadPreference;
  58         172  
  58         2419  
30 58         274 use MongoDB::_Types qw(
31             MongoDBClient
32             WriteConcern
33             ReadConcern
34             ReadPreference
35 58     58   431 );
  58         121  
36 58         396 use Types::Standard qw(
37             HashRef
38             Any
39             Maybe
40             Int
41 58     58   53210 );
  58         131  
42 58     58   60901 use namespace::clean -except => 'meta';
  58         134  
  58         322  
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;