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.



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 options
    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 = 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.



68
69
70
# File 'lib/mongo/operation/context.rb', line 68

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.



71
72
73
# File 'lib/mongo/operation/context.rb', line 71

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.



69
70
71
# File 'lib/mongo/operation/context.rb', line 69

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.



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

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.



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

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.



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.

Returns:

  • (Boolean)


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

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.



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=#{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)


113
114
115
# File 'lib/mongo/operation/context.rb', line 113

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)


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

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

Returns:



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


130
131
132
# File 'lib/mongo/operation/context.rb', line 130

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.



121
122
123
124
125
126
127
# File 'lib/mongo/operation/context.rb', line 121

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)


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.

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.



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