github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/transactionoptions.go (about) 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 7 package options 8 9 import ( 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 // TransactionOptions represents options that can be used to configure a transaction. 18 type TransactionOptions struct { 19 // The read concern for operations in the transaction. The default value is nil, which means that the default 20 // read concern of the session used to start the transaction will be used. 21 ReadConcern *readconcern.ReadConcern 22 23 // The read preference for operations in the transaction. The default value is nil, which means that the default 24 // read preference of the session used to start the transaction will be used. 25 ReadPreference *readpref.ReadPref 26 27 // The write concern for operations in the transaction. The default value is nil, which means that the default 28 // write concern of the session used to start the transaction will be used. 29 WriteConcern *writeconcern.WriteConcern 30 31 // The default maximum amount of time that a CommitTransaction operation executed in the session can run on the 32 // server. The default value is nil, meaning that there is no time limit for execution. 33 34 // The maximum amount of time that a CommitTransaction operation can executed in the transaction can run on the 35 // server. The default value is nil, which means that the default maximum commit time of the session used to 36 // start the transaction will be used. 37 // 38 // NOTE(benjirewis): MaxCommitTime will be deprecated in a future release. The more general Timeout option may 39 // be used in its place to control the amount of time that a single operation can run before returning an error. 40 // MaxCommitTime is ignored if Timeout is set on the client. 41 MaxCommitTime *time.Duration 42 } 43 44 // Transaction creates a new TransactionOptions instance. 45 func Transaction() *TransactionOptions { 46 return &TransactionOptions{} 47 } 48 49 // SetReadConcern sets the value for the ReadConcern field. 50 func (t *TransactionOptions) SetReadConcern(rc *readconcern.ReadConcern) *TransactionOptions { 51 t.ReadConcern = rc 52 return t 53 } 54 55 // SetReadPreference sets the value for the ReadPreference field. 56 func (t *TransactionOptions) SetReadPreference(rp *readpref.ReadPref) *TransactionOptions { 57 t.ReadPreference = rp 58 return t 59 } 60 61 // SetWriteConcern sets the value for the WriteConcern field. 62 func (t *TransactionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *TransactionOptions { 63 t.WriteConcern = wc 64 return t 65 } 66 67 // SetMaxCommitTime sets the value for the MaxCommitTime field. 68 // 69 // NOTE(benjirewis): MaxCommitTime will be deprecated in a future release. The more general Timeout 70 // option may be used in its place to control the amount of time that a single operation can run before 71 // returning an error. MaxCommitTime is ignored if Timeout is set on the client. 72 func (t *TransactionOptions) SetMaxCommitTime(mct *time.Duration) *TransactionOptions { 73 t.MaxCommitTime = mct 74 return t 75 } 76 77 // MergeTransactionOptions combines the given TransactionOptions instances into a single TransactionOptions in a 78 // last-one-wins fashion. 79 // 80 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a 81 // single options struct instead. 82 func MergeTransactionOptions(opts ...*TransactionOptions) *TransactionOptions { 83 t := Transaction() 84 for _, opt := range opts { 85 if opt == nil { 86 continue 87 } 88 if opt.ReadConcern != nil { 89 t.ReadConcern = opt.ReadConcern 90 } 91 if opt.ReadPreference != nil { 92 t.ReadPreference = opt.ReadPreference 93 } 94 if opt.WriteConcern != nil { 95 t.WriteConcern = opt.WriteConcern 96 } 97 if opt.MaxCommitTime != nil { 98 t.MaxCommitTime = opt.MaxCommitTime 99 } 100 } 101 102 return t 103 }