Skip to content

Commit 8249b70

Browse files
committed
RUBY-1028: Improve performance of query protocol upconverter
1 parent c36898d commit 8249b70

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

lib/mongo/protocol/query.rb

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -158,37 +158,47 @@ class Upconverter
158158
#
159159
# @since 2.1.0
160160
OPTION_MAPPINGS = {
161-
:project => :projection,
162-
:skip => :skip,
163-
:limit => :limit,
164-
:batch_size => :batchSize
165-
}
161+
:project => 'projection',
162+
:skip => 'skip',
163+
:limit => 'limit',
164+
:batch_size => 'batchSize'
165+
}.freeze
166166

167167
SPECIAL_FIELD_MAPPINGS = {
168-
:$readPreference => :readPreference,
169-
:$orderby => :sort,
170-
:$hint => :hint,
171-
:$comment => :comment,
172-
:$returnKey => :returnKey,
173-
:$snapshot => :snapshot,
174-
:$maxScan => :maxScan,
175-
:$max => :max,
176-
:$min => :min,
177-
:$maxTimeMS => :maxTimeMS,
178-
:$showDiskLoc => :showRecordId,
179-
:$explain => :explain
180-
}
168+
:$readPreference => 'readPreference',
169+
:$orderby => 'sort',
170+
:$hint => 'hint',
171+
:$comment => 'comment',
172+
:$returnKey => 'returnKey',
173+
:$snapshot => 'snapshot',
174+
:$maxScan => 'maxScan',
175+
:$max => 'max',
176+
:$min => 'min',
177+
:$maxTimeMS => 'maxTimeMS',
178+
:$showDiskLoc => 'showRecordId',
179+
:$explain => 'explain'
180+
}.freeze
181181

182182
# Mapping of flags to find command options.
183183
#
184184
# @since 2.1.0
185185
FLAG_MAPPINGS = {
186-
:tailable_cursor => :tailable,
187-
:oplog_replay => :oplogReplay,
188-
:no_cursor_timeout => :noCursorTimeout,
189-
:await_data => :awaitData,
190-
:partial => :allowPartialResults
191-
}
186+
:tailable_cursor => 'tailable',
187+
:oplog_replay => 'oplogReplay',
188+
:no_cursor_timeout => 'noCursorTimeout',
189+
:await_data => 'awaitData',
190+
:partial => 'allowPartialResults'
191+
}.freeze
192+
193+
# Find command constant.
194+
#
195+
# @since 2.1.0
196+
FIND = 'find'.freeze
197+
198+
# Filter attribute constant.
199+
#
200+
# @since 2.1.0
201+
FILTER = 'filter'.freeze
192202

193203
# @return [ String ] collection The name of the collection.
194204
attr_reader :collection
@@ -242,7 +252,7 @@ def command
242252
#
243253
# @since 2.1.0
244254
def command_name
245-
command? ? filter.keys.first : 'find'
255+
command? ? filter.keys.first : FIND
246256
end
247257

248258
private
@@ -257,16 +267,16 @@ def op_command
257267

258268
def find_command
259269
document = BSON::Document.new
260-
document[:find] = collection
261-
document[:filter] = filter[:$query] ? filter[:$query] : filter
270+
document.store(FIND, collection)
271+
document.store(FILTER, filter[:$query] ? filter[:$query] : filter)
262272
OPTION_MAPPINGS.each do |legacy, option|
263-
document[option] = options[legacy] unless options[legacy].nil?
273+
document.store(option, options[legacy]) unless options[legacy].nil?
264274
end
265275
SPECIAL_FIELD_MAPPINGS.each do |special, normal|
266-
document[normal] = filter[special] unless filter[special].nil?
276+
document.store(normal, filter[special]) unless filter[special].nil?
267277
end
268278
FLAG_MAPPINGS.each do |legacy, flag|
269-
document[flag] = true if flags.include?(legacy)
279+
document.store(flag, true) if flags.include?(legacy)
270280
end
271281
document
272282
end

0 commit comments

Comments
 (0)