Class: Droonga::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, dispatcher, collector_runner, tasks, inputs) ⇒ Session

Returns a new instance of Session.



18
19
20
21
22
23
24
25
# File 'lib/droonga/session.rb', line 18

def initialize(id, dispatcher, collector_runner, tasks, inputs)
  @id = id
  @dispatcher = dispatcher
  @collector_runner = collector_runner
  @tasks = tasks
  @n_dones = 0
  @inputs = inputs
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/droonga/session.rb', line 27

def done?
  @n_dones == @tasks.size
end

#receive(name, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/droonga/session.rb', line 43

def receive(name, value)
  tasks = @inputs[name]
  unless tasks
    #TODO: result arrived before its query
    return
  end
  tasks.each do |task|
    task["n_of_inputs"] += 1
    step = task["step"]
    command = step["type"]
    n_of_expects = step["n_of_expects"]
    message = {
      "task"=>task,
      "name"=>name,
      "value"=>value
    }
    @collector_runner.collect(message)
    return if task["n_of_inputs"] < n_of_expects
    #the task is done
    result = task["values"]
    post = step["post"]
    if post
      # XXX: It is just a workaround.
      # Remove me when super step is introduced.
      if result["errors"]
        reply_body = result
      elsif command == "search_gather"
        reply_body = result
      else
        reply_body = result["result"]
      end
      @dispatcher.reply("body" => reply_body)
    end
    step["descendants"].each do |name, routes|
      message = {
        "id" => @id,
        "input" => name,
        "value" => result[name]
      }
      routes.each do |route|
        @dispatcher.dispatch(message, route)
      end
    end
    @n_dones += 1
  end
end

#startObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/droonga/session.rb', line 31

def start
  tasks = @inputs[nil] || []
  tasks.each do |task|
    local_message = {
      "id"   => @id,
      "task" => task,
    }
    @dispatcher.process_local_message(local_message)
    @n_dones += 1
  end
end