Skip to content

Commit

Permalink
Avoid returning dupe common prefixes and refactor positional args
Browse files Browse the repository at this point in the history
into named accumulators in the state.
Dupe CommonPrefixes were caused by inclusion of unique {Key, CommonPrefix} tuple
in the ordset:add_element. Now we return {<<key_or_common_prefix>> [...]}
to achieve uniq|sort.
  • Loading branch information
Paul Henrich authored and jvoegele committed Apr 14, 2017
1 parent b80cee1 commit f3b9b17
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/riak_kv_group_keys_fsm.erl
Expand Up @@ -37,6 +37,8 @@
from :: from(),
bucket :: riak_object:bucket(),
entries_acc = ordsets:new() :: ordsets:ordset(any()),
metadatas_acc = [] :: list(),
common_prefixes_acc = [] :: list(),
group_params :: any()
}).

Expand Down Expand Up @@ -92,19 +94,24 @@ process_entries(_Bucket, Entries, State) ->
lists:foldl(fun process_entry/2, State, Entries).

%% By the time we get here we should only have results from TargetBucket; crash if we don't.
process_entry({{TargetBucket, Key}, Entry}, State = #state{bucket=TargetBucket}) ->
State#state{entries_acc = ordsets:add_element({Key, Entry}, State#state.entries_acc)}.
%% for sorting reasons, entries_acc stores both kinds of entry {InterleavingSortKey ...}
process_entry({{TargetBucket, Key}, Metadata = {metadata, _M}}, State = #state{bucket=TargetBucket}) ->
State#state{entries_acc = ordsets:add_element({Key, Metadata}, State#state.entries_acc)};
process_entry({{TargetBucket, _}, {common_prefix, CommonPrefix}}, State = #state{bucket=TargetBucket})->
State#state{entries_acc = ordsets:add_element({CommonPrefix}, State#state.entries_acc)}.

collate_list_group_keys(#state{entries_acc = Entries0}) ->
collate_list_group_keys(State0 = #state{entries_acc = Entries0}) ->
%% TODO: encapsulate GroupParams so we can extract the actual max keys in a reasonable way
MaxKeys = 1000,
Entries = ordsets:to_list(Entries0),
TruncatedEntries = lists:sublist(Entries, MaxKeys),
{CommonPrefixes, Metadatas} = lists:foldr(fun partition_entry/2, {[],[]}, TruncatedEntries),
[{common_prefixes, CommonPrefixes},
{metadatas, Metadatas}].

partition_entry({_Key, {common_prefix, CommonPrefix}}, {CommonPrefixesAcc, MetadatasAcc}) ->
{[CommonPrefix|CommonPrefixesAcc], MetadatasAcc};
partition_entry({Key, {metadata, Metadata}}, {CommonPrefixesAcc, MetadatasAcc}) ->
{CommonPrefixesAcc, [{Key, Metadata}|MetadatasAcc]}.
State = lists:foldr(fun partition_entry/2, State0, TruncatedEntries),
[{common_prefixes, State#state.common_prefixes_acc},
{metadatas, State#state.metadatas_acc}].

partition_entry({Key, {metadata, Metadata}},
State = #state{metadatas_acc = MetadatasAcc}) ->
State#state{metadatas_acc = [{Key, Metadata} | MetadatasAcc]};
partition_entry({CommonPrefix},
State = #state{common_prefixes_acc = CommonPrefixesAcc}) ->
State#state{common_prefixes_acc = [CommonPrefix | CommonPrefixesAcc]}.

0 comments on commit f3b9b17

Please sign in to comment.