Class: CouchProxy::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/couchproxy/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, couchjs, reducers) ⇒ Cluster

Returns a new instance of Cluster.



7
8
9
10
11
# File 'lib/couchproxy/cluster.rb', line 7

def initialize(nodes, couchjs, reducers)
  @nodes, @partitions = [], 0
  nodes.each {|n| self << n }
  @reducers = Array.new(reducers) { Reducer.new(couchjs) }
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/couchproxy/cluster.rb', line 5

def nodes
  @nodes
end

Instance Method Details

#<<(node) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/couchproxy/cluster.rb', line 17

def <<(node)
  @nodes << node
  @partitions = @nodes.inject(0) do |acc, n|
    acc + n.partitions.size
  end
  self
end

#any_nodeObject



31
32
33
# File 'lib/couchproxy/cluster.rb', line 31

def any_node
  @nodes[rand(@nodes.size)]
end

#any_partitionObject



35
36
37
# File 'lib/couchproxy/cluster.rb', line 35

def any_partition
  any_node.any_partition
end

#partition(doc_id) ⇒ Object



25
26
27
28
29
# File 'lib/couchproxy/cluster.rb', line 25

def partition(doc_id)
  num = Zlib.crc32(doc_id.to_s).abs % @partitions
  node = @nodes.find {|n| n.hosts?(num) }
  node.partition(num)
end

#partitionsObject



39
40
41
# File 'lib/couchproxy/cluster.rb', line 39

def partitions
  @nodes.map {|n| n.partitions}.flatten
end

#reducerObject



13
14
15
# File 'lib/couchproxy/cluster.rb', line 13

def reducer
  @reducers[rand(@reducers.size)]
end