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   386 use strict;
  59         127  
  59         2618  
16 59     59   272 use warnings;
  59         110  
  59         1799  
17             package MongoDB::_TransactionOptions;
18              
19             # MongoDB options for transactions
20              
21 59     59   280 use version;
  59         103  
  59         395  
22             our $VERSION = 'v2.2.2';
23              
24 59     59   4222 use MongoDB::Error;
  59         106  
  59         4764  
25              
26 59     59   300 use Moo;
  59         108  
  59         1455  
27 59     59   33335 use MongoDB::ReadConcern;
  59         193  
  59         3650  
28 59     59   22146 use MongoDB::WriteConcern;
  59         164  
  59         1907  
29 59     59   25615 use MongoDB::ReadPreference;
  59         171  
  59         2279  
30 59         254 use MongoDB::_Types qw(
31             MongoDBClient
32             WriteConcern
33             ReadConcern
34             ReadPreference
35 59     59   368 );
  59         117  
36 59         367 use Types::Standard qw(
37             HashRef
38             Any
39             Maybe
40             Int
41 59     59   47944 );
  59         132  
42 59     59   54381 use namespace::clean -except => 'meta';
  59         129  
  59         284  
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;