Skip to content

Commit 19bb2d9

Browse files
committed
RUBY-1279 Refactor Operations
1 parent 1c66ce0 commit 19bb2d9

File tree

211 files changed

+5960
-4143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+5960
-4143
lines changed

lib/mongo/auth/user/view.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class View
4444
def create(user_or_name, options = {})
4545
user = generate(user_or_name, options)
4646
client.send(:with_session, options) do |session|
47-
Operation::Write::Command::CreateUser.new(
47+
Operation::CreateUser.new(
4848
user: user,
4949
db_name: database.name,
5050
session: session
@@ -79,7 +79,7 @@ def initialize(database)
7979
# @since 2.0.0
8080
def remove(name, options = {})
8181
client.send(:with_session, options) do |session|
82-
Operation::Write::Command::RemoveUser.new(
82+
Operation::RemoveUser.new(
8383
user_name: name,
8484
db_name: database.name,
8585
session: session
@@ -103,7 +103,7 @@ def remove(name, options = {})
103103
def update(user_or_name, options = {})
104104
client.send(:with_session, options) do |session|
105105
user = generate(user_or_name, options)
106-
Operation::Write::Command::UpdateUser.new(
106+
Operation::UpdateUser.new(
107107
user: user,
108108
db_name: database.name,
109109
session: session
@@ -132,7 +132,7 @@ def info(name, options = {})
132132

133133
def user_query(name, options = {})
134134
client.send(:with_session, options) do |session|
135-
Operation::Commands::UsersInfo.new(
135+
Operation::UsersInfo.new(
136136
user_name: name,
137137
db_name: database.name,
138138
session: session

lib/mongo/bulk_write.rb

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def execute
6161
write_with_retry(session, write_concern) do |server, txn_num|
6262
execute_operation(
6363
operation.keys.first,
64-
operation.values.first,
64+
operation.values.flatten,
6565
server,
6666
operation_id,
6767
result_combiner,
@@ -72,7 +72,7 @@ def execute
7272
legacy_write_with_retry do |server|
7373
execute_operation(
7474
operation.keys.first,
75-
operation.values.first,
75+
operation.values.flatten,
7676
server,
7777
operation_id,
7878
result_combiner,
@@ -175,7 +175,8 @@ def execute_operation(name, values, server, operation_id, combiner, session, txn
175175
if values.size > server.max_write_batch_size
176176
split_execute(name, values, server, operation_id, combiner, session, txn_num)
177177
else
178-
combiner.combine!(send(name, values, server, operation_id, session, txn_num), values.size)
178+
r = send(name, values, server, operation_id, session, txn_num)
179+
combiner.combine!(r, values.size)
179180
end
180181
rescue Error::MaxBSONSize, Error::MaxMessageSize => e
181182
raise e if values.size <= 1
@@ -195,35 +196,29 @@ def split_execute(name, values, server, operation_id, combiner, session, txn_num
195196
end
196197

197198
def delete_one(documents, server, operation_id, session, txn_num)
198-
Operation::Write::Bulk::Delete.new(
199-
base_spec(operation_id, session).merge(:deletes => documents, :txn_num => txn_num)
200-
).execute(server)
199+
spec = base_spec(operation_id, session).merge(:deletes => documents, :txn_num => txn_num)
200+
Operation::Delete.new(spec).bulk_execute(server)
201201
end
202202

203203
def delete_many(documents, server, operation_id, session, txn_num)
204-
Operation::Write::Bulk::Delete.new(
205-
base_spec(operation_id, session).merge(:deletes => documents)
206-
).execute(server)
204+
spec = base_spec(operation_id, session).merge(:deletes => documents)
205+
Operation::Delete.new(spec).bulk_execute(server)
207206
end
208207

209-
210208
def insert_one(documents, server, operation_id, session, txn_num)
211-
Operation::Write::Bulk::Insert.new(
212-
base_spec(operation_id, session).merge(:documents => documents, :txn_num => txn_num)
213-
).execute(server)
209+
spec = base_spec(operation_id, session).merge(:documents => documents, :txn_num => txn_num)
210+
Operation::Insert.new(spec).bulk_execute(server)
214211
end
215212

216213
def update_one(documents, server, operation_id, session, txn_num)
217-
Operation::Write::Bulk::Update.new(
218-
base_spec(operation_id, session).merge(:updates => documents, :txn_num => txn_num)
219-
).execute(server)
214+
spec = base_spec(operation_id, session).merge(:updates => documents, :txn_num => txn_num)
215+
Operation::Update.new(spec).bulk_execute(server)
220216
end
221217
alias :replace_one :update_one
222218

223219
def update_many(documents, server, operation_id, session, txn_num)
224-
Operation::Write::Bulk::Update.new(
225-
base_spec(operation_id, session).merge(:updates => documents)
226-
).execute(server)
220+
spec = base_spec(operation_id, session).merge(:updates => documents)
221+
Operation::Update.new(spec).bulk_execute(server)
227222
end
228223
end
229224
end

lib/mongo/cluster/reapers/cursor_reaper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def kill_cursors
124124
if server.features.find_command_enabled?
125125
Cursor::Builder::KillCursorsCommand.update_cursors(op_spec, active_cursors_copy.to_a)
126126
if Cursor::Builder::KillCursorsCommand.get_cursors_list(op_spec).size > 0
127-
Operation::Commands::Command.new(op_spec).execute(server)
127+
Operation::KillCursors.new(op_spec).execute(server)
128128
end
129129
else
130130
Cursor::Builder::OpKillCursors.update_cursors(op_spec, active_cursors_copy.to_a)

lib/mongo/collection.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ def create(opts = {})
189189
raise Error::UnsupportedCollation.new
190190
end
191191
client.send(:with_session, opts) do |session|
192-
Operation::Commands::Create.new({
193-
selector: operation,
194-
db_name: database.name,
195-
write_concern: write_concern,
196-
session: session
197-
}).execute(server)
192+
Operation::Create.new({
193+
selector: operation,
194+
db_name: database.name,
195+
write_concern: write_concern,
196+
session: session
197+
}).execute(server)
198198
end
199199
end
200200

@@ -215,12 +215,12 @@ def create(opts = {})
215215
# @since 2.0.0
216216
def drop(opts = {})
217217
client.send(:with_session, opts) do |session|
218-
Operation::Commands::Drop.new({
219-
selector: { :drop => name },
220-
db_name: database.name,
221-
write_concern: write_concern,
222-
session: session
223-
}).execute(next_primary)
218+
Operation::Drop.new({
219+
selector: { :drop => name },
220+
db_name: database.name,
221+
write_concern: write_concern,
222+
session: session
223+
}).execute(next_primary)
224224
end
225225
rescue Error::OperationFailure => ex
226226
raise ex unless ex.message =~ /ns not found/
@@ -420,7 +420,7 @@ def inspect
420420
def insert_one(document, opts = {})
421421
client.send(:with_session, opts) do |session|
422422
write_with_retry(session, write_concern) do |server, txn_num|
423-
Operation::Write::Insert.new(
423+
Operation::Insert.new(
424424
:documents => [ document ],
425425
:db_name => database.name,
426426
:coll_name => name,

lib/mongo/collection/view/aggregation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def new(options)
103103
end
104104

105105
def initial_query_op(session)
106-
Operation::Commands::Aggregate.new(aggregate_spec(session))
106+
Operation::Aggregate.new(aggregate_spec(session))
107107
end
108108

109109
def valid_server?(server)

lib/mongo/collection/view/iterable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ def initial_query_op(server, session)
6565
if server.features.find_command_enabled?
6666
initial_command_op(session)
6767
else
68-
Operation::Read::Query.new(Builder::OpQuery.new(self).specification)
68+
Operation::Find.new(Builder::OpQuery.new(self).specification)
6969
end
7070
end
7171

7272
def initial_command_op(session)
7373
if explained?
74-
Operation::Commands::Explain.new(Builder::FindCommand.new(self, session).explain_specification)
74+
Operation::Explain.new(Builder::FindCommand.new(self, session).explain_specification)
7575
else
76-
Operation::Commands::Find.new(Builder::FindCommand.new(self, session).specification)
76+
Operation::Find.new(Builder::FindCommand.new(self, session).specification)
7777
end
7878
end
7979

lib/mongo/collection/view/map_reduce.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def new(options)
217217
end
218218

219219
def initial_query_op(session)
220-
Operation::Commands::MapReduce.new(map_reduce_spec(session))
220+
Operation::MapReduce.new(map_reduce_spec(session))
221221
end
222222

223223
def valid_server?(server)
@@ -247,9 +247,9 @@ def find_command_spec(session)
247247

248248
def fetch_query_op(server, session)
249249
if server.features.find_command_enabled?
250-
Operation::Commands::Find.new(find_command_spec(session))
250+
Operation::Find.new(find_command_spec(session))
251251
else
252-
Operation::Read::Query.new(fetch_query_spec)
252+
Operation::Find.new(fetch_query_spec)
253253
end
254254
end
255255

lib/mongo/collection/view/readable.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ def count(opts = {})
139139
server = selector.select_server(cluster)
140140
apply_collation!(cmd, server, opts)
141141
with_session(opts) do |session|
142-
Operation::Commands::Count.new({
143-
:selector => cmd,
144-
:db_name => database.name,
145-
:options => {:limit => -1},
146-
:read => read_pref,
147-
:session => session
148-
}).execute(server)
142+
Operation::Count.new({
143+
:selector => cmd,
144+
:db_name => database.name,
145+
:options => {:limit => -1},
146+
:read => read_pref,
147+
:session => session
148+
}).execute(server)
149149
end.n.to_i
150150
end
151151
end
@@ -179,13 +179,13 @@ def distinct(field_name, opts = {})
179179
server = selector.select_server(cluster)
180180
apply_collation!(cmd, server, opts)
181181
with_session(opts) do |session|
182-
Operation::Commands::Distinct.new({
183-
:selector => cmd,
184-
:db_name => database.name,
185-
:options => {:limit => -1},
186-
:read => read_pref,
187-
:session => session
188-
}).execute(server)
182+
Operation::Distinct.new({
183+
:selector => cmd,
184+
:db_name => database.name,
185+
:options => {:limit => -1},
186+
:read => read_pref,
187+
:session => session
188+
}).execute(server)
189189
end.first['values']
190190
end
191191
end
@@ -480,7 +480,7 @@ def server_selector
480480
def parallel_scan(cursor_count, options = {})
481481
session = client.send(:get_session, @options)
482482
server = server_selector.select_server(cluster)
483-
cmd = Operation::Commands::ParallelScan.new({
483+
cmd = Operation::ParallelScan.new({
484484
:coll_name => collection.name,
485485
:db_name => database.name,
486486
:cursor_count => cursor_count,
@@ -489,14 +489,14 @@ def parallel_scan(cursor_count, options = {})
489489
}.merge!(options))
490490
cmd.execute(server).cursor_ids.map do |cursor_id|
491491
result = if server.features.find_command_enabled?
492-
Operation::Commands::GetMore.new({
492+
Operation::GetMore.new({
493493
:selector => {:getMore => cursor_id,
494494
:collection => collection.name},
495495
:db_name => database.name,
496496
:session => session
497497
}).execute(server)
498498
else
499-
Operation::Read::GetMore.new({
499+
Operation::GetMore.new({
500500
:to_return => 0,
501501
:cursor_id => cursor_id,
502502
:db_name => database.name,

lib/mongo/collection/view/writable.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def find_one_and_delete(opts = {})
4949
with_session(opts) do |session|
5050
write_with_retry(session, write_concern) do |server, txn_num|
5151
apply_collation!(cmd, server, opts)
52-
Operation::Commands::Command.new(
52+
Operation::Command.new(
5353
:selector => cmd,
5454
:db_name => database.name,
5555
:session => session,
@@ -121,7 +121,7 @@ def find_one_and_update(document, opts = {})
121121
write_with_retry(session, write_concern) do |server, txn_num|
122122
apply_collation!(cmd, server, opts)
123123
apply_array_filters!(cmd, server, opts)
124-
Operation::Commands::Command.new(
124+
Operation::Command.new(
125125
:selector => cmd,
126126
:db_name => database.name,
127127
:session => session,
@@ -149,8 +149,8 @@ def delete_many(opts = {})
149149
with_session(opts) do |session|
150150
legacy_write_with_retry do |server|
151151
apply_collation!(delete_doc, server, opts)
152-
Operation::Write::Delete.new(
153-
:delete => delete_doc,
152+
Operation::Delete.new(
153+
:deletes => [ delete_doc ],
154154
:db_name => collection.database.name,
155155
:coll_name => collection.name,
156156
:write_concern => collection.write_concern,
@@ -178,8 +178,8 @@ def delete_one(opts = {})
178178
with_session(opts) do |session|
179179
write_with_retry(session, write_concern) do |server, txn_num|
180180
apply_collation!(delete_doc, server, opts)
181-
Operation::Write::Delete.new(
182-
:delete => delete_doc,
181+
Operation::Delete.new(
182+
:deletes => [ delete_doc ],
183183
:db_name => collection.database.name,
184184
:coll_name => collection.name,
185185
:write_concern => write_concern,
@@ -217,8 +217,8 @@ def replace_one(replacement, opts = {})
217217
apply_collation!(update_doc, server, opts)
218218
apply_array_filters!(update_doc, server, opts)
219219

220-
Operation::Write::Update.new(
221-
:update => update_doc,
220+
Operation::Update.new(
221+
:updates => [ update_doc ],
222222
:db_name => collection.database.name,
223223
:coll_name => collection.name,
224224
:write_concern => write_concern,
@@ -256,8 +256,8 @@ def update_many(spec, opts = {})
256256
legacy_write_with_retry do |server|
257257
apply_collation!(update_doc, server, opts)
258258
apply_array_filters!(update_doc, server, opts)
259-
Operation::Write::Update.new(
260-
:update => update_doc,
259+
Operation::Update.new(
260+
:updates => [ update_doc ],
261261
:db_name => collection.database.name,
262262
:coll_name => collection.name,
263263
:write_concern => collection.write_concern,
@@ -296,8 +296,8 @@ def update_one(spec, opts = {})
296296
apply_collation!(update_doc, server, opts)
297297
apply_array_filters!(update_doc, server, opts)
298298

299-
Operation::Write::Update.new(
300-
:update => update_doc,
299+
Operation::Update.new(
300+
:updates => [ update_doc ],
301301
:db_name => collection.database.name,
302302
:coll_name => collection.name,
303303
:write_concern => write_concern,

lib/mongo/cursor.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ def get_more
206206

207207
def get_more_operation
208208
if @server.features.find_command_enabled?
209-
Operation::Commands::GetMore.new(Builder::GetMoreCommand.new(self, @session).specification)
209+
spec = Builder::GetMoreCommand.new(self, @session).specification
210210
else
211-
Operation::Read::GetMore.new(Builder::OpGetMore.new(self).specification)
211+
spec = Builder::OpGetMore.new(self).specification
212212
end
213+
Operation::GetMore.new(spec)
213214
end
214215

215216
def kill_cursors
@@ -227,11 +228,7 @@ def end_session
227228
end
228229

229230
def kill_cursors_operation
230-
if @server.features.find_command_enabled?
231-
Operation::Commands::Command.new(kill_cursors_op_spec)
232-
else
233-
Operation::KillCursors.new(kill_cursors_op_spec)
234-
end
231+
Operation::KillCursors.new(kill_cursors_op_spec)
235232
end
236233

237234
def kill_cursors_op_spec

0 commit comments

Comments
 (0)