Module: Mongo::Cluster::Topology

Extended by:
Topology
Included in:
Topology
Defined in:
lib/mongo/cluster/topology.rb,
lib/mongo/cluster/topology.rb,
lib/mongo/cluster/topology/base.rb,
lib/mongo/cluster/topology/single.rb,
lib/mongo/cluster/topology/sharded.rb,
lib/mongo/cluster/topology/unknown.rb,
lib/mongo/cluster/topology/no_replica_set_options.rb,
lib/mongo/cluster/topology/replica_set_no_primary.rb,
lib/mongo/cluster/topology/replica_set_with_primary.rb

Overview

Defines behavior for getting servers.

Topologies are associated with their clusters - for example, a ReplicaSet topology contains the replica set name. A topology object therefore cannot be used with multiple cluster objects.

At the same time, topology objects do not know anything about specific servers in a cluster, despite what their constructor may suggest. Which means, in particular, that topology change events require the application to maintain cluster references on its own if it wants to track server changes within a replica set.

Since:

  • 2.0.0

Defined Under Namespace

Modules: NoReplicaSetOptions Classes: Base, ReplicaSetNoPrimary, ReplicaSetWithPrimary, Sharded, Single, Unknown

Constant Summary collapse

OPTIONS =

The various topologies for server selection.

Since:

  • 2.0.0

{
  replica_set: ReplicaSetNoPrimary,
  sharded: Sharded,
  direct: Single,
}.freeze

Instance Method Summary collapse

Instance Method Details

#initial(cluster, monitoring, options) ⇒ ReplicaSet, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the initial cluster topology for the provided options.

Examples:

Get the initial cluster topology.

Topology.initial(topology: :replica_set)

Parameters:

  • cluster (Cluster)

    The cluster.

  • monitoring (Monitoring)

    The monitoring.

  • options (Hash)

    The cluster options.

Returns:

Since:

  • 2.0.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mongo/cluster/topology.rb', line 69

def initial(cluster, monitoring, options)
  cls = if options.key?(:connect)
    OPTIONS.fetch(options[:connect].to_sym)
  elsif options.key?(:replica_set) || options.key?(:replica_set_name)
    ReplicaSetNoPrimary
  else
    Unknown
  end
  # Options here are client/cluster/server options.
  # In particular the replica set name key is different for
  # topology.
  # If replica_set_name is given (as might be internally by driver),
  # use that key.
  # Otherwise (e.g. options passed down from client),
  # move replica_set to replica_set_name.
  if (cls <= ReplicaSetNoPrimary || cls == Single) && !options[:replica_set_name]
    options = options.dup
    options[:replica_set_name] = options.delete(:replica_set)
  end
  cls.new(options, monitoring, cluster)
end