Skip to content

Commit

Permalink
Add information about using bucket types in input lists and test for …
Browse files Browse the repository at this point in the history
…such.
  • Loading branch information
Luke Bakken committed Feb 3, 2017
1 parent f3e6401 commit 82ae32b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,14 @@ It is possible to define a wide range of inputs for a mapreduce job. Some exampl

**Bucket/Key list:** `[{<<"bucket1">>,<<"key1">>},{<<"bucket1">>,<<"key2">>}]`

**Bucket Type/Bucket/Key list:** `[{{{<<"type1">>,<<"bucket1">>},<<"key1">>},key_data1},{{{<<"type1">>,<<"bucket1">>},<<"key2">>},key_data2}]`

*Note*: due to [a bug](https://github.com/basho/riak_kv/issues/1623), you must include "key data" in the above input list to be able to include bucket type.

**All keys in a bucket:** `<<"bucket1">>`

**All keys in a typed bucket:** `{<<"type1">>,<<"bucket1">>}`

**Result of exact secondary index match:** `{index, <<"bucket1">>, {binary_index, "idx"}, <<"key">>}`, `{index, <<"bucket1">>, <<"idx_bin">>, <<"key">>}`

**Result of secondary index range query:** `{index, <<"bucket1">>, {integer_index, "idx"}, 1, 100}`, `{index, <<"bucket1">>, <<"idx_int">>, <<"1">>, <<"100">>}`
Expand Down
57 changes: 51 additions & 6 deletions test/riakc_pb_socket_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,40 @@ integration_tests() ->
{ok, Pid} = riakc_test_utils:start_link(),
B = <<"bucket">>,
K = <<"foo">>,
O=riakc_obj:new(B, K),
O = riakc_obj:new(B, K),
riakc_pb_socket:put(Pid, riakc_obj:update_value(O, <<"2">>, "application/json")),
Inputs = [{B, K}],
Args = [{map, {jsanon, <<"function (v) { return [JSON.parse(v.values[0].data)]; }">>}, undefined, true}],
Want = {ok, [{0, [2]}]},
Got = riakc_pb_socket:mapred(Pid, Inputs, Args),
?assertMatch(Want, Got)
end)},

?assertEqual({ok, [{0, [2]}]},
riakc_pb_socket:mapred(Pid,
[{B, K}],
[{map, {jsanon, <<"function (v) { return [JSON.parse(v.values[0].data)]; }">>},
undefined, true}]))
{"javascript_source_map_with_bucket_type_test()",
?_test(begin
riakc_test_utils:reset_riak(),
{ok, Pid} = riakc_test_utils:start_link(),
BT = <<"plain">>,
B = <<"my_bucket">>,
BWT = {BT, B}, % NB: Bucket With Type
K = <<"baz">>,
Value = <<"bat">>,
O = riakc_obj:new(BWT, K),
O2 = riakc_obj:update_value(O, Value, "application/json"),
?assertMatch(BT, riakc_obj:bucket_type(O2)),
?assertMatch(BWT, riakc_obj:bucket(O2)),
?assertMatch(K, riakc_obj:key(O2)),
?assertMatch(ok, riakc_pb_socket:put(Pid, O2)),
{ok, GotObj} = riakc_pb_socket:get(Pid, BWT, K),
?assertMatch(BT, riakc_obj:bucket_type(GotObj)),
?assertMatch(BWT, riakc_obj:bucket(GotObj)),
?assertMatch(K, riakc_obj:key(GotObj)),
% NB: see basho/riak_kv#1623
Inputs = [{{BWT, K}, ignored},{{BWT, K}, ignored}],
Args = [{map, {jsanon, <<"function (v) { return [v.values[0].data]; }">>}, undefined, true}],
Want = {ok, [{0, [Value, Value]}]},
Got = riakc_pb_socket:mapred(Pid, Inputs, Args),
?assertMatch(Want, Got)
end)},

{"javascript_named_map_test()",
Expand Down Expand Up @@ -520,6 +546,25 @@ integration_tests() ->
{reduce, {jsfun, <<"Riak.reduceSum">>}, undefined, true}]))
end)},

{"javascript_bucket_type_map_reduce_test()",
?_test(begin
riakc_test_utils:reset_riak(),
{ok, Pid} = riakc_test_utils:start_link(),
BT = {<<"plain">>,<<"bucket">>},
Store = fun({K, V}) ->
O=riakc_obj:new(BT, K),
riakc_pb_socket:put(Pid,riakc_obj:update_value(O, V, "application/json"))
end,
[Store(KV) || KV <- [{<<"foo">>, <<"2">>},
{<<"bar">>, <<"3">>},
{<<"baz">>, <<"4">>}]],
Want = {ok, [{1, [9]}]},
Query = [{map, {jsfun, <<"Riak.mapValuesJson">>}, undefined, false},
{reduce, {jsfun, <<"Riak.reduceSum">>}, undefined, true}],
Got = riakc_pb_socket:mapred_bucket(Pid, BT, Query),
?assertMatch(Want, Got)
end)},

{"javascript_arg_map_reduce_test()",
?_test(begin
riakc_test_utils:reset_riak(),
Expand Down

0 comments on commit 82ae32b

Please sign in to comment.