Skip to content

Commit

Permalink
Support slices for select command
Browse files Browse the repository at this point in the history
  • Loading branch information
Masafumi Yokoyama committed Dec 6, 2016
1 parent f888df6 commit 7768690
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/groonga/client/response/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class Select < Base
# Otherwise, `[drilldown1, drilldown2]` is returned.
attr_accessor :drilldowns

# @return [::Hash<String, Groonga::Client::Response::Select>]
attr_accessor :slices

def body=(body)
super(body)
parse_body(body)
Expand All @@ -46,10 +49,27 @@ def body=(body)
def parse_body(body)
if body.is_a?(::Array)
@n_hits, @records = parse_match_records_v1(body.first)
@drilldowns = parse_drilldowns_v1(body[1..-1])
body[1..-1].each do |record|
if record.is_a?(::Hash) &&
record.first[1][1].none? {|key| key[0] == "_nsubrecs"}
@slices = {}
record.each do |key, slice_body|
n_hits, body = parse_match_records_v1(slice_body)
@slices[key] = body
end
else
@drilldowns = parse_drilldowns_v1([record])
end
end
else
@n_hits, @records = parse_match_records_v3(body)
@drilldowns = parse_drilldowns_v3(body["drilldowns"])
@slices = {}
if body["slices"]
body["slices"].each do |key, records|
@slices[key] = parse_match_records_v3(records)
end
end
end
body
end
Expand Down
59 changes: 59 additions & 0 deletions test/response/test-select-command-version1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,64 @@ def collect_values(body)
values
end
end

class TestSlices < self
def setup
pair_arguments = {
"slices[groonga].filter" => 'tag @ "groonga"',
}
@command = Groonga::Command::Select.new("select", pair_arguments)
@body = [
[
[3],
[
[
"_id",
"UInt32"
],
[
"tag",
"ShortText"
]
],
[1, "groonga"],
[2, "rroonga"],
[3, "groonga"],
],
{
"groonga" => [
[2],
[
[
"_id",
"UInt32"
],
[
"tag",
"ShortText"
]
],
[1, "groonga"],
[3, "groonga"],
]
}
]
end

def test_slices
assert_equal({
"groonga" => [
{"_id" => 1, "tag" => "groonga"},
{"_id" => 3, "tag" => "groonga"},
]
},
slices(@body))
end

private
def slices(body)
create_response(body).slices
end
end
end
end

0 comments on commit 7768690

Please sign in to comment.