<commit_before>-module(mc_bucket).

-include_lib("eunit/include/eunit.hrl").

-include("mc_constants.hrl").

-include("mc_entry.hrl").

-compile(export_all).

%% API for buckets.

%% TODO: A proper implementation.
%% TODO: Consider replacing implementation with gen_server.

% Callers should consider the returned value to be opaque.
% One day, the return value, for example, might be changed
% into a gen_server Pid.
create(BucketId, BucketAddrs, BucketPolicy) ->
    #mc_bucket{id = BucketId,
               addrs = BucketAddrs,
               policy = BucketPolicy}.

id(#mc_bucket{id = Id})          -> Id.
addrs(#mc_bucket{addrs = Addrs}) -> Addrs.

% Choose the Addr that should contain the Key.
choose_addr(#mc_bucket{addrs = Addrs}, Key) ->
    % TODO: A proper consistent hashing.
    {Key, hd(Addrs)}.

% Choose several Addr's that should contain the Key given replication,
% with the primary Addr coming first.  The number of Addr's returned
% is based on Bucket default replication level.
choose_addrs(#mc_bucket{addrs = Addrs, policy = Policy}, Key) ->
    % TODO: A proper consistent hashing.
    {Key, Addrs, Policy}.

% Choose several Addr's that should contain the Key given replication,
% with the primary Addr coming first.  The result Addr's list might
% have length <= N.
choose_addrs(#mc_bucket{addrs = Addrs, policy = Policy}, Key, N) ->
    % TODO: A proper consistent hashing.
    {Key, lists:sublist(Addrs, N), Policy}.

% ------------------------------------------------

choose_addr_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({key1, a1}, choose_addr(B1, key1)),
    ?assertMatch({key2, a1}, choose_addr(B1, key2)),
    ok.

choose_addrs_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({key5, [a1], policy}, choose_addrs(B1, key5, 1)),
    ?assertMatch({key6, [a1], policy}, choose_addrs(B1, key6, 1)),
    ok.

choose_addr_str_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({"key1", a1}, choose_addr(B1, "key1")),
    ?assertMatch({"key2", a1}, choose_addr(B1, "key2")),
    ok.

choose_addrs_str_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({"key5", [a1], policy}, choose_addrs(B1, "key5", 1)),
    ?assertMatch({"key6", [a1], policy}, choose_addrs(B1, "key6", 1)),
    ok.

<commit_msg>simplified param names<commit_after>-module(mc_bucket).

-include_lib("eunit/include/eunit.hrl").

-include("mc_constants.hrl").

-include("mc_entry.hrl").

-compile(export_all).

%% API for buckets.

%% TODO: A proper implementation.
%% TODO: Consider replacing implementation with gen_server.

% Callers should consider the returned value to be opaque.
% One day, the return value, for example, might be changed
% into a gen_server Pid.
create(Id, Addrs, Policy) ->
    #mc_bucket{id = Id,
               addrs = Addrs,
               policy = Policy}.

id(#mc_bucket{id = Id})          -> Id.
addrs(#mc_bucket{addrs = Addrs}) -> Addrs.

% Choose the Addr that should contain the Key.
choose_addr(#mc_bucket{addrs = Addrs}, Key) ->
    % TODO: A proper consistent hashing.
    {Key, hd(Addrs)}.

% Choose several Addr's that should contain the Key given replication,
% with the primary Addr coming first.  The number of Addr's returned
% is based on Bucket default replication level.
choose_addrs(#mc_bucket{addrs = Addrs, policy = Policy}, Key) ->
    % TODO: A proper consistent hashing.
    {Key, Addrs, Policy}.

% Choose several Addr's that should contain the Key given replication,
% with the primary Addr coming first.  The result Addr's list might
% have length <= N.
choose_addrs(#mc_bucket{addrs = Addrs, policy = Policy}, Key, N) ->
    % TODO: A proper consistent hashing.
    {Key, lists:sublist(Addrs, N), Policy}.

% ------------------------------------------------

choose_addr_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({key1, a1}, choose_addr(B1, key1)),
    ?assertMatch({key2, a1}, choose_addr(B1, key2)),
    ok.

choose_addrs_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({key5, [a1], policy}, choose_addrs(B1, key5, 1)),
    ?assertMatch({key6, [a1], policy}, choose_addrs(B1, key6, 1)),
    ok.

choose_addr_str_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({"key1", a1}, choose_addr(B1, "key1")),
    ?assertMatch({"key2", a1}, choose_addr(B1, "key2")),
    ok.

choose_addrs_str_test() ->
    B1 = create(buck1, [a1], policy),
    ?assertMatch({"key5", [a1], policy}, choose_addrs(B1, "key5", 1)),
    ?assertMatch({"key6", [a1], policy}, choose_addrs(B1, "key6", 1)),
    ok.


{'fraction_non_alphanumeric': 0.1702127659574468, 'fraction_numerical': 0.021057249396797543, 'mean_word_length': 3.4837758112094397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6154969', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1571, 'n_words': 438}