blob: ffe45e6e60d0915f3f29ee5ec795c9eef4dd0ea4 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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 "github.com/mongodb/mongo-go-driver/mongo/readconcern"
11 "github.com/mongodb/mongo-go-driver/mongo/readpref"
12 "github.com/mongodb/mongo-go-driver/mongo/writeconcern"
13)
14
15// DefaultCausalConsistency is the default value for the CausalConsistency option.
16var DefaultCausalConsistency = true
17
18// SessionOptions represents all possible options for creating a new session.
19type SessionOptions struct {
20 CausalConsistency *bool // Specifies if reads should be causally consistent. Defaults to true.
21 DefaultReadConcern *readconcern.ReadConcern // The default read concern for transactions started in the session.
22 DefaultReadPreference *readpref.ReadPref // The default read preference for transactions started in the session.
23 DefaultWriteConcern *writeconcern.WriteConcern // The default write concern for transactions started in the session.
24}
25
26// Session creates a new *SessionOptions
27func Session() *SessionOptions {
28 return &SessionOptions{
29 CausalConsistency: &DefaultCausalConsistency,
30 }
31}
32
33// SetCausalConsistency specifies if a session should be causally consistent. Defaults to true.
34func (s *SessionOptions) SetCausalConsistency(b bool) *SessionOptions {
35 s.CausalConsistency = &b
36 return s
37}
38
39// SetDefaultReadConcern sets the default read concern for transactions started in a session.
40func (s *SessionOptions) SetDefaultReadConcern(rc *readconcern.ReadConcern) *SessionOptions {
41 s.DefaultReadConcern = rc
42 return s
43}
44
45// SetDefaultReadPreference sets the default read preference for transactions started in a session.
46func (s *SessionOptions) SetDefaultReadPreference(rp *readpref.ReadPref) *SessionOptions {
47 s.DefaultReadPreference = rp
48 return s
49}
50
51// SetDefaultWriteConcern sets the default write concern for transactions started in a session.
52func (s *SessionOptions) SetDefaultWriteConcern(wc *writeconcern.WriteConcern) *SessionOptions {
53 s.DefaultWriteConcern = wc
54 return s
55}
56
57// MergeSessionOptions combines the given *SessionOptions into a single *SessionOptions in a last one wins fashion.
58func MergeSessionOptions(opts ...*SessionOptions) *SessionOptions {
59 s := Session()
60 for _, opt := range opts {
61 if opt == nil {
62 continue
63 }
64 if opt.CausalConsistency != nil {
65 s.CausalConsistency = opt.CausalConsistency
66 }
67 if opt.DefaultReadConcern != nil {
68 s.DefaultReadConcern = opt.DefaultReadConcern
69 }
70 if opt.DefaultReadPreference != nil {
71 s.DefaultReadPreference = opt.DefaultReadPreference
72 }
73 if opt.DefaultWriteConcern != nil {
74 s.DefaultWriteConcern = opt.DefaultWriteConcern
75 }
76 }
77
78 return s
79}