1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package options
8
9import (
10	"time"
11
12	"go.mongodb.org/mongo-driver/mongo/readconcern"
13	"go.mongodb.org/mongo-driver/mongo/readpref"
14	"go.mongodb.org/mongo-driver/mongo/writeconcern"
15)
16
17// DefaultCausalConsistency is the default value for the CausalConsistency option.
18var DefaultCausalConsistency = true
19
20// SessionOptions represents options that can be used to configure a Session.
21type SessionOptions struct {
22	// If true, causal consistency will be enabled for the session. The default value is true. See
23	// https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#sessions for more information.
24	CausalConsistency *bool
25
26	// The default read concern for transactions started in the session. The default value is nil, which means that
27	// the read concern of the client used to start the session will be used.
28	DefaultReadConcern *readconcern.ReadConcern
29
30	// The default read preference for transactions started in the session. The default value is nil, which means that
31	// the read preference of the client used to start the session will be used.
32	DefaultReadPreference *readpref.ReadPref
33
34	// The default write concern for transactions started in the session. The default value is nil, which means that
35	// the write concern of the client used to start the session will be used.
36	DefaultWriteConcern *writeconcern.WriteConcern
37
38	// The default maximum amount of time that a CommitTransaction operation executed in the session can run on the
39	// server. The default value is nil, which means that that there is no time limit for execution.
40	DefaultMaxCommitTime *time.Duration
41}
42
43// Session creates a new SessionOptions instance.
44func Session() *SessionOptions {
45	return &SessionOptions{
46		CausalConsistency: &DefaultCausalConsistency,
47	}
48}
49
50// SetCausalConsistency sets the value for the CausalConsistency field.
51func (s *SessionOptions) SetCausalConsistency(b bool) *SessionOptions {
52	s.CausalConsistency = &b
53	return s
54}
55
56// SetDefaultReadConcern sets the value for the DefaultReadConcern field.
57func (s *SessionOptions) SetDefaultReadConcern(rc *readconcern.ReadConcern) *SessionOptions {
58	s.DefaultReadConcern = rc
59	return s
60}
61
62// SetDefaultReadPreference sets the value for the DefaultReadPreference field.
63func (s *SessionOptions) SetDefaultReadPreference(rp *readpref.ReadPref) *SessionOptions {
64	s.DefaultReadPreference = rp
65	return s
66}
67
68// SetDefaultWriteConcern sets the value for the DefaultWriteConcern field.
69func (s *SessionOptions) SetDefaultWriteConcern(wc *writeconcern.WriteConcern) *SessionOptions {
70	s.DefaultWriteConcern = wc
71	return s
72}
73
74// SetDefaultMaxCommitTime sets the value for the DefaultMaxCommitTime field.
75func (s *SessionOptions) SetDefaultMaxCommitTime(mct *time.Duration) *SessionOptions {
76	s.DefaultMaxCommitTime = mct
77	return s
78}
79
80// MergeSessionOptions combines the given SessionOptions instances into a single SessionOptions in a last-one-wins
81// fashion.
82func MergeSessionOptions(opts ...*SessionOptions) *SessionOptions {
83	s := Session()
84	for _, opt := range opts {
85		if opt == nil {
86			continue
87		}
88		if opt.CausalConsistency != nil {
89			s.CausalConsistency = opt.CausalConsistency
90		}
91		if opt.DefaultReadConcern != nil {
92			s.DefaultReadConcern = opt.DefaultReadConcern
93		}
94		if opt.DefaultReadPreference != nil {
95			s.DefaultReadPreference = opt.DefaultReadPreference
96		}
97		if opt.DefaultWriteConcern != nil {
98			s.DefaultWriteConcern = opt.DefaultWriteConcern
99		}
100		if opt.DefaultMaxCommitTime != nil {
101			s.DefaultMaxCommitTime = opt.DefaultMaxCommitTime
102		}
103	}
104
105	return s
106}
107