class Mongo::Protocol::Query::Upconverter

Converts legacy query messages to the appropriare OP_COMMAND style message.

@since 2.1.0

Constants

FLAG_MAPPINGS

Mapping of flags to find command options.

@since 2.1.0

OPTION_MAPPINGS

Mappings of the options to the find command options.

@since 2.1.0

SPECIAL_FIELD_MAPPINGS

Attributes

collection[R]

@return [ String ] collection The name of the collection.

filter[R]

@return [ BSON::Document, Hash ] filter The query filter or command.

flags[R]

@return [ Array<Symbol> ] flags The flags.

options[R]

@return [ BSON::Document, Hash ] options The options.

Public Class Methods

new(collection, filter, options, flags) click to toggle source

Instantiate the upconverter.

@example Instantiate the upconverter.

Upconverter.new('users', { name: 'test' }, { skip: 10 })

@param [ String ] collection The name of the collection. @param [ BSON::Document, Hash ] filter The filter or command. @param [ BSON::Document, Hash ] options The options. @param [ Array<Symbol> ] flags The flags.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 282
def initialize(collection, filter, options, flags)
  # Although the docstring claims both hashes and BSON::Documents
  # are acceptable, this class expects the filter and options to
  # contain symbol keys which isn't what the operation layer produces.
  unless BSON::Document === filter
    raise ArgumentError, 'Filter must provide indifferent access'
  end
  unless BSON::Document === options
    raise ArgumentError, 'Options must provide indifferent access'
  end
  @collection = collection
  @filter = filter
  @options = options
  @flags = flags
end

Public Instance Methods

command() click to toggle source

Get the upconverted command.

@example Get the command.

upconverter.command

@return [ BSON::Document ] The upconverted command.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 306
def command
  command? ? op_command : find_command
end
command_name() click to toggle source

Get the name of the command. If the collection is $cmd then it's the first key in the filter, otherwise it's a find.

@example Get the command name.

upconverter.command_name

@return [ String ] The command name.

@since 2.1.0

# File lib/mongo/protocol/query.rb, line 319
def command_name
  ((filter[:$query] || !command?) ? :find : filter.keys.first).to_s
end

Private Instance Methods

command?() click to toggle source
# File lib/mongo/protocol/query.rb, line 325
def command?
  collection == Database::COMMAND
end
find_command() click to toggle source
# File lib/mongo/protocol/query.rb, line 341
def find_command
  document = BSON::Document.new(
    find: collection,
    filter: query_filter,
  )
  OPTION_MAPPINGS.each do |legacy, option|
    document.store(option, options[legacy]) unless options[legacy].nil?
  end
  if Lint.enabled?
    filter.each do |k, v|
      unless String === k
        raise Error::LintError, "All keys in filter must be strings: #{filter.inspect}"
      end
    end
  end
  Lint.validate_camel_case_read_preference(filter['readPreference'])
  SPECIAL_FIELD_MAPPINGS.each do |special, normal|
    unless (v = filter[special]).nil?
      document.store(normal, v)
    end
  end
  FLAG_MAPPINGS.each do |legacy, flag|
    document.store(flag, true) if flags.include?(legacy)
  end
  document
end
op_command() click to toggle source
# File lib/mongo/protocol/query.rb, line 333
def op_command
  document = BSON::Document.new
  query_filter.each do |field, value|
    document.store(field.to_s, value)
  end
  document
end
query_filter() click to toggle source
# File lib/mongo/protocol/query.rb, line 329
def query_filter
  filter[:$query] || filter
end