You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

transactionoptions.go 2.6 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. package options
  7. import (
  8. "time"
  9. "go.mongodb.org/mongo-driver/mongo/readconcern"
  10. "go.mongodb.org/mongo-driver/mongo/readpref"
  11. "go.mongodb.org/mongo-driver/mongo/writeconcern"
  12. )
  13. // TransactionOptions represents all possible options for starting a transaction.
  14. type TransactionOptions struct {
  15. ReadConcern *readconcern.ReadConcern // The read concern for the transaction. Defaults to the session's read concern.
  16. ReadPreference *readpref.ReadPref // The read preference for the transaction. Defaults to the session's read preference.
  17. WriteConcern *writeconcern.WriteConcern // The write concern for the transaction. Defaults to the session's write concern.
  18. MaxCommitTime *time.Duration // The maximum amount of time to allow a single commitTransaction command to run.
  19. }
  20. // Transaction creates a new *TransactionOptions
  21. func Transaction() *TransactionOptions {
  22. return &TransactionOptions{}
  23. }
  24. // SetReadConcern sets the read concern for the transaction.
  25. func (t *TransactionOptions) SetReadConcern(rc *readconcern.ReadConcern) *TransactionOptions {
  26. t.ReadConcern = rc
  27. return t
  28. }
  29. // SetReadPreference sets the read preference for the transaction.
  30. func (t *TransactionOptions) SetReadPreference(rp *readpref.ReadPref) *TransactionOptions {
  31. t.ReadPreference = rp
  32. return t
  33. }
  34. // SetWriteConcern sets the write concern for the transaction.
  35. func (t *TransactionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *TransactionOptions {
  36. t.WriteConcern = wc
  37. return t
  38. }
  39. // SetMaxCommitTime sets the max commit time for the transaction.
  40. func (t *TransactionOptions) SetMaxCommitTime(mct *time.Duration) *TransactionOptions {
  41. t.MaxCommitTime = mct
  42. return t
  43. }
  44. // MergeTransactionOptions combines the given *TransactionOptions into a single *TransactionOptions in a last one wins
  45. // fashion.
  46. func MergeTransactionOptions(opts ...*TransactionOptions) *TransactionOptions {
  47. t := Transaction()
  48. for _, opt := range opts {
  49. if opt == nil {
  50. continue
  51. }
  52. if opt.ReadConcern != nil {
  53. t.ReadConcern = opt.ReadConcern
  54. }
  55. if opt.ReadPreference != nil {
  56. t.ReadPreference = opt.ReadPreference
  57. }
  58. if opt.WriteConcern != nil {
  59. t.WriteConcern = opt.WriteConcern
  60. }
  61. if opt.MaxCommitTime != nil {
  62. t.MaxCommitTime = opt.MaxCommitTime
  63. }
  64. }
  65. return t
  66. }