Skip to content

Commit

Permalink
adapter: split adapter into input/output adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 20, 2013
1 parent 4b43cb1 commit 0ae60de
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 50 deletions.
Expand Up @@ -14,32 +14,21 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/input_adapter_plugin"
require "droonga/output_adapter_plugin"

module Droonga
class GroongaInputAdapter < Droonga::InputAdapterPlugin
repository.register("groonga", self)

command :select
def select(input_message)
command = GroongaAdapter::Select.new
command = Select.new
select_request = input_message.body
search_request = command.convert_request(select_request)
search_request = command.convert(select_request)
input_message.add_route("select_response")
input_message.command = "search"
input_message.body = search_request
end
end

class GroongaOutputAdapter < Droonga::OutputAdapterPlugin
repository.register("groonga", self)

command :select_response
def select_response(search_response)
command = GroongaAdapter::Select.new
emit(command.convert_response(search_response))
end
end
end

require "droonga/plugin/adapter/groonga/select"
require "droonga/plugin/input_adapter/groonga/select"
Expand Up @@ -14,9 +14,9 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

module Droonga
class GroongaAdapter
class GroongaInputAdapter
class Select
def convert_request(select_request)
def convert(select_request)
table = select_request["table"]
result_name = table + "_result"
match_columns = select_request["match_columns"]
Expand Down Expand Up @@ -58,40 +58,6 @@ def convert_request(select_request)
end
search_request
end

def convert_response(search_response)
select_responses = search_response.collect do |key, value|
status_code = 0

start_time = value["startTime"]
start_time_in_unix_time = if start_time
Time.parse(start_time).to_f
else
Time.now.to_f
end
elapsed_time = value["elapsedTime"] || 0
count = value["count"]

attributes = value["attributes"] || []
converted_attributes = attributes.collect do |attribute|
name = attribute["name"]
type = attribute["type"]
[name, type]
end

header = [status_code, start_time_in_unix_time, elapsed_time]
records = value["records"]
if records.empty?
results = [[count], converted_attributes]
else
results = [[count], converted_attributes, records]
end
body = [results]

[header, body]
end
select_responses.first
end
end
end
end
30 changes: 30 additions & 0 deletions lib/droonga/plugin/output_adapter/groonga.rb
@@ -0,0 +1,30 @@
# Copyright (C) 2013 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/output_adapter_plugin"

module Droonga
class GroongaOutputAdapter < Droonga::OutputAdapterPlugin
repository.register("groonga", self)

command :select_response
def select_response(search_response)
command = Select.new
emit(command.convert(search_response))
end
end
end

require "droonga/plugin/output_adapter/groonga/select"
54 changes: 54 additions & 0 deletions lib/droonga/plugin/output_adapter/groonga/select.rb
@@ -0,0 +1,54 @@
# Copyright (C) 2013 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

module Droonga
class GroongaOutputAdapter
class Select
def convert(search_response)
select_responses = search_response.collect do |key, value|
status_code = 0

start_time = value["startTime"]
start_time_in_unix_time = if start_time
Time.parse(start_time).to_f
else
Time.now.to_f
end
elapsed_time = value["elapsedTime"] || 0
count = value["count"]

attributes = value["attributes"] || []
converted_attributes = attributes.collect do |attribute|
name = attribute["name"]
type = attribute["type"]
[name, type]
end

header = [status_code, start_time_in_unix_time, elapsed_time]
records = value["records"]
if records.empty?
results = [[count], converted_attributes]
else
results = [[count], converted_attributes, records]
end
body = [results]

[header, body]
end
select_responses.first
end
end
end
end

0 comments on commit 0ae60de

Please sign in to comment.