class Mongo::Operation::Context

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.

@api private

Attributes

client[R]
options[R]
session[R]

Public Class Methods

new(client: nil, session: nil, connection_global_id: nil, options: nil) click to toggle source
# File lib/mongo/operation/context.rb, line 37
def initialize(client: nil, session: nil, connection_global_id: 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
  @connection_global_id = connection_global_id
  @options = options
end

Public Instance Methods

aborting_transaction?() click to toggle source
# File lib/mongo/operation/context.rb, line 78
def aborting_transaction?
  in_transaction? && session.aborting_transaction?
end
any_retry_writes?() click to toggle source
# File lib/mongo/operation/context.rb, line 90
def any_retry_writes?
  modern_retry_writes? || legacy_retry_writes?
end
committing_transaction?() click to toggle source
# File lib/mongo/operation/context.rb, line 74
def committing_transaction?
  in_transaction? && session.committing_transaction?
end
connection_global_id() click to toggle source
# File lib/mongo/operation/context.rb, line 62
def connection_global_id
  @connection_global_id || session&.pinned_connection_global_id
end
decrypt?() click to toggle source
# File lib/mongo/operation/context.rb, line 124
def decrypt?
  !!client&.encrypter
end
encrypt?() click to toggle source
# File lib/mongo/operation/context.rb, line 120
def encrypt?
  client&.encrypter&.encrypt? || false
end
encrypter() click to toggle source
# File lib/mongo/operation/context.rb, line 128
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?() click to toggle source
# File lib/mongo/operation/context.rb, line 66
def in_transaction?
  session&.in_transaction? || false
end
legacy_retry_writes?() click to toggle source
# File lib/mongo/operation/context.rb, line 86
def legacy_retry_writes?
  client && !client.options[:retry_writes] && client.max_write_retries > 0
end
modern_retry_writes?() click to toggle source
# File lib/mongo/operation/context.rb, line 82
def modern_retry_writes?
  client && client.options[:retry_writes]
end
retry?() click to toggle source

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

# File lib/mongo/operation/context.rb, line 103
def retry?
  !!@is_retry
end
server_api() click to toggle source
# File lib/mongo/operation/context.rb, line 94
def server_api
  if client
    client.options[:server_api]
  elsif options
    options[:server_api]
  end
end
starting_transaction?() click to toggle source
# File lib/mongo/operation/context.rb, line 70
def starting_transaction?
  session&.starting_transaction? || false
end
with(**opts) click to toggle source

Returns a new context with the parameters changed as per the provided arguments.

@option opts [ true|false ] :is_retry Whether the operation is a retry

or a first attempt.
# File lib/mongo/operation/context.rb, line 112
def with(**opts)
  dup.tap do |copy|
    opts.each do |k, v|
      copy.instance_variable_set("@#{k}", v)
    end
  end
end