Class: Mongo::Operation::Context Private
- Inherits:
-
CsotTimeoutHolder
- Object
- CsotTimeoutHolder
- Mongo::Operation::Context
- Defined in:
- lib/mongo/operation/context.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Context for operations.
Holds various objects needed to make decisions about operation execution in a single container, and provides facade methods for the contained objects.
The context contains parameters for operations, and as such while an operation is being prepared nothing in the context should change. When the result of the operation is being processed, the data returned by the context may change (for example, because a transaction is aborted), but at that point the operation should no longer read anything from the context. Because context data may change during operation execution, context objects should not be reused for multiple operations.
Instance Attribute Summary collapse
- #client ⇒ Object readonly private
- #options ⇒ Object readonly private
- #session ⇒ Object readonly private
- #view ⇒ Object readonly private
Attributes inherited from CsotTimeoutHolder
#deadline, #operation_timeouts, #timeout_sec
Instance Method Summary collapse
- #aborting_transaction? ⇒ Boolean private
- #any_retry_writes? ⇒ Boolean private
- #committing_transaction? ⇒ Boolean private
- #connection_global_id ⇒ Object private
- #decrypt(cmd) ⇒ Object private
- #decrypt? ⇒ Boolean private
- #encrypt(db_name, cmd) ⇒ Object private
- #encrypt? ⇒ Boolean private
- #encrypter ⇒ Object private
- #in_transaction? ⇒ Boolean private
-
#initialize(client: nil, session: nil, connection_global_id: nil, operation_timeouts: {}, view: nil, options: nil) ⇒ Context
constructor
private
A new instance of Context.
- #inspect ⇒ Object private
- #legacy_retry_writes? ⇒ Boolean private
- #modern_retry_writes? ⇒ Boolean private
-
#refresh(connection_global_id: @connection_global_id, timeout_ms: nil, view: nil) ⇒ Operation::Context
private
Returns a new Operation::Context with the deadline refreshed and relative to the current moment.
-
#retry? ⇒ Boolean
private
Whether the operation is a retry (true) or an initial attempt (false).
- #server_api ⇒ Object private
- #starting_transaction? ⇒ Boolean private
-
#with(**opts) ⇒ Object
private
Returns a new context with the parameters changed as per the provided arguments.
Methods inherited from CsotTimeoutHolder
#check_timeout!, #csot?, #remaining_timeout_ms, #remaining_timeout_ms!, #remaining_timeout_sec, #remaining_timeout_sec!, #timeout?, #timeout_expired?
Constructor Details
#initialize(client: nil, session: nil, connection_global_id: nil, operation_timeouts: {}, view: nil, options: nil) ⇒ Context
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Context.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/operation/context.rb', line 38 def initialize( client: nil, session: nil, connection_global_id: nil, operation_timeouts: {}, view: nil, options: nil ) if if client raise ArgumentError, 'Client and options cannot both be specified' end if session raise ArgumentError, 'Session and options cannot both be specified' end end if connection_global_id && session&.pinned_connection_global_id raise ArgumentError, 'Trying to pin context to a connection when the session is already pinned to a connection.' end @client = client @session = session @view = view @connection_global_id = connection_global_id @options = super(session: session, operation_timeouts: operation_timeouts) end |
Instance Attribute Details
#client ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 |
# File 'lib/mongo/operation/context.rb', line 68 def client @client end |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 |
# File 'lib/mongo/operation/context.rb', line 71 def @options end |
#session ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
69 70 71 |
# File 'lib/mongo/operation/context.rb', line 69 def session @session end |
#view ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
70 71 72 |
# File 'lib/mongo/operation/context.rb', line 70 def view @view end |
Instance Method Details
#aborting_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 |
# File 'lib/mongo/operation/context.rb', line 105 def aborting_transaction? in_transaction? && session.aborting_transaction? end |
#any_retry_writes? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
117 118 119 |
# File 'lib/mongo/operation/context.rb', line 117 def any_retry_writes? modern_retry_writes? || legacy_retry_writes? end |
#committing_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
101 102 103 |
# File 'lib/mongo/operation/context.rb', line 101 def committing_transaction? in_transaction? && session.committing_transaction? end |
#connection_global_id ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
89 90 91 |
# File 'lib/mongo/operation/context.rb', line 89 def connection_global_id @connection_global_id || session&.pinned_connection_global_id end |
#decrypt(cmd) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
159 160 161 |
# File 'lib/mongo/operation/context.rb', line 159 def decrypt(cmd) encrypter.decrypt(cmd, self) end |
#decrypt? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 156 157 |
# File 'lib/mongo/operation/context.rb', line 155 def decrypt? !!client&.encrypter end |
#encrypt(db_name, cmd) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
151 152 153 |
# File 'lib/mongo/operation/context.rb', line 151 def encrypt(db_name, cmd) encrypter.encrypt(db_name, cmd, self) end |
#encrypt? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
147 148 149 |
# File 'lib/mongo/operation/context.rb', line 147 def encrypt? client&.encrypter&.encrypt? || false end |
#encrypter ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
163 164 165 166 167 168 169 |
# File 'lib/mongo/operation/context.rb', line 163 def encrypter if client&.encrypter client.encrypter else raise Error::InternalDriverError, 'Encrypter should only be accessed when encryption is to be performed' end end |
#in_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 |
# File 'lib/mongo/operation/context.rb', line 93 def in_transaction? session&.in_transaction? || false end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
171 172 173 |
# File 'lib/mongo/operation/context.rb', line 171 def inspect "#<#{self.class} connection_global_id=#{connection_global_id.inspect} deadline=#{deadline.inspect} options=#{.inspect} operation_timeouts=#{operation_timeouts.inspect}>" end |
#legacy_retry_writes? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
113 114 115 |
# File 'lib/mongo/operation/context.rb', line 113 def legacy_retry_writes? client && !client.[:retry_writes] && client.max_write_retries > 0 end |
#modern_retry_writes? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
109 110 111 |
# File 'lib/mongo/operation/context.rb', line 109 def modern_retry_writes? client && client.[:retry_writes] end |
#refresh(connection_global_id: @connection_global_id, timeout_ms: nil, view: nil) ⇒ Operation::Context
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new Operation::Context with the deadline refreshed and relative to the current moment.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mongo/operation/context.rb', line 77 def refresh(connection_global_id: @connection_global_id, timeout_ms: nil, view: nil) operation_timeouts = @operation_timeouts operation_timeouts = operation_timeouts.merge(operation_timeout_ms: timeout_ms) if timeout_ms self.class.new(client: client, session: session, connection_global_id: connection_global_id, operation_timeouts: operation_timeouts, view: view || self.view, options: ) end |
#retry? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the operation is a retry (true) or an initial attempt (false).
130 131 132 |
# File 'lib/mongo/operation/context.rb', line 130 def retry? !!@is_retry end |
#server_api ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
121 122 123 124 125 126 127 |
# File 'lib/mongo/operation/context.rb', line 121 def server_api if client client.[:server_api] elsif [:server_api] end end |
#starting_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
97 98 99 |
# File 'lib/mongo/operation/context.rb', line 97 def starting_transaction? session&.starting_transaction? || false end |
#with(**opts) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new context with the parameters changed as per the provided arguments.
139 140 141 142 143 144 145 |
# File 'lib/mongo/operation/context.rb', line 139 def with(**opts) dup.tap do |copy| opts.each do |k, v| copy.instance_variable_set("@#{k}", v) end end end |