Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Partition All Docs Query With Keys #4152

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/fabric/src/fabric_view_all_docs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ all_docs_concurrency() ->
10
end.

validate_if_partition(Row, Acc) ->
case chttpd:qs_value(Acc#vacc.req, "partition") of
undefined -> Row;
Partition -> is_doc_in_partition(Row, list_to_binary(Partition))
end.
is_doc_in_partition(Row, Partition) ->
case couch_partition:is_member(Row#view_row.key, Partition) of
true -> Row;
false -> #view_row{key = Row#view_row.key}
end.

doc_receive_loop(Keys, Pids, SpawnFun, MaxJobs, Callback, AccIn) ->
case {Keys, queue:len(Pids)} of
{[], 0} ->
Expand All @@ -298,9 +309,10 @@ doc_receive_loop(Keys, Pids, SpawnFun, MaxJobs, Callback, AccIn) ->
Timeout = fabric_util:all_docs_timeout(),
receive
{'DOWN', Ref, process, Pid, Row} ->
case Row of
ValidRow = validate_if_partition(Row, AccIn),
case ValidRow of
#view_row{} ->
case Callback(fabric_view:transform_row(Row), AccIn) of
case Callback(fabric_view:transform_row(ValidRow), AccIn) of
{ok, Acc} ->
doc_receive_loop(
Keys, RestPids, SpawnFun, MaxJobs, Callback, Acc
Expand Down
2 changes: 1 addition & 1 deletion test/elixir/test/config/suite.elixir
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"partition _all_docs works with limit",
"partition all docs can set query limits",
"partition all_docs errors with incorrect partition supplied",
"partitioned _all_docs works with keys",
"partitioned _all_docs works with keys and rejects docs not in partition",
"partitioned _all_docs works with startkey, endkey range"
],
"PartitionCrudTest": [
Expand Down
15 changes: 11 additions & 4 deletions test/elixir/test/partition_all_docs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,22 @@ defmodule PartitionAllDocsTest do
assert Enum.dedup(partitions) == ["foo"]
end

test "partitioned _all_docs works with keys", context do
test "partitioned _all_docs works with keys and rejects docs not in partition", context do
db_name = context[:db_name]

url = "/#{db_name}/_partition/foo/_all_docs"
resp = Couch.post(url, body: %{keys: ["foo:2", "foo:4", "foo:6"]})
resp = Couch.post(url, body: %{keys: ["foo:2", "foo:4", "foo:6", "bar:7", "bar:11"]})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 3
assert ids == ["foo:2", "foo:4", "foo:6"]
assert length(ids) == 5
assert ids == ["foo:2", "foo:4", "foo:6", nil, nil]

url2 = "/#{db_name}/_partition/bar/_all_docs"
resp2 = Couch.post(url2, body: %{keys: ["bar:3", "bar:5", "bar:7", "foo:6", "foo:10"]})
assert resp2.status_code == 200
ids2 = get_ids(resp2)
assert length(ids2) == 5
assert ids2 == ["bar:3", "bar:5", "bar:7", nil, nil]
end

test "partition _all_docs works with limit", context do
Expand Down