Navigation Menu

Skip to content

Commit

Permalink
groonga: Convert drilldown results
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2014
1 parent 250f4c2 commit 6903ffa
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions lib/droonga/plugins/groonga/select.rb
Expand Up @@ -19,6 +19,8 @@ module Droonga
module Plugins
module Groonga
module Select
DRILLDOWN_RESULT_PREFIX = "drilldown_result_"

class RequestConverter
def convert(select_request)
@table = select_request["table"]
Expand Down Expand Up @@ -114,6 +116,7 @@ def convert_drilldown(select_request)
"output" => {
"elements" => [
"count",
"attributes",
"records",
],
"attributes" => columns,
Expand All @@ -131,51 +134,68 @@ def convert_drilldown(select_request)
}
end

queries["drilldown_result_#{key}"] = query
queries["#{DRILLDOWN_RESULT_PREFIX}#{key}"] = query
end
queries
end
end

class ResponseConverter
def convert(search_response)
@drilldown_results = []
search_response.each do |key, value|
convert_main_result(value)
if key.start_with?(DRILLDOWN_RESULT_PREFIX)
key = key[DRILLDOWN_RESULT_PREFIX.size..-1]
convert_drilldown_result(key, value)
else
convert_main_result(value)
end
end

select_results = [@header, [@body]]
unless @drilldown_results.empty?
select_results.last += @drilldown_results
end

select_results
end

private
def convert_main_result(result)
status_code = 0
start_time = result["startTime"]
start_time_in_unix_time = if start_time
Time.parse(start_time).to_f
else
Time.now.to_f
end
elapsed_time = result["elapsedTime"] || 0
count = result["count"]
@header = [status_code, start_time_in_unix_time, elapsed_time]
@body = convert_search_result(result)
end

attributes = result["attributes"] || []
converted_attributes = attributes.collect do |attribute|
name = attribute["name"]
type = attribute["type"]
[name, type]
end
def convert_drilldown_result(key, result)
@drilldown_results << convert_search_result(result)
end

status_code = 0
@header = [status_code, start_time_in_unix_time, elapsed_time]
records = result["records"]
def convert_search_result(result)
count = result["count"]
attributes = convert_attributes(result["attributes"])
records = result["records"]
if records.empty?
results = [[count], converted_attributes]
[[count], attributes]
else
results = [[count], converted_attributes, records]
[[count], attributes, records]
end
end

@body = results
def convert_attributes(attributes)
attributes = attributes || []
attributes.collect do |attribute|
name = attribute["name"]
type = attribute["type"]
[name, type]
end
end
end

Expand Down

0 comments on commit 6903ffa

Please sign in to comment.