Class: Mongo::Operation::Context Private

Inherits:
CsotTimeoutHolder show all
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

Attributes inherited from CsotTimeoutHolder

#deadline, #operation_timeouts, #timeout_sec

Instance Method Summary collapse

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mongo/operation/context.rb', line 36

def initialize(
  client: nil,
  session: nil,
  connection_global_id: nil,
  operation_timeouts: {},
  view: nil,
  options: nil
)
  if options
    raise ArgumentError, 'Client and options cannot both be specified' if client

    raise ArgumentError, 'Session and options cannot both be specified' if session
  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 = options
  super(session: session, operation_timeouts: operation_timeouts)
end

Instance Attribute Details

#clientObject (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.



63
64
65
# File 'lib/mongo/operation/context.rb', line 63

def client
  @client
end

#optionsObject (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.



63
64
65
# File 'lib/mongo/operation/context.rb', line 63

def options
  @options
end

#sessionObject (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.



63
64
65
# File 'lib/mongo/operation/context.rb', line 63

def session
  @session
end

#viewObject (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.



63
64
65
# File 'lib/mongo/operation/context.rb', line 63

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.

Returns:

  • (Boolean)


97
98
99
# File 'lib/mongo/operation/context.rb', line 97

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.

Returns:

  • (Boolean)


109
110
111
# File 'lib/mongo/operation/context.rb', line 109

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.

Returns:

  • (Boolean)


93
94
95
# File 'lib/mongo/operation/context.rb', line 93

def committing_transaction?
  in_transaction? && session.committing_transaction?
end

#connection_global_idObject

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.



81
82
83
# File 'lib/mongo/operation/context.rb', line 81

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.



156
157
158
# File 'lib/mongo/operation/context.rb', line 156

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.

Returns:

  • (Boolean)


152
153
154
# File 'lib/mongo/operation/context.rb', line 152

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.



148
149
150
# File 'lib/mongo/operation/context.rb', line 148

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.

Returns:

  • (Boolean)


144
145
146
# File 'lib/mongo/operation/context.rb', line 144

def encrypt?
  client&.encrypter&.encrypt? || false
end

#encrypterObject

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.



160
161
162
163
164
165
166
# File 'lib/mongo/operation/context.rb', line 160

def encrypter
  unless client&.encrypter
    raise Error::InternalDriverError, 'Encrypter should only be accessed when encryption is to be performed'
  end

  client.encrypter
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.

Returns:

  • (Boolean)


85
86
87
# File 'lib/mongo/operation/context.rb', line 85

def in_transaction?
  session&.in_transaction? || false
end

#inspectObject

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.



168
169
170
# File 'lib/mongo/operation/context.rb', line 168

def inspect
  "#<#{self.class} connection_global_id=#{connection_global_id.inspect} deadline=#{deadline.inspect} options=#{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.

Returns:

  • (Boolean)


105
106
107
# File 'lib/mongo/operation/context.rb', line 105

def legacy_retry_writes?
  client && !client.options[: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.

Returns:

  • (Boolean)


101
102
103
# File 'lib/mongo/operation/context.rb', line 101

def modern_retry_writes?
  client && client.options[:retry_writes]
end

#overload_only_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 every retry so far has been due to overload only.

Returns:

  • (Boolean)


127
128
129
# File 'lib/mongo/operation/context.rb', line 127

def overload_only_retry?
  !!@overload_only_retry
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.

Returns:



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mongo/operation/context.rb', line 69

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: 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).

Returns:

  • (Boolean)


122
123
124
# File 'lib/mongo/operation/context.rb', line 122

def retry?
  !!@is_retry
end

#server_apiObject

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
116
117
118
119
# File 'lib/mongo/operation/context.rb', line 113

def server_api
  if client
    client.options[:server_api]
  elsif options
    options[: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.

Returns:

  • (Boolean)


89
90
91
# File 'lib/mongo/operation/context.rb', line 89

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.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :is_retry (true|false)

    Whether the operation is a retry or a first attempt.



136
137
138
139
140
141
142
# File 'lib/mongo/operation/context.rb', line 136

def with(**opts)
  dup.tap do |copy|
    opts.each do |k, v|
      copy.instance_variable_set("@#{k}", v)
    end
  end
end