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 all possible options for creating a new session.
21type SessionOptions struct {
22	CausalConsistency     *bool                      // Specifies if reads should be causally consistent. Defaults to true.
23	DefaultReadConcern    *readconcern.ReadConcern   // The default read concern for transactions started in the session.
24	DefaultReadPreference *readpref.ReadPref         // The default read preference for transactions started in the session.
25	DefaultWriteConcern   *writeconcern.WriteConcern // The default write concern for transactions started in the session.
26	DefaultMaxCommitTime  *time.Duration             // The default max commit time for transactions started in the session.
27}
28
29// Session creates a new *SessionOptions
30func Session() *SessionOptions {
31	return &SessionOptions{
32		CausalConsistency: &DefaultCausalConsistency,
33	}
34}
35
36// SetCausalConsistency specifies if a session should be causally consistent. Defaults to true.
37func (s *SessionOptions) SetCausalConsistency(b bool) *SessionOptions {
38	s.CausalConsistency = &b
39	return s
40}
41
42// SetDefaultReadConcern sets the default read concern for transactions started in a session.
43func (s *SessionOptions) SetDefaultReadConcern(rc *readconcern.ReadConcern) *SessionOptions {
44	s.DefaultReadConcern = rc
45	return s
46}
47
48// SetDefaultReadPreference sets the default read preference for transactions started in a session.
49func (s *SessionOptions) SetDefaultReadPreference(rp *readpref.ReadPref) *SessionOptions {
50	s.DefaultReadPreference = rp
51	return s
52}
53
54// SetDefaultWriteConcern sets the default write concern for transactions started in a session.
55func (s *SessionOptions) SetDefaultWriteConcern(wc *writeconcern.WriteConcern) *SessionOptions {
56	s.DefaultWriteConcern = wc
57	return s
58}
59
60// SetDefaultMaxCommitTime sets the default max commit time for transactions started in a session.
61func (s *SessionOptions) SetDefaultMaxCommitTime(mct *time.Duration) *SessionOptions {
62	s.DefaultMaxCommitTime = mct
63	return s
64}
65
66// MergeSessionOptions combines the given *SessionOptions into a single *SessionOptions in a last one wins fashion.
67func MergeSessionOptions(opts ...*SessionOptions) *SessionOptions {
68	s := Session()
69	for _, opt := range opts {
70		if opt == nil {
71			continue
72		}
73		if opt.CausalConsistency != nil {
74			s.CausalConsistency = opt.CausalConsistency
75		}
76		if opt.DefaultReadConcern != nil {
77			s.DefaultReadConcern = opt.DefaultReadConcern
78		}
79		if opt.DefaultReadPreference != nil {
80			s.DefaultReadPreference = opt.DefaultReadPreference
81		}
82		if opt.DefaultWriteConcern != nil {
83			s.DefaultWriteConcern = opt.DefaultWriteConcern
84		}
85		if opt.DefaultMaxCommitTime != nil {
86			s.DefaultMaxCommitTime = opt.DefaultMaxCommitTime
87		}
88	}
89
90	return s
91}
92