Class: Droonga::EngineState

Inherits:
Object
  • Object
show all
Includes:
Deferrable, Loggable
Defined in:
lib/droonga/engine_state.rb

Constant Summary collapse

DEFAULT_SESSION_TIMEOUT_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Deferrable

#wait_until_ready

Constructor Details

#initialize(params) ⇒ EngineState

Returns a new instance of EngineState.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/droonga/engine_state.rb', line 41

def initialize(params)
  @loop = params[:loop]
  @name = params[:name]
  @internal_name = params[:internal_name]
  @internal_connection_lifetime = params[:internal_connection_lifetime]
  @sessions = {}
  @current_id = 0
  @forwarder = create_forwarder
  @replier = create_replier
  @on_finish = nil
  @catalog = params[:catalog]
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



38
39
40
# File 'lib/droonga/engine_state.rb', line 38

def catalog
  @catalog
end

#forwarderObject (readonly)

Returns the value of attribute forwarder.



36
37
38
# File 'lib/droonga/engine_state.rb', line 36

def forwarder
  @forwarder
end

#internal_connection_lifetimeObject (readonly)

Returns the value of attribute internal_connection_lifetime.



35
36
37
# File 'lib/droonga/engine_state.rb', line 35

def internal_connection_lifetime
  @internal_connection_lifetime
end

#internal_nameObject (readonly)

Returns the value of attribute internal_name.



34
35
36
# File 'lib/droonga/engine_state.rb', line 34

def internal_name
  @internal_name
end

#loopObject (readonly)

Returns the value of attribute loop.



32
33
34
# File 'lib/droonga/engine_state.rb', line 32

def loop
  @loop
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/droonga/engine_state.rb', line 33

def name
  @name
end

#on_finishObject

Returns the value of attribute on_finish.



39
40
41
# File 'lib/droonga/engine_state.rb', line 39

def on_finish
  @on_finish
end

#replierObject (readonly)

Returns the value of attribute replier.



37
38
39
# File 'lib/droonga/engine_state.rb', line 37

def replier
  @replier
end

Instance Method Details

#find_session(id) ⇒ Object



120
121
122
# File 'lib/droonga/engine_state.rb', line 120

def find_session(id)
  @sessions[id]
end

#generate_idObject



114
115
116
117
118
# File 'lib/droonga/engine_state.rb', line 114

def generate_id
  id = @current_id
  @current_id = id.succ
  return [@internal_name, id].join(".#")
end

#have_session?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/droonga/engine_state.rb', line 147

def have_session?
  not @sessions.empty?
end

#internal_farm_path(route) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/droonga/engine_state.rb', line 92

def internal_farm_path(route)
  name = Address.parse(route).node
  if name == @name
    @internal_name
  else
    name
  end
rescue ArgumentError
  route
end

#internal_route(route) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/droonga/engine_state.rb', line 70

def internal_route(route)
  name = Address.parse(route).node
  if name == @name
    route.sub(name, @internal_name)
  else
    route
  end
rescue ArgumentError
  route
end

#local_route?(route) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/droonga/engine_state.rb', line 66

def local_route?(route)
  route.start_with?(@name) or route.start_with?(@internal_name)
end

#public_farm_path(route) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/droonga/engine_state.rb', line 103

def public_farm_path(route)
  name = Address.parse(route).node
  if name == @internal_name
    @name
  else
    name
  end
rescue ArgumentError
  route
end

#public_route(route) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/droonga/engine_state.rb', line 81

def public_route(route)
  name = Address.parse(route).node
  if name == @internal_name
    route.sub(name, @name)
  else
    route
  end
rescue ArgumentError
  route
end

#register_session(id, session, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/droonga/engine_state.rb', line 124

def register_session(id, session, options={})
  @sessions[id] = session
  logger.trace("new session #{id} is registered. rest sessions=#{@sessions.size}")

  timeout = options[:timeout] ||
    @internal_connection_lifetime ||
    DEFAULT_SESSION_TIMEOUT_SECONDS
  session.set_timeout(@loop, timeout) do
    logger.trace("session #{id} is timed out!")
    unregister_session(id)
  end
end

#shutdownObject



60
61
62
63
64
# File 'lib/droonga/engine_state.rb', line 60

def shutdown
  logger.trace("shutdown: start")
  @forwarder.shutdown
  logger.trace("shutdown: done")
end

#startObject



54
55
56
57
58
# File 'lib/droonga/engine_state.rb', line 54

def start
  logger.trace("start start")
  @forwarder.start
  logger.trace("start done")
end

#unregister_session(id) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/droonga/engine_state.rb', line 137

def unregister_session(id)
  session = @sessions[id]
  session.finish
  @sessions.delete(id)
  unless have_session?
    @on_finish.call if @on_finish
  end
  logger.trace("session #{id} is unregistered. rest sessions=#{@sessions.size}")
end