123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Operation::Context Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongo::CsotTimeoutHolder
Defined in: lib/mongo/operation/context.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

::Mongo::CsotTimeoutHolder - Inherited

Instance Method Summary

::Mongo::CsotTimeoutHolder - Inherited

Instance Attribute Details

#aborting_transaction?Boolean (readonly)

[ GitHub ]

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

def aborting_transaction?
  in_transaction? && session.aborting_transaction?
end

#any_retry_writes?Boolean (readonly)

[ GitHub ]

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

def any_retry_writes?
  modern_retry_writes? || legacy_retry_writes?
end

#client (readonly)

[ GitHub ]

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

attr_reader :client, :session, :view, :options

#committing_transaction?Boolean (readonly)

[ GitHub ]

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

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

#decrypt?Boolean (readonly)

[ GitHub ]

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

def decrypt?
  !!client&.encrypter
end

#encrypt?Boolean (readonly)

[ GitHub ]

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

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

#in_transaction?Boolean (readonly)

[ GitHub ]

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

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

#legacy_retry_writes?Boolean (readonly)

[ GitHub ]

  
# 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 (readonly)

[ GitHub ]

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

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

#options (readonly)

[ GitHub ]

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

attr_reader :client, :session, :view, :options

#overload_only_retry?Boolean (readonly)

Whether every retry so far has been due to overload only.

[ GitHub ]

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

def overload_only_retry?
  !!@overload_only_retry
end

#retry?Boolean (readonly)

Whether the operation is a retry (true) or an initial attempt (false).

[ GitHub ]

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

def retry?
  !!@is_retry
end

#session (readonly)

[ GitHub ]

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

attr_reader :client, :session, :view, :options

#starting_transaction?Boolean (readonly)

[ GitHub ]

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

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

#view (readonly)

[ GitHub ]

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

attr_reader :client, :session, :view, :options

Instance Method Details

#connection_global_id

[ GitHub ]

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

def connection_global_id
  @connection_global_id || session&.pinned_connection_global_id
end

#decrypt(cmd) (readonly)

[ GitHub ]

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

def decrypt(cmd)
  encrypter.decrypt(cmd, self)
end

#encrypt(db_name, cmd) (readonly)

[ GitHub ]

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

def encrypt(db_name, cmd)
  encrypter.encrypt(db_name, cmd, self)
end

#encrypter

[ GitHub ]

  
# 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

#inspect

[ GitHub ]

  
# 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

#refresh(connection_global_id: @connection_global_id, timeout_ms: nil, view: nil) ⇒ Operation::Context

Returns a new Context with the deadline refreshed and relative to the current moment.

Returns:

[ GitHub ]

  
# 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

#server_api

[ GitHub ]

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

def server_api
  if client
    client.options[:server_api]
  elsif options
    options[:server_api]
  end
end

#with(**opts)

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.

[ GitHub ]

  
# 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