Class: Mongo::Protocol::Query::Upconverter
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/mongo/protocol/query.rb |
Overview
Converts legacy query messages to the appropriare OP_COMMAND style message.
Constant Summary
-
FLAG_MAPPINGS =
Mapping of flags to find command options.
{ :tailable_cursor => 'tailable', :oplog_replay => 'oplogReplay', :no_cursor_timeout => 'noCursorTimeout', :await_data => 'awaitData', :partial => 'allowPartialResults' }.freeze
-
OPTION_MAPPINGS =
Mappings of the options to the find command options.
{ :project => 'projection', :skip => 'skip', :limit => 'limit', :batch_size => 'batchSize' }.freeze
-
SPECIAL_FIELD_MAPPINGS =
{ :$readPreference => '$readPreference', :$orderby => 'sort', :$hint => 'hint', :$comment => 'comment', :$returnKey => 'returnKey', :$snapshot => 'snapshot', :$maxScan => 'maxScan', :$max => 'max', :$min => 'min', :$maxTimeMS => 'maxTimeMS', :$showDiskLoc => 'showRecordId', :$explain => 'explain' }.freeze
Class Method Summary
-
.new(collection, filter, options, flags) ⇒ Upconverter
constructor
Instantiate the upconverter.
Instance Attribute Summary
- #collection ⇒ String readonly
-
#command ⇒ BSON::Document
readonly
Get the upconverted command.
- #filter ⇒ BSON::Document, Hash readonly
- #flags ⇒ Array<Symbol> readonly
- #options ⇒ BSON::Document, Hash readonly
- #command? ⇒ Boolean readonly private
Instance Method Summary
-
#command_name ⇒ String
Get the name of the command.
- #find_command private
- #op_command private
- #query_filter private
Constructor Details
.new(collection, filter, options, flags) ⇒ Upconverter
Instantiate the upconverter.
# File 'lib/mongo/protocol/query.rb', line 283
def initialize(collection, filter, , 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 === raise ArgumentError, 'Options must provide indifferent access' end @collection = collection @filter = filter @options = @flags = flags end
Instance Attribute Details
#collection ⇒ String
(readonly)
# File 'lib/mongo/protocol/query.rb', line 261
attr_reader :collection
#command ⇒ BSON::Document
(readonly)
Get the upconverted command.
# File 'lib/mongo/protocol/query.rb', line 307
def command command? ? op_command : find_command end
#command? ⇒ Boolean
(readonly, private)
# File 'lib/mongo/protocol/query.rb', line 326
def command? collection == Database::COMMAND end
#filter ⇒ BSON::Document
, Hash
(readonly)
# File 'lib/mongo/protocol/query.rb', line 264
attr_reader :filter
#flags ⇒ Array
<Symbol> (readonly)
# File 'lib/mongo/protocol/query.rb', line 270
attr_reader :flags
#options ⇒ BSON::Document
, Hash
(readonly)
# File 'lib/mongo/protocol/query.rb', line 267
attr_reader :
Instance Method Details
#command_name ⇒ String
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.
#find_command (private)
# File 'lib/mongo/protocol/query.rb', line 342
def find_command document = BSON::Document.new( find: collection, filter: query_filter, ) OPTION_MAPPINGS.each do |legacy, option| document.store(option, [legacy]) unless [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 (private)
# File 'lib/mongo/protocol/query.rb', line 334
def op_command document = BSON::Document.new query_filter.each do |field, value| document.store(field.to_s, value) end document end