Class: Elasticsearch::Transport::Transport::Connections::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticsearch/transport/transport/connections/collection.rb

Overview

Wraps the collection of connections for the transport object as an Enumerable object.

Constant Summary collapse

DEFAULT_SELECTOR =
Selector::RoundRobin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments):



27
28
29
30
31
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 27

def initialize(arguments={})
  selector_class = arguments[:selector_class] || DEFAULT_SELECTOR
  @connections   = arguments[:connections]    || []
  @selector      = arguments[:selector]       || selector_class.new(arguments.merge(:connections => self))
end

Instance Attribute Details

#selectorObject (readonly)

Returns the value of attribute selector.



21
22
23
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 21

def selector
  @selector
end

Instance Method Details

#add(connections) ⇒ self

Add connection(s) to the collection

Parameters:

  • connections (Connection, Array)

    A connection or an array of connections to add

Returns:

  • (self)


98
99
100
101
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 98

def add(connections)
  @connections += Array(connections).to_a
  self
end

#allArray

Returns an Array of all connections, both dead and alive

Returns:

  • (Array)


62
63
64
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 62

def all
  @connections
end

#connectionsArray Also known as: alive

Returns an Array of alive connections.

Returns:

  • (Array)


45
46
47
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 45

def connections
  @connections.reject { |c| c.dead? }
end

#deadArray

Returns an Array of dead connections.

Returns:

  • (Array)


54
55
56
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 54

def dead
  @connections.select { |c| c.dead? }
end

#each(&block) ⇒ Object



80
81
82
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 80

def each(&block)
  connections.each(&block)
end

#get_connection(options = {}) ⇒ Connection

Returns a connection.

If there are no alive connections, resurrects a connection with least failures. Delegates to selector’s ‘#select` method to get the connection.

Returns:



73
74
75
76
77
78
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 73

def get_connection(options={})
  if connections.empty? && dead_connection = dead.sort { |a,b| a.failures <=> b.failures }.first
    dead_connection.alive!
  end
  selector.select(options)
end

#hostsArray

Returns an Array of hosts information in this collection as Hashes.

Returns:

  • (Array)


37
38
39
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 37

def hosts
  @connections.to_a.map { |c| c.host }
end

#remove(connections) ⇒ self

Remove connection(s) from the collection

Parameters:

  • connections (Connection, Array)

    A connection or an array of connections to remove

Returns:

  • (self)


108
109
110
111
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 108

def remove(connections)
  @connections -= Array(connections).to_a
  self
end

#sizeObject



89
90
91
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 89

def size
  connections.size
end

#slice(*args) ⇒ Object Also known as: []



84
85
86
# File 'lib/elasticsearch/transport/transport/connections/collection.rb', line 84

def slice(*args)
  connections.slice(*args)
end