Class: CouchProxy::ReduceProcess

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/couchproxy/reducer.rb

Instance Method Summary collapse

Constructor Details

#initialize(unbind = nil) ⇒ ReduceProcess

Returns a new instance of ReduceProcess.



26
27
28
# File 'lib/couchproxy/reducer.rb', line 26

def initialize(unbind=nil)
  @unbind, @connected, @callbacks, @deferred = unbind, false, [], []
end

Instance Method Details

#post_initObject



30
31
32
33
34
35
# File 'lib/couchproxy/reducer.rb', line 30

def post_init
  @connected = true
  @deferred.slice!(0, @deferred.size).each do |fn, values, callback|
    rereduce(fn, values, callback)
  end
end

#receive_data(data) ⇒ Object



46
47
48
49
50
# File 'lib/couchproxy/reducer.rb', line 46

def receive_data(data)
  data.split("\n").each do |line|
    @callbacks.shift.call(JSON.parse(line))
  end
end

#rereduce(fn, values, callback) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/couchproxy/reducer.rb', line 37

def rereduce(fn, values, callback)
  if @connected
    @callbacks << callback
    send_data(["rereduce", [fn], values].to_json + "\n")
  else
    @deferred << [fn, values, callback]
  end
end

#unbindObject



52
53
54
55
# File 'lib/couchproxy/reducer.rb', line 52

def unbind
  @connected = false
  @unbind.call if @unbind
end