Class: Mongo::ServerSelector::SecondaryPreferred

Inherits:
Object
  • Object
show all
Includes:
Selectable
Defined in:
lib/mongo/server_selector/secondary_preferred.rb

Overview

Encapsulates specifications for selecting servers, with

secondaries preferred, given a list of candidates.

Since:

  • 2.0.0

Constant Summary

SERVER_FORMATTED_NAME =

Name of the this read preference in the server's format.

Since:

  • 2.5.0

'secondaryPreferred'.freeze

Instance Attribute Summary

Attributes included from Selectable

#max_staleness, #options, #tag_sets

Instance Method Summary collapse

Methods included from Selectable

#==, #candidates, #initialize, #inspect, #local_threshold, #select_server, #server_selection_timeout

Instance Method Details

#nameSymbol

Get the name of the server mode type.

Examples:

Get the name of the server mode for this preference.

preference.name

Returns:

  • (Symbol)

    :secondary_preferred

Since:

  • 2.0.0



39
40
41
# File 'lib/mongo/server_selector/secondary_preferred.rb', line 39

def name
  :secondary_preferred
end

#slave_ok?true

Whether the slaveOk bit should be set on wire protocol messages.

I.e. whether the operation can be performed on a secondary server.

Returns:

  • (true)

    true

Since:

  • 2.0.0



49
50
51
# File 'lib/mongo/server_selector/secondary_preferred.rb', line 49

def slave_ok?
  true
end

#tags_allowed?true

Whether tag sets are allowed to be defined for this server preference.

Returns:

  • (true)

    true

Since:

  • 2.0.0



58
59
60
# File 'lib/mongo/server_selector/secondary_preferred.rb', line 58

def tags_allowed?
  true
end

#to_docHash

Convert this server preference definition into a format appropriate

for a server.

Examples:

Convert this server preference definition into a format

for a server.
preference = Mongo::ServerSelector::SecondaryPreferred.new
preference.to_doc

Returns:

  • (Hash)

    The server preference formatted for a server.

Since:

  • 2.5.0



91
92
93
94
95
96
# File 'lib/mongo/server_selector/secondary_preferred.rb', line 91

def to_doc
  @doc ||= (preference = { mode: SERVER_FORMATTED_NAME }
    preference.merge!({ tags: tag_sets }) unless tag_sets.empty?
    preference.merge!({ maxStalenessSeconds: max_staleness }) if max_staleness
    preference)
end

#to_mongosHash

Convert this server preference definition into a format appropriate

for a mongos server.

Note that the server preference is not sent to mongos as part of the query

selector if there are no tag sets, for maximum backwards compatibility.

Examples:

Convert this server preference definition into a format

for mongos.
preference = Mongo::ServerSelector::SecondaryPreferred.new
preference.to_mongos

Returns:

  • (Hash)

    The server preference formatted for a mongos server.

Since:

  • 2.0.0



75
76
77
78
# File 'lib/mongo/server_selector/secondary_preferred.rb', line 75

def to_mongos
  return nil if tag_sets.empty? && max_staleness.nil?
  to_doc
end