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 =
# File 'lib/mongo/protocol/query.rb', line 248
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 =
# File 'lib/mongo/protocol/query.rb', line 223
Mappings of the options to the find command options.
{ project: 'projection', skip: 'skip', limit: 'limit', batch_size: 'batchSize' }.freeze -
SPECIAL_FIELD_MAPPINGS =
# File 'lib/mongo/protocol/query.rb', line 230
{ :$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 279
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. raise ArgumentError, 'Filter must provide indifferent access' unless filter.is_a?(BSON::Document) raise ArgumentError, 'Options must provide indifferent access' unless .is_a?(BSON::Document) @collection = collection @filter = filter @options = @flags = flags end
Instance Attribute Details
#collection ⇒ String (readonly)
# File 'lib/mongo/protocol/query.rb', line 257
attr_reader :collection
#command ⇒ BSON::Document (readonly)
Get the upconverted command.
# File 'lib/mongo/protocol/query.rb', line 300
def command command? ? op_command : find_command end
#command? ⇒ Boolean (readonly, private)
# File 'lib/mongo/protocol/query.rb', line 319
def command? collection == Database::COMMAND end
#filter ⇒ BSON::Document, Hash (readonly)
# File 'lib/mongo/protocol/query.rb', line 260
attr_reader :filter
#flags ⇒ Array<Symbol> (readonly)
# File 'lib/mongo/protocol/query.rb', line 266
attr_reader :flags
#options ⇒ BSON::Document, Hash (readonly)
# File 'lib/mongo/protocol/query.rb', line 263
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 335
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| raise Error::LintError, "All keys in filter must be strings: #{filter.inspect}" unless k.is_a?(String) 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 327
def op_command document = BSON::Document.new query_filter.each do |field, value| document.store(field.to_s, value) end document end