Class: Droonga::AdapterRunner

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/droonga/adapter_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, plugins) ⇒ AdapterRunner

Returns a new instance of AdapterRunner.



26
27
28
29
30
31
32
33
# File 'lib/droonga/adapter_runner.rb', line 26

def initialize(dispatcher, plugins)
  @dispatcher = dispatcher
  default_plugins = ["error"]
  plugins += (default_plugins - plugins)
  logger.debug("activating plugins: #{plugins.join(", ")}")
  @adapter_classes = Adapter.find_sub_classes(plugins)
  logger.debug("activated:\n#{@adapter_classes.join("\n")}")
end

Instance Method Details

#adapt_input(message) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/droonga/adapter_runner.rb', line 38

def adapt_input(message)
  logger.trace("adapt_input: start",
               :dataset => message["dataset"],
               :type => message["type"])
  adapted_message = message
  adapted_message["appliedAdapters"] = []
  @adapter_classes.each do |adapter_class|
    adapter_class_id = adapter_class.id
    pattern = adapter_class.input_message.pattern
    if pattern
      matcher = MessageMatcher.new(pattern)
      logger.trace("adapt_input: skip: #{adapter_class_id}",
                   :pattern => pattern)
      next unless matcher.match?(adapted_message)
    end
    logger.trace("adapt_input: use: #{adapter_class_id}")
    input_message = InputMessage.new(adapted_message)
    adapter = adapter_class.new
    adapter.adapt_input(input_message)
    adapted_message = input_message.adapted_message
    adapted_message["appliedAdapters"] << adapter_class_id
  end
  logger.trace("adapt_input: done",
               :dataset => adapted_message["dataset"],
               :type => adapted_message["type"])
  adapted_message
end

#adapt_output(message) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/droonga/adapter_runner.rb', line 66

def adapt_output(message)
  logger.trace("adapt_output: start",
               :dataset => message["dataset"],
               :type => message["type"])
  adapted_message = message
  applied_adapters = adapted_message["appliedAdapters"]
  @adapter_classes.reverse_each do |adapter_class|
    adapter_class_id = adapter_class.id
    if applied_adapters
      logger.trace("adapt_output: skip: #{adapter_class_id}: " +
                     "input adapter wasn't applied",
                   :applied_adapters => applied_adapters)
      next unless applied_adapters.include?(adapter_class.id)
    end
    pattern = adapter_class.output_message.pattern
    if pattern
      matcher = MessageMatcher.new(pattern)
      logger.trace("adapt_output: skip: #{adapter_class_id}",
                   :pattern => pattern)
      next unless matcher.match?(adapted_message)
    end
    logger.trace("adapt_output: use: #{adapter_class_id}")
    output_message = OutputMessage.new(adapted_message)
    adapter = adapter_class.new
    adapter.adapt_output(output_message)
    adapted_message = output_message.adapted_message
  end
  logger.trace("adapt_output: done",
               :dataset => adapted_message["dataset"],
               :type => adapted_message["type"])
  adapted_message
end

#shutdownObject



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

def shutdown
end