lib/mongo/cluster/topology/replica_set.rb in mongo-2.0.5 vs lib/mongo/cluster/topology/replica_set.rb in mongo-2.0.6

- old
+ new

@@ -123,10 +123,58 @@ (replica_set_name.nil? || server.replica_set_name == replica_set_name) && server.primary? || server.secondary? end end + # Whether a server description's hosts may be added to the cluster. + # + # @example Check if a description's hosts may be added to the cluster. + # topology.add_hosts?(description, servers) + # + # @param [ Mongo::Server::Description ] description The description. + # @param [ Array<Mongo::Server> ] servers The cluster servers. + # + # @return [ true, false ] Whether a description's hosts may be added. + # + # @since 2.0.6 + def add_hosts?(description, servers) + !!(member_of_this_set?(description) && !has_primary?(servers)) + end + + # Whether a description can be used to remove hosts from the cluster. + # + # @example Check if a description can be used to remove hosts from the cluster. + # topology.remove_hosts?(description) + # + # @param [ Mongo::Server::Description ] description The description. + # + # @return [ true, false ] Whether hosts may be removed from the cluster. + # + # @since 2.0.6 + def remove_hosts?(description) + !description.config.empty? && + (description.primary? || + description.hosts.empty? || + !member_of_this_set?(description)) + end + + # Whether a specific server in the cluster can be removed, given a description. + # + # @example Check if a specific server can be removed from the cluster. + # topology.remove_server?(description, server) + # + # @param [ Mongo::Server::Description ] description The description. + # @param [ Mongo::Serve ] server The server in question. + # + # @return [ true, false ] Whether the server can be removed from the cluster. + # + # @since 2.0.6 + def remove_server?(description, server) + remove_self?(description, server) || + (member_of_this_set?(description) && !description.lists_server?(server)) + end + # A replica set topology is not sharded. # # @example Is the topology sharded? # ReplicaSet.sharded? # @@ -152,9 +200,36 @@ # # @return [ false ] Always false. # # @since 2.0.0 def unknown?; false; end + + # Notify the topology that a standalone was discovered. + # + # @example Notify the topology that a standalone was discovered. + # topology.standalone_discovered + # + # @return [ Topology::ReplicaSet ] Always returns self. + # + # @since 2.0.6 + def standalone_discovered; self; end + + private + + def has_primary?(servers) + servers.find { |s| s.primary? } + end + + def member_of_this_set?(description) + description.replica_set_member? && + description.replica_set_name == replica_set_name + end + + def remove_self?(description, server) + !member_of_this_set?(description) && + description.is_server?(server) && + !description.ghost? + end end end end end