Skip to content

Commit

Permalink
2520 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Mar 26, 2021
1 parent e819484 commit 1c08e84
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 88 deletions.
3 changes: 1 addition & 2 deletions lib/mongo/operation.rb
Expand Up @@ -6,6 +6,7 @@
require 'mongo/operation/shared/executable_no_validate'
require 'mongo/operation/shared/executable_transaction_label'
require 'mongo/operation/shared/polymorphic_lookup'
require 'mongo/operation/shared/polymorphic_operation'
require 'mongo/operation/shared/polymorphic_result'
require 'mongo/operation/shared/read_preference_supported'
require 'mongo/operation/shared/bypass_document_validation'
Expand All @@ -19,8 +20,6 @@
require 'mongo/operation/shared/object_id_generator'
require 'mongo/operation/shared/op_msg_or_command'
require 'mongo/operation/shared/op_msg_or_find_command'
require 'mongo/operation/shared/op_msg_or_list_indexes_command'
require 'mongo/operation/shared/collections_info_or_list_collections'

require 'mongo/operation/op_msg_base'
require 'mongo/operation/command'
Expand Down
19 changes: 18 additions & 1 deletion lib/mongo/operation/collections_info.rb
Expand Up @@ -25,7 +25,24 @@ module Operation
# @since 2.0.0
class CollectionsInfo
include Specifiable
include CollectionsInfoOrListCollections
include PolymorphicOperation
include PolymorphicLookup

private

def final_operation(connection)
op_class = if connection.features.list_collections_enabled?
if connection.features.op_msg_enabled?
ListCollections::OpMsg
else
ListCollections::Command
end
else
CollectionsInfo::Command
end

op_class.new(spec)
end
end
end
end
16 changes: 15 additions & 1 deletion lib/mongo/operation/indexes.rb
Expand Up @@ -27,7 +27,21 @@ module Operation
# @since 2.0.0
class Indexes
include Specifiable
include OpMsgOrListIndexesCommand
include PolymorphicOperation
include PolymorphicLookup

private

def final_operation(connection)
cls = if connection.features.op_msg_enabled?
polymorphic_class(self.class.name, :OpMsg)
elsif connection.features.list_indexes_enabled?
polymorphic_class(self.class.name, :Command)
else
polymorphic_class(self.class.name, :Legacy)
end
cls.new(spec)
end
end
end
end
8 changes: 1 addition & 7 deletions lib/mongo/operation/shared/op_msg_or_command.rb
Expand Up @@ -20,15 +20,9 @@ module Operation
#
# @api private
module OpMsgOrCommand
include PolymorphicOperation
include PolymorphicLookup

def execute(server, context:, options: {})
server.with_connection do |connection|
operation = final_operation(connection)
operation.execute(connection, context: context, options: options)
end
end

private

def final_operation(connection)
Expand Down
8 changes: 1 addition & 7 deletions lib/mongo/operation/shared/op_msg_or_find_command.rb
Expand Up @@ -21,15 +21,9 @@ module Operation
#
# @api private
module OpMsgOrFindCommand
include PolymorphicOperation
include PolymorphicLookup

def execute(server, context:)
server.with_connection do |connection|
operation = final_operation(connection)
operation.execute(connection, context: context)
end
end

private

def final_operation(connection)
Expand Down
47 changes: 0 additions & 47 deletions lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb

This file was deleted.

@@ -1,4 +1,4 @@
# Copyright (C) 2020 MongoDB Inc.
# Copyright (C) 2021 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,39 +15,26 @@
module Mongo
module Operation

# Shared behavior of implementing an operation differently based on
# the server that will be executing the operation.
#
# @api private
module CollectionsInfoOrListCollections
include PolymorphicLookup
module PolymorphicOperation
private

# Execute the operation.
#
# @param [ Mongo::Server ] server The server to send the operation to.
# @param [ Operation::Context ] context The operation context.
# @param [ Hash ] options Operation execution options.
#
# @return [ Mongo::Operation::CollectionsInfo::Result,
# Mongo::Operation::ListCollections::Result ] The operation result.
def execute(server, context:)
# @return [ Mongo::Operation::Result ] The operation result.
def execute(server, context:, options: {})
server.with_connection do |connection|
operation = final_operation(connection)
operation.execute(connection, context: context)
operation.execute(connection, context: context, options: options)
end
end

private

def final_operation(connection)
op_class = if connection.features.list_collections_enabled?
if connection.features.op_msg_enabled?
ListCollections::OpMsg
else
ListCollections::Command
end
else
CollectionsInfo::Command
end

op_class.new(spec)
end
end
end
end

0 comments on commit 1c08e84

Please sign in to comment.