Skip to content

Commit c0a94a2

Browse files
committed
Slight optimization in upconverters
1 parent 7f47bc9 commit c0a94a2

File tree

6 files changed

+100
-29
lines changed

6 files changed

+100
-29
lines changed

lib/mongo/protocol/delete.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def op_code
9999
# @since 2.1.0
100100
class Upconverter
101101

102+
# The delete command constant.
103+
#
104+
# @since 2.2.0
105+
DELETE = 'delete'.freeze
106+
107+
# The deletes command constant.
108+
#
109+
# @since 2.2.0
110+
DELETES = 'deletes'.freeze
111+
102112
# @return [ String ] collection The name of the collection.
103113
attr_reader :collection
104114

@@ -132,11 +142,11 @@ def initialize(collection, filter, options)
132142
#
133143
# @since 2.1.0
134144
def command
135-
BSON::Document.new(
136-
delete: collection,
137-
deletes: [ BSON::Document.new(q: filter, limit: limit) ],
138-
ordered: true
139-
)
145+
document = BSON::Document.new
146+
document.store(DELETE => collection)
147+
document.store(DELETES => [ BSON::Document.new(Message::Q => filter, Message::LIMIT => limit) ])
148+
document.store(Message::ORDERED => true)
149+
document
140150
end
141151

142152
private

lib/mongo/protocol/get_more.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def op_code
104104
# @since 2.1.0
105105
class Upconverter
106106

107+
# The get more constant.
108+
#
109+
# @since 2.2.0
110+
GET_MORE = 'getMore'.freeze
111+
107112
# @return [ String ] collection The name of the collection.
108113
attr_reader :collection
109114

@@ -139,11 +144,11 @@ def initialize(collection, cursor_id, number_to_return)
139144
#
140145
# @since 2.1.0
141146
def command
142-
BSON::Document.new(
143-
getMore: cursor_id,
144-
batchSize: number_to_return,
145-
collection: collection
146-
)
147+
document = BSON::Document.new
148+
document.store(GET_MORE => cursor_id)
149+
document.store(Message::BATCH_SIZE => number_to_return)
150+
document.store(Message::COLLECTION => collection)
151+
document
147152
end
148153
end
149154
end

lib/mongo/protocol/insert.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ class Upconverter
114114
# @since 2.1.0
115115
DOCUMENTS = 'documents'.freeze
116116

117-
# Ordered field constant.
118-
#
119-
# @since 2.1.0
120-
ORDERED = 'ordered'.freeze
121-
122117
# Write concern field constant.
123118
#
124119
# @since 2.1.0
@@ -161,7 +156,7 @@ def command
161156
document = BSON::Document.new
162157
document.store(INSERT, collection)
163158
document.store(DOCUMENTS, documents)
164-
document.store(ORDERED, options.fetch(:ordered, true))
159+
document.store(Message::ORDERED, options.fetch(:ordered, true))
165160
document.merge!(WRITE_CONCERN => options[:write_concern].options) if options[:write_concern]
166161
document
167162
end

lib/mongo/protocol/kill_cursors.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def op_code
8282
# @since 2.1.0
8383
class Upconverter
8484

85+
# The kill cursors constant.
86+
#
87+
# @since 2.2.0
88+
KILL_CURSORS = 'killCursors'.freeze
89+
90+
# The cursors constant.
91+
#
92+
# @since 2.2.0
93+
CURSORS = 'cursors'.freeze
94+
8595
# @return [ String ] collection The name of the collection.
8696
attr_reader :collection
8797

@@ -111,7 +121,10 @@ def initialize(collection, cursor_ids)
111121
#
112122
# @since 2.1.0
113123
def command
114-
BSON::Document.new(killCursors: collection, cursors: cursor_ids)
124+
document = BSON::Document.new
125+
document.store(KILL_CURSORS => collection)
126+
document.store(CURSORS => cursor_ids)
127+
document
115128
end
116129
end
117130
end

lib/mongo/protocol/message.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ module Protocol
4242
class Message
4343
include Serializers
4444

45+
# The batch size constant.
46+
#
47+
# @since 2.2.0
48+
BATCH_SIZE = 'batchSize'.freeze
49+
50+
# The collection constant.
51+
#
52+
# @since 2.2.0
53+
COLLECTION = 'collection'.freeze
54+
55+
# The limit constant.
56+
#
57+
# @since 2.2.0
58+
LIMIT = 'limit'.freeze
59+
60+
# The ordered constant.
61+
#
62+
# @since 2.2.0
63+
ORDERED = 'ordered'.freeze
64+
65+
# The q constant.
66+
#
67+
# @since 2.2.0
68+
Q = 'q'.freeze
69+
4570
# Returns the request id for the message
4671
#
4772
# @return [Fixnum] The request id for this message

lib/mongo/protocol/update.rb

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ def op_code
116116
# @since 2.1.0
117117
class Upconverter
118118

119+
# The multi constant.
120+
#
121+
# @since 2.2.0
122+
MULTI = 'multi'.freeze
123+
124+
# The u constant.
125+
#
126+
# @since 2.2.0
127+
U = 'u'.freeze
128+
129+
# The update constant.
130+
#
131+
# @since 2.2.0
132+
UPDATE = 'update'.freeze
133+
134+
# The updates constant.
135+
#
136+
# @since 2.2.0
137+
UPDATES = 'updates'.freeze
138+
139+
# The upsert constant.
140+
#
141+
# @since 2.2.0
142+
UPSERT = 'upsert'.freeze
143+
119144
# @return [ String ] collection The name of the collection.
120145
attr_reader :collection
121146

@@ -160,18 +185,16 @@ def initialize(collection, filter, update, flags)
160185
#
161186
# @since 2.1.0
162187
def command
163-
BSON::Document.new(
164-
update: collection,
165-
ordered: true,
166-
updates: [
167-
BSON::Document.new(
168-
q: filter,
169-
u: update,
170-
multi: flags.include?(:multi_update),
171-
upsert: flags.include?(:upsert),
172-
)
173-
]
174-
)
188+
document = BSON::Document.new
189+
updates = BSON::Document.new
190+
updates.store(Message::Q => filter)
191+
updates.store(U => update)
192+
updates.store(MULTI => flags.include?(:multi_update))
193+
updates.store(UPSERT => flags.include?(:upsert))
194+
document.store(UPDATE => collection)
195+
document.store(Message::ORDERED => true)
196+
document.store(UPDATES => [ updates ])
197+
document
175198
end
176199
end
177200
end

0 commit comments

Comments
 (0)