From 1c08e84634dd6ae77facd67ce5fbd5ad20fd6a25 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 26 Mar 2021 09:00:24 -0400 Subject: [PATCH] 2520 refactor --- lib/mongo/operation.rb | 3 +- lib/mongo/operation/collections_info.rb | 19 +++++++- lib/mongo/operation/indexes.rb | 16 ++++++- .../operation/shared/op_msg_or_command.rb | 8 +--- .../shared/op_msg_or_find_command.rb | 8 +--- .../shared/op_msg_or_list_indexes_command.rb | 47 ------------------- ...ollections.rb => polymorphic_operation.rb} | 33 ++++--------- 7 files changed, 46 insertions(+), 88 deletions(-) delete mode 100644 lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb rename lib/mongo/operation/shared/{collections_info_or_list_collections.rb => polymorphic_operation.rb} (57%) diff --git a/lib/mongo/operation.rb b/lib/mongo/operation.rb index e9c7c51bb2..298ee5bc95 100644 --- a/lib/mongo/operation.rb +++ b/lib/mongo/operation.rb @@ -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' @@ -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' diff --git a/lib/mongo/operation/collections_info.rb b/lib/mongo/operation/collections_info.rb index 6d88c988b6..0c3fc725cf 100644 --- a/lib/mongo/operation/collections_info.rb +++ b/lib/mongo/operation/collections_info.rb @@ -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 diff --git a/lib/mongo/operation/indexes.rb b/lib/mongo/operation/indexes.rb index b28d5dafc8..c03ae15a31 100644 --- a/lib/mongo/operation/indexes.rb +++ b/lib/mongo/operation/indexes.rb @@ -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 diff --git a/lib/mongo/operation/shared/op_msg_or_command.rb b/lib/mongo/operation/shared/op_msg_or_command.rb index 0808312203..20e5ffbbfc 100644 --- a/lib/mongo/operation/shared/op_msg_or_command.rb +++ b/lib/mongo/operation/shared/op_msg_or_command.rb @@ -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) diff --git a/lib/mongo/operation/shared/op_msg_or_find_command.rb b/lib/mongo/operation/shared/op_msg_or_find_command.rb index 3b0a58946d..3bc8e44452 100644 --- a/lib/mongo/operation/shared/op_msg_or_find_command.rb +++ b/lib/mongo/operation/shared/op_msg_or_find_command.rb @@ -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) diff --git a/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb b/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb deleted file mode 100644 index ad72252388..0000000000 --- a/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright (C) 2018-2020 MongoDB Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -module Mongo - module Operation - - # Shared behavior of executing the operation as an OpMsg when supported, - # as a Command when list indxes command is supported by the server, and as - # Legacy otherwise. - # - # @api private - module OpMsgOrListIndexesCommand - 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) - 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 diff --git a/lib/mongo/operation/shared/collections_info_or_list_collections.rb b/lib/mongo/operation/shared/polymorphic_operation.rb similarity index 57% rename from lib/mongo/operation/shared/collections_info_or_list_collections.rb rename to lib/mongo/operation/shared/polymorphic_operation.rb index 3f9f055a77..c4c37eb516 100644 --- a/lib/mongo/operation/shared/collections_info_or_list_collections.rb +++ b/lib/mongo/operation/shared/polymorphic_operation.rb @@ -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. @@ -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